SpringBoot--整合Mybatis & 删除无效的文件和文件夹
Posted Z && Y
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot--整合Mybatis & 删除无效的文件和文件夹相关的知识,希望对你有一定的参考价值。
1. SpringBoot--整和Mybatis
mybatis-spring-boot-autoconfigure官方文档
1.1 新建一个项目
删除无效的文件和文件夹
1.2 建立数据库表
create database `mybatis`;
USE `mybatis`;
CREATE TABLE `user` (
`id` INT NOT NULL PRIMARY KEY,
`name` VARCHAR ( 30 ) DEFAULT NULL,
`pwd` VARCHAR ( 30 ) DEFAULT NULL
) ENGINE = INNODB DEFAULT CHARSET = utf8;
INSERT INTO USER (`id`, `name`, `pwd`) VALUES
(1,'余余', '123456'),
(2, '磊磊', '123456'),
(3, '骄骄', '123456');
1.3 导入 MyBatis 所需要的依赖
pom.xml
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
1.4 配置数据库连接信息
application.properties
spring.datasource.username=root
spring.datasource.password=317525
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1.5 创建实体类,导入 Lombok
Lombok相关依赖
pom.xml
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
User.java
package com.tian.springbootmybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* ClassName: User
* Description: User的实体类
*
* @author Tianjiao
* @date 2021/5/27 15:07
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
1.6 创建mapper目录以及对应的 Mapper 接口
package com.tian.springbootmybatis.mapper;
import com.tian.springbootmybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* ClassName: UserMapper
* Description: User实体类的映射
*
* @author Tianjiao
* @date 2021/5/27 15:17
*/
// @Mapper : 表示本类是一个 MyBatis 的 Mapper
@Mapper
@Repository
public interface UserMapper {
// 获取所有部门信息
List<User> getUsers();
// 通过id获得部门
User getUserById(Integer id);
}
也可以在SpringbootMybatisApplication扫描mapper
package com.tian.springbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// 扫描这个包下面的mapper
@MapperScan("com.tian.springbootmybatis.mapper")
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
1.7 编写对应的Mapper映射文件
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tian.springbootmybatis.mapper.UserMapper">
<select id="getUsers" resultType="User">
select *
from mybatis.user;
</select>
<select id="getUserById" resultType="User">
select *
from mybatis.user
where id = #{id};
</select>
</mapper>
1.8 整合Mybatis
application.properties
spring.datasource.username=root
spring.datasource.password=317525
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 整合mybatis
# 配置别名
mybatis.type-aliases-package=com.tian.springbootmybatis.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
1.9 编写相应的controller
UserController.java
package com.tian.springbootmybatis.controller;
import com.tian.springbootmybatis.mapper.UserMapper;
import com.tian.springbootmybatis.pojo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* ClassName: UserController
* Description:
*
* @author Tianjiao
* @date 2021/5/27 17:16
*/
@RestController
public class UserController {
final UserMapper userMapper;
public UserController(UserMapper userMapper) {
this.userMapper = userMapper;
}
@RequestMapping("/users")
List<User> getUsers() {
return userMapper.getUsers();
}
@RequestMapping("/userByID/{id}")
User getUser(@PathVariable("id") int id) {
return userMapper.getUserById(id);
}
}
2.0 运行结果
以上是关于SpringBoot--整合Mybatis & 删除无效的文件和文件夹的主要内容,如果未能解决你的问题,请参考以下文章
企业分布式微服务云SpringCloud SpringBoot mybatis (十三)Spring Boot整合MyBatis
SpringBoot整合Mybatis方式2:使用注解方式整合Mybatis