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接口即可

项目架构:

具体步骤代码:

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/

SpringBoot 整合 Mybatis-Plus + Mysql的更多相关文章

  1. springboot整合mybatis连接mysql数据库出现SQLException异常

    在springboot整合mybatis连接数据库的时候,项目中遇到一个SQLException,我检查了properties配置文件,看数据源有没有配错,检查有没有打错字,在数据库中把sql语句查询 ...

  2. SpringBoot 整合 Mybatis 和 Mysql (详细版)

    结构如下 1.引入相关依赖 <!--mysql--><dependency> <groupId>mysql</groupId> <artifact ...

  3. kotlin + springboot整合mybatis操作mysql数据库及单元测试

    项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller ...

  4. SpringBoot 整合 Mybatis + Mysql——XML配置方式

    一.介绍 SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式. SpringBoot整合Mybatis也有两 ...

  5. 学习springboot整合mybatis并编写测试类

    报名立减200元.暑假直降6888. 邀请链接:http://www.jnshu.com/login/1/20535344 邀请码:20535344 遇到的问题: 1.原因是在启动类上只有一个@Map ...

  6. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  7. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  8. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  9. springboot整合mybatis出现的一些问题

    springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...

  10. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

随机推荐

  1. CF280D-k-Maximum Subsequence Sum【模拟费用流,线段树】

    正题 题目链接:https://www.luogu.com.cn/problem/CF280D 题目大意 一个长度为\(n\)的序列,\(m\)次操作 修改一个数 询问一个区间中选出\(k\)段不交子 ...

  2. CSharp委托与匿名函数

    CSharp委托与匿名函数 场景 面对事件处理,我们通常会通过定义某一个通用接口,在该接口中定义方法,然后在框架代码中,调用实现该接口的类实例的方法来实现函数的回调.可能这样来说有些抽象,那我们提供一 ...

  3. The Data Way Vol.4|开源是创造软件诸多方法中最好的一种形式

    关于「The Data Way」 「The Data Way」是由 SphereEx 公司出品的一档播客节目.这里有开源.数据.技术的故事,同时我们关注开发者的工作日常,也讨论开发者的生活日常:我们聚 ...

  4. 运用shapefile.js解析Shp文件

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  5. Java基础之(四):使用IDEA实现HelloWorld

    使用IDEA实现HelloWorld 在使用IDEA这个集成开发环境写Java程序之前,我们要先对IDEA进行一些基本的配置,以便我们能够更好地使用它. 新建一个空项目,用来学习基础语法 设置项目名称 ...

  6. LOJ6356 四色灯(容斥+dp

    纪念第一次所有的解析全写在代码里面 QWQ 这里就简单说几句了 首先一个灯有贡献,当且仅当他被按了\(4k\)次. 那么我们定义\(f(S)\)表示\([1,n]\)中有多少个数\(x\)是集合\(S ...

  7. 【UE4】GamePlay架构

    新标签打开或者下载看大图 更新: 增加 编程子系统 Subsystem 思维导图 Character pipeline

  8. djago后台管理页面

    from django.contrib import admin from blogtest.models import * #修改网页title和站点header.+ admin.site.site ...

  9. UltraSoft - Alpha - 测试报告

    遇到的bug bug:在vue.config.js里配置proxy,并修改请求的url后仍无法连接到后端. 解决: url最后忘了'/',导致和后端不匹配,会有404.500等错误. 后端服务未打开或 ...

  10. Gitflow branch与Docker image tag命名冲突怎么办?

    谷歌还是比必应要好用一点. 在前公司,我根据主流的git flow 给团队搭建了一套devops流程,运行在 docker & k8s上. 在现代devops流程中,一般推荐使用git分支名或 ...