SpringBoot整合Mybatis

SpringBoot整合MyBatis

作者:gqk


1.导入 MyBatis 所需要的依赖

 1  <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-jdbc</artifactId>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.mybatis.spring.boot</groupId>
 8             <artifactId>mybatis-spring-boot-starter</artifactId>
 9             <version>2.1.4</version>
10         </dependency>
11 
12         <dependency>
13             <groupId>org.springframework.boot</groupId>
14             <artifactId>spring-boot-devtools</artifactId>
15             <scope>runtime</scope>
16             <optional>true</optional>
17         </dependency>
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-configuration-processor</artifactId>
21             <optional>true</optional>
22         </dependency>
23         <dependency>
24             <groupId>org.projectlombok</groupId>
25             <artifactId>lombok</artifactId>
26             <optional>true</optional>
27         </dependency>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-test</artifactId>
31             <scope>test</scope>
32         </dependency>
33         <dependency>
34             <groupId>mysql</groupId>
35             <artifactId>mysql-connector-java</artifactId>
36         </dependency>
37     </dependencies>
View Code

2.配置数据库连接信息(不变)

 1 spring:
 2   datasource:
 3     username: root
 4     password: root
 5     #?serverTimezone=UTC解决时区的报错
 6     url: jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
 7     driver-class-name: com.mysql.cj.jdbc.Driver
 8     #Spring Boot 默认是不注入这些属性值的,需要自己绑定
 9     #druid 数据源专有配置
10     initialSize: 5
11     minIdle: 5
12     maxActive: 20
13     maxWait: 60000
14     timeBetweenEvictionRunsMillis: 60000
15     minEvictableIdleTimeMillis: 300000
16     validationQuery: SELECT 1 FROM DUAL
17     testWhileIdle: true
18     testOnBorrow: false
19     testOnReturn: false
20     poolPreparedStatements: true
21 
22   #mybatis
23 mybatis:
24   mapper-locations: classpath:com/ds/mapper/*.xml
25   type-aliases-package: com.ds.entity
26 
27 
28 #showSql
29 logging:
30   level:
31     com:
32       ds:
33         mapper : debug
View Code

3.测试数据库是否连接成功!

1   @Autowired
2     private UserMapper userMapper;
3     @Test
4     void contextLoads() throws SQLException {
5         System.out.println(dataSource.getConnection());
6     }
View Code

表示数据库连接成功;

4.创建实体类,导入 Lombok!

 1 import lombok.AllArgsConstructor;
 2 import lombok.Data;
 3 import lombok.NoArgsConstructor;
 4 @NoArgsConstructor
 5 @AllArgsConstructor
 6 @Data
 7 public class User {
 8     private int id;
 9     private String name;
10     private int age;
11     private String email;
12 
13 }
View Code

5.创建mapper目录以及对应的 Mapper 接口

1 @Mapper
2 @Repository
3 public interface UserMapper {
4     List<User> queryUserList();
5     //User queryUserById(int id);
6     //int addUser(User user);
7     //int updateUser(User user);
8     //int deleteUser(int id);
9 }
View Code

6.对应的Mapper映射文件

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3               "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4 <mapper namespace="com.ds.mapper.UserMapper">
5     <select id="queryUserList" resultType="User">
6         select * from user
7     </select>
8 </mapper>
View Code

7.测试

1  @Test
2     void queryAll(){
3         List<User> users = userMapper.queryUserList();
4         for(User user :users){
5             System.out.println(user);
6         }
7     }
View Code

posted @ 2021-02-24 10:37  少侠gqk  阅读(64)  评论(0编辑  收藏  举报