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接口即可
项目架构:
具体步骤代码:
1、在pom.xml加入相关配置
- <!-- spring-boot-starter-parent 和 mybatis-plus-boot-starter 有版本要求-->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.3.RELEASE</version>
- </parent>
- <dependencies>
- <!-- spring-boot -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!--mybatis-plus自动的维护了mybatis以及mybatis-spring的依赖,
- 在springboot中这三者不能同时的出现,避免版本的冲突,表示:跳进过这个坑-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.0.1</version>
- </dependency>
- <!-- 引入Druid依赖,阿里巴巴所提供的数据源 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.29</version>
- </dependency>
- <!-- 提供mysql驱动 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.38</version>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.16.16</version>
- </dependency>
- </dependencies>
2、在resources中添加application.yml,设置mysql相关配置
- server:
- port: 2525
- # 该配置的名称是固定的,不可以改变,否则将不能自动加载到数据源中
- spring:
- datasource:
- # 使用druid数据源
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.jdbc.Driver
- url: jdbc:mysql://192.168.3.172:3306/poc?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
- username: devuser
- password: dev123
- filters: stat
- maxActive: 20
- initialSize: 1
- maxWait: 60000
- minIdle: 1
- timeBetweenEvictionRunsMillis: 60000
- minEvictableIdleTimeMillis: 300000
- validationQuery: select 1 FROM DUAL
- testWhileIdle: true
- testOnBorrow: false
- testOnReturn: false
- poolPreparedStatements: true
- maxOpenPreparedStatements: 20
3、在基类中添加表名和表名中对应的字段名
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- @TableName("tb_test_student") //对应表名
- public class Student implements Serializable {
- //对应id,可不填
- @TableId(value = "id")
- private int id;
- //对应字段名,如果属性名和字段名一致,可不填
- @TableField("name")
- private String name;
- private String school;
- private String city;
- //表示表中没有这个字段,如果不加该注释,会抛异常
- @TableField(exist = false)
- private String address;
- }
4、定义mapper接口,继承BaseMapper<T>接口,该接口中封装了Sql
- public interface StudentMapper extends BaseMapper<Student> {
- }
5、在启动类中添加mapper扫描的包
- @SpringBootApplication
- @MapperScan("com.springboot.mybatisplus.mapper")
- public class App {
- public static void main(String[] args) {
- SpringApplication.run(App.class,args);
- }
- }
5、调用mapper接口即可
- @RestController
- @RequestMapping("/mybatisplus")
- public class TestMain {
- @Autowired
- private StudentMapper studentMapper;
- @GetMapping("/list")
- public List<Student> list(){
- List<Student> students = studentMapper.selectList(null);
- return students;
- }
- @GetMapping("/save")
- public String save(){
- Student student = new Student();
- student.setId(2);
- student.setCity("杭州");
- student.setName("马云");
- student.setSchool("杭州师范");
- studentMapper.insert(student);
- return "success";
- }
- }
打印sql:
- mybatis-plus:
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
具体的API请参照mybatis-plus官网:http://mp.baomidou.com/guide/
SpringBoot 整合 Mybatis-Plus + Mysql的更多相关文章
- springboot整合mybatis连接mysql数据库出现SQLException异常
在springboot整合mybatis连接数据库的时候,项目中遇到一个SQLException,我检查了properties配置文件,看数据源有没有配错,检查有没有打错字,在数据库中把sql语句查询 ...
- SpringBoot 整合 Mybatis 和 Mysql (详细版)
结构如下 1.引入相关依赖 <!--mysql--><dependency> <groupId>mysql</groupId> <artifact ...
- kotlin + springboot整合mybatis操作mysql数据库及单元测试
项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller ...
- SpringBoot 整合 Mybatis + Mysql——XML配置方式
一.介绍 SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式. SpringBoot整合Mybatis也有两 ...
- 学习springboot整合mybatis并编写测试类
报名立减200元.暑假直降6888. 邀请链接:http://www.jnshu.com/login/1/20535344 邀请码:20535344 遇到的问题: 1.原因是在启动类上只有一个@Map ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- springboot整合mybatis出现的一些问题
springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...
- springBoot整合mybatis、jsp 或 HTML
springBoot整合mybatis.jsp Spring Boot的主要优点: 1: 为所有Spring开发者更快的入门: 2: 开箱即用,提供各种默认配置来简化项目配置: 3: 内嵌式容器 ...
随机推荐
- R7000 电脑调整亮度
R7000 电脑亮度太亮,想调整亮度,fn+F5,F6 不起作用,需要调整显卡的设置
- HDFS 10 - HDFS 的联邦机制(Federation 机制)
目录 1 - 为什么需要联邦 2 - Federation 架构设计 3 HDFS Federation 的不足 版权声明 1 - 为什么需要联邦 单 NameNode 的架构存在的问题:当集群中数据 ...
- aizhan查询旁IP网站脚本
<?php print_r("-------------------------\r\n"); print_r("-------爱站旁站查询------\r\n&q ...
- 踩坑系列《六》Spring增加事务处理遇到异常解决办法
当在对数据进行增删改操作时,需要用到事务的处理,所以在业务层中加入@Transactional注解,但是在执行业务操作的前面遇到异常,因此对异常进行抛出,但是数据又诡异地成功保存到数据库了. 解决方法 ...
- mysql8.0.20下载安装教程
mysql8.0.20安装教程 1.浏览器搜索mysql下载安装 地址:https://dev.mysql.com/downloads/mysql/ 2.登录或者不登录下载 3.下载的是一个压缩包,直 ...
- 实时获取股票数据,免费!——Python爬虫Sina Stock实战
更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. 实时股票数据的重要性 对于四大可交易资产:股票.期货.期权.数字货币来说,期货.期权.数字货币,可以从交 ...
- 单体应用 适合采用 dapr 构建吗?
缘起今天在微信群里有同学问 "纯.net 项目,有必要上dapr吗?" 当时不假思索的说不是微服务没必要,其他群友也说没必要.下午细想了一下,觉得这个和微服务没有关系,如果我的应用 ...
- 8086的复位与启动 CPU执行指令的步骤
东北大学-计算机硬件技术基础 CPU执行指令的步骤 取指令 Fetch 指令译码 Decode 执行指令 Execute 回写 Write-back 修改指令指针 取指令 将CS和IP的内容通过地址加 ...
- pg_basebackup报错: pg_basebackup: incompatible server version 12.4
pg_basebackup报错 今日从库复制主库data时,发现pg_basebackup无法使用,详情如下: 错误为:incompatible server version 12.4 [postgr ...
- python的函数参数传递方式
python的一切数据类型都是对象.但是python的对象分为不可变对象和可变对象.python的变量是引用,对python变量的赋值是引用去绑定该对象. 可变对象的数据发生改变,例如列表和字典,引用 ...