MyBatis-Spring-Boot-Starter官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
maven仓库:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter
依赖导入
1、导入 MyBatis 所需要的依赖
<!-- mybatis整合springboot --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency>
2.、其他用到的依赖
pom.xml
<!-- web(web项目必须要的依赖,不然启动的就不是web项目) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mysql(数据库连接依赖) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- jdbc(mybatis-spring-boot-starter下存在这个包,可以不用导入) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- lombok(简化实体类) --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- test(springboot自带依赖) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
3、配置数据库连接信息
application.yaml
spring: # 配置数据源 datasource: username: root # 用户名 password: 123456 # 密码 # ?serverTimezone=UTC解决时区的报错 url: jdbc:mysql://localhost:3306/mybatistest?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动,新版用cj # 整合mybatis mybatis: type-aliases-package: com.yzh.pojo mapper-locations: classpath:mybatis/mapper/*Mapper.xml
4、测试数据库是否连接成功!(在测试类中注入DataSource,获取class信息和connection信息)
@SpringBootTest class Springboot06MybatisApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() throws SQLException { System.out.println(dataSource.getClass()); System.out.println(dataSource.getConnection()); } }
5、创建实体类,导入 Lombok!(使用Lombok必须导入依赖和配置插件)
User.java
@Data @AllArgsConstructor @NoArgsConstructor public class User { private Integer id; // ID private String name; // 用户名 private String pwd; // 密码 }
6、创建mapper目录以及对应的 Mapper 接口
//@Mapper // 该注解表示这是 mybatis 的 mapper 类,也可以去主方法配置@MapperScan包扫描 //@Component // @Component 和 @Repository任选一个,都是注册作用,@Component更万能 @Repository //(可以不导入,但是使用@Autowired注入的时候可能不能被识别) public interface UserMapper { List<User> queryUserList(); User queryUserById(Integer id); int addUser(User user); int updateUser(User user); int deleteUser(Integer id); }
7、对应的Mapper映射文件(放在了resource下的mybatis/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.yzh.mapper.UserMapper"> <select id="queryUserList" resultType="User"> select * from mybatistest.user; </select> <select id="queryUserById" resultType="User"> select * from mybatistest.user where id = #{id}; </select> <insert id="addUser" parameterType="User"> insert into mybatistest.user(id, name, pwd) values (#{id}, #{name}, #{pwd}); </insert> <update id="updateUser" parameterType="User"> update mybatistest.user set name = #{name}, pwd = #{pwd} where id = #{id}; </update> <delete id="deleteUser" parameterType="int"> delete from mybatistest.user where id = #{id}; </delete> </mapper>
8、maven配置资源过滤问题(在pom文件的build标签下)
pom.xml
<!-- 资源过滤 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources>
9、编写Controller 进行测试!
UserController.java
@RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/userlist") public List<User> queryUserList(){ return userMapper.queryUserList(); } @GetMapping("/user/{id}") public User queryUserById(@PathVariable("id") Integer id){ return userMapper.queryUserById(id); } @GetMapping("/adduser") public String addUser(){ User user = new User(6,"admin4","111"); userMapper.addUser(user); return "addok"; } @GetMapping("/updateuser/{id}") public String updateUser(){ User user = new User(6,"admin","000"); userMapper.updateUser(user); return "updateok"; } @GetMapping("delete/{id}") public String deleteUser(@PathVariable("id") Integer id){ userMapper.deleteUser(id); return "deleteok"; } }
10、启动项目访问进行测试!
localhost:8080/下的各个方法依次测试。
至此,springboot整合mybatis 并且进行增删改查操作就全部结束了。