SpringBoot 整合 Mybatis-Plus + Mysql

mybatis-plus是mybatis的一款插件,它的主要作用是快速开发,省略mybatis的配置,具体的功能请参照官网。

开发环境:

springboot,maven,mybatis-plus,mysql,jdk1.8,lombok,阿里druid数据源

整合步骤:

1、在pom.xml加入相关配置

2、在resources中添加application.yml,设置mysql相关配置

3、在基类中添加表名和表名中对应的字段名

4、定义mapper接口,继承BaseMapper<T>接口,该接口中封装了Sql

5、在启动类中添加mapper扫描的包

5、调用mapper接口即可

项目架构:

SpringBoot 整合 Mybatis-Plus + Mysql

具体步骤代码:

1、在pom.xml加入相关配置

  1. <!-- spring-boot-starter-parent 和 mybatis-plus-boot-starter 有版本要求-->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.0.3.RELEASE</version>
  6. </parent>
  7.  
  8.  
  9. <dependencies>
  10. <!-- spring-boot -->
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-web</artifactId>
  14. </dependency>
  15.  
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-test</artifactId>
  23. <scope>test</scope>
  24. </dependency>
  25.  
  26.  
  27. <!--mybatis-plus自动的维护了mybatis以及mybatis-spring的依赖,
  28. 在springboot中这三者不能同时的出现,避免版本的冲突,表示:跳进过这个坑-->
  29. <dependency>
  30. <groupId>com.baomidou</groupId>
  31. <artifactId>mybatis-plus-boot-starter</artifactId>
  32. <version>3.0.1</version>
  33. </dependency>
  34.  
  35. <!-- 引入Druid依赖,阿里巴巴所提供的数据源 -->
  36. <dependency>
  37. <groupId>com.alibaba</groupId>
  38. <artifactId>druid</artifactId>
  39. <version>1.0.29</version>
  40. </dependency>
  41.  
  42. <!-- 提供mysql驱动 -->
  43. <dependency>
  44. <groupId>mysql</groupId>
  45. <artifactId>mysql-connector-java</artifactId>
  46. <version>5.1.38</version>
  47. </dependency>
  48.  
  49. <dependency>
  50. <groupId>org.projectlombok</groupId>
  51. <artifactId>lombok</artifactId>
  52. <version>1.16.16</version>
  53. </dependency>
  54.  
  55. </dependencies>

2、在resources中添加application.yml,设置mysql相关配置

  1. server:
  2. port: 2525
  3.  
  4. # 该配置的名称是固定的,不可以改变,否则将不能自动加载到数据源中
  5. spring:
  6. datasource:
  7. # 使用druid数据源
  8. type: com.alibaba.druid.pool.DruidDataSource
  9. driver-class-name: com.mysql.jdbc.Driver
  10. url: jdbc:mysql://192.168.3.172:3306/poc?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
  11. username: devuser
  12. password: dev123
  13. filters: stat
  14. maxActive: 20
  15. initialSize: 1
  16. maxWait: 60000
  17. minIdle: 1
  18. timeBetweenEvictionRunsMillis: 60000
  19. minEvictableIdleTimeMillis: 300000
  20. validationQuery: select 1 FROM DUAL
  21. testWhileIdle: true
  22. testOnBorrow: false
  23. testOnReturn: false
  24. poolPreparedStatements: true
  25. maxOpenPreparedStatements: 20

3、在基类中添加表名和表名中对应的字段名

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @TableName("tb_test_student") //对应表名
  5. public class Student implements Serializable {
  6.  
  7. //对应id,可不填
  8. @TableId(value = "id")
  9. private int id;
  10.  
  11. //对应字段名,如果属性名和字段名一致,可不填
  12. @TableField("name")
  13. private String name;
  14.  
  15. private String school;
  16.  
  17. private String city;
  18.  
  19. //表示表中没有这个字段,如果不加该注释,会抛异常
  20. @TableField(exist = false)
  21. private String address;
  22. }

4、定义mapper接口,继承BaseMapper<T>接口,该接口中封装了Sql

  1. public interface StudentMapper extends BaseMapper<Student> {
  2.  
  3. }

5、在启动类中添加mapper扫描的包

  1. @SpringBootApplication
  2. @MapperScan("com.springboot.mybatisplus.mapper")
  3. public class App {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run(App.class,args);
  7. }
  8. }

5、调用mapper接口即可

  1. @RestController
  2. @RequestMapping("/mybatisplus")
  3. public class TestMain {
  4.  
  5. @Autowired
  6. private StudentMapper studentMapper;
  7.  
  8. @GetMapping("/list")
  9. public List<Student> list(){
  10. List<Student> students = studentMapper.selectList(null);
  11. return students;
  12. }
  13.  
  14.  
  15. @GetMapping("/save")
  16. public String save(){
  17. Student student = new Student();
  18. student.setId(2);
  19. student.setCity("杭州");
  20. student.setName("马云");
  21. student.setSchool("杭州师范");
  22. studentMapper.insert(student);
  23. return "success";
  24. }
  25.  
  26. }

打印sql:

  1. mybatis-plus:
  2. configuration:
  3. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

具体的API请参照mybatis-plus官网:http://mp.baomidou.com/guide/

上一篇:spring PROPAGATION


下一篇:SQL排序