添加依赖

  1. <!--mybatis-plus的springboot支持-->
  2. <dependency>
  3. <groupId>com.baomidou</groupId>
  4. <artifactId>mybatis-plus-boot-starter</artifactId>
  5. <version>3.0.5</version>
  6. </dependency>
  7. <!--mysql驱动-->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <version>5.1.47</version>
  12. </dependency>

创建数据库和表

  1. CREATE TABLE `user` (`id` BIGINT(20) not null AUTO_INCREMENT comment '主键ID',
  2. `name` VARCHAR(30) DEFAULT null comment '姓名',
  3. `age` int(11) DEFAULT null comment '年龄',
  4. `email` VARCHAR(50) DEFAULT null comment '邮箱',
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=INNODB DEFAULT CHARSET=utf8;
  7. insert into user (id,name,age,email) VALUES ('1','Jone','18','test1@qq.com');

  8. insert into user (id,name,age,email) VALUES ('2','Jack','20','test2@qq.com');

  9. insert into user (id,name,age,email) VALUES ('3','Tom','28','test3@qq.com');

  10. insert into user (id,name,age,email) VALUES ('4','Sandy','21','test4@qq.com');

  11. insert into user (id,name,age,email) VALUES ('5','Billie','24','test5@qq.com');

配置 application.yml

  1. spring:
  2. application:
  3. name: spring-boot-mybatis-plus
  4. datasource:
  5. driver-class-name: com.mysql.jdbc.Driver
  6. url: jdbc:mysql://127.0.0.1:3306/mysqlName?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
  7. username: root
  8. password: root

创建pojo类

  1. public class User {
  2. @TableId(value = "ID",type = IdType.AUTO) //设置id为自增长
  3. private Long id;
  4. private String name;
  5. private Integer age;
  6. private String email;
  7. //( set,get,toString方法自行添加 )
  8. }

创建 UserMapper接口

  1. public interface UserMapper extends BaseMapper<User> {
  2. }

注意:BaseMapper接口引入的包是

com.baomidou.mybatisplus.core.mapper.BaseMapper

编写 SpringBoot 启动类

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

@MapperScan("cn.mybatisplus.mapper"). 扫描 mapper 包

编写测试代码

  1. @SpringBootTest
  2. @RunWith(SpringRunner.class)
  3. public class UserMapperTest {
  4. @Autowired
  5. private UserMapper userMapper;
  6. @Test
  7. public void testList(){
  8.     List&lt;User&gt; users = this.userMapper.selectList(null);
  9.     for (User user: users) {
  10.         System.out.println(user);
  11.     }
  12. }
  13.   @Test
  14. public void testSelectById(){
  15.     User user = this.userMapper.selectById(3L);   //根据id查数据
  16.     System.out.println(user);
  17. }
  18. @Test
  19. public void testSelectByLike(){
  20.     QueryWrapper&lt;User&gt; wrapper = new QueryWrapper&lt;&gt;();
  21.     wrapper.like(&quot;name&quot;,&quot;o&quot;);   //查询 name字段中包含‘o’的数据
  22.     List&lt;User&gt; users = this.userMapper.selectList(wrapper);//返回值是一个list集合
  23.     for (User u : users) {
  24.         System.out.println(u);
  25.     }
  26. }
  27. @Test
  28. public void testSelectByLe(){
  29.     QueryWrapper&lt;User&gt; wrapper = new QueryWrapper&lt;&gt;();
  30.     /**
  31.      * eq : 等于
  32.      * ne : 不等于
  33.      * gt : 大于
  34.      * ge : 大于等于
  35.      * lt : 小于
  36.      * le : 小于等于
  37.      * 更多查看 https://mp.baomidou.com/guide/wrapper.html#abstractwrapper
  38.      */
  39.     wrapper.le(&quot;age&quot;,&quot;20&quot;);       //设置年龄小于等于20
  40.     List&lt;User&gt; users = this.userMapper.selectList(wrapper);
  41.     for (User u : users) {
  42.         System.out.println(u);
  43.     }
  44. }
  45. @Test
  46. public void testSave() {
  47.     User user = new User();
  48.     user.setAge(25);
  49.     user.setEmail("zhangsan@qq.com");
  50.     user.setName("zhangsan");
  51.     int count = this.userMapper.insert(user);
  52.     System.out.println("新增数据成功  count ==> "+count);
  53. }
  54. @Test
  55. public void testDelete(){
  56.     this.userMapper.deleteById(6L);
  57.     System.out.println("删除成功");
  58. }
  59. @Test
  60. public void testUpdate(){
  61.     User user = new User();
  62.     user.setId(5L);
  63.     user.setName("Lisi");
  64.     int i = this.userMapper.updateById(user);
  65.     System.out.println(" 有"+i+"条数据被修改");
  66. }
  67. }

分页查询

需要增加分页插件配置(这里就简单在启动类加 Bean)

  1. @MapperScan("cn.itcast.mybatisplus.mapper")
  2. @SpringBootApplication
  3. public class MyApplication {
  4. /*
  5.  * 分页插件
  6.  */
  7. @Bean
  8. public PaginationInterceptor paginationInterceptor(){
  9.     return new PaginationInterceptor();
  10. }
  11. public static void main(String[] args) {
  12.     SpringApplication.run(MyApplication.class,args);
  13. }
  14. }

测试

  1. @Test
  2. public void testSelectPage() {
  3. Page<User> page = new Page<>(2, 2);
  4. IPage<User> userIPage = this.userMapper.selectPage(page, null);
  5. System.out.println("总页数 ----> " + userIPage.getTotal());
  6. System.out.println("当前页数 ----> " + userIPage.getCurrent());
  7. System.out.println("当前每页显示数 ----> " + userIPage.getSize());
  8. List<User> records = userIPage.getRecords();
  9. for (User u: records) {
  10. System.out.println(u);
  11. }
  12. }

运行结果

  1. 总页数 ----> 5
  2. 当前页数 ----> 2
  3. 当前每页显示数 ----> 2
  4. User(id=3, name=Tom, age=28, email=test3@qq.com)
  5. User(id=4, name=Sandy, age=21, email=test4@qq.com)

SpringBoot 整合 MybatisPlus (项目的创建及简单的单表查询)的更多相关文章

  1. 实践丨SpringBoot整合Mybatis-Plus项目存在Mapper时报错

    摘要:在SpringBoot运行测试Mybatis-Plus测试的时候报错的问题分析与修复 本文分享自华为云社区<SpringBoot整合MybatisPlus项目存在Mapper时运行报错的问 ...

  2. SpringBoot整合MyBatisPlus配置动态数据源

    目录 SpringBoot整合MyBatisPlus配置动态数据源 SpringBoot整合MyBatisPlus配置动态数据源 推文:2018开源中国最受欢迎的中国软件MyBatis-Plus My ...

  3. 5、SpringBoot整合之SpringBoot整合MybatisPlus

    SpringBoot整合MybatisPlus 目录(可点击直接跳转,但还是建议按照顺序观看,四部分具有一定的关联性): 实现基础的增删改查 实现自动填充功能 实现逻辑删除 实现分页 首先给出四部分完 ...

  4. SpringBoot 整合 MyBatis-Plus 入门体验

    一.前言 本文小编将基于 SpringBoot 整合 MyBatis-Plus , MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上做增强并且不改变原本功能 ...

  5. SpringBoot整合Mybatis-Plus

    这篇文章介绍一个SpringBoot整合Mybatis-Plus,提供一个小的Demo供大家参考. 已经很久没有写文章了,最近家里有点事刚刚处理完,顺便也趁机休息了一段时间.刚回到公司看了一下码云,发 ...

  6. SpringBoot整合Mybatis-plus实现增删查改

    今天给大家分享一下SpringBoot整合Mybatis-plus的增删查改案例. pom.xml <?xml version="1.0" encoding="UT ...

  7. springBoot 整合 mybatis 项目实战

    二.springBoot 整合 mybatis 项目实战   前言 上一篇文章开始了我们的springboot序篇,我们配置了mysql数据库,但是我们sql语句直接写在controller中并且使用 ...

  8. Springboot 整合 MyBatisPlus[详细过程]

    Springboot 整合 MyBatisPlus[详细过程] 提要 这里已经将Springboot环境创建好 这里只是整合MyBatis过程 引入Maven依赖 添加MyBatisPlus启动依赖, ...

  9. springboot 整合 web 项目找不到 jsp 文件

    今天遇到一个问题,就是使用springboot整合web项目的时候,怎么都访问不到 \webapp\WEB-INF\jsp\index.jsp 页面.这个问题搞了半天,试了各种方式.最后是因为在启动的 ...

随机推荐

  1. php iconv函数转换出错问题

    本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...

  2. 日记(OI 无关,文化课无关)

    2019.11.13 今天在研究 wss 的代码为什么比我快那么多. 看见他定义了一个结构体叫 thxorz,一定是因为 orz 了 thx 得到了信仰加成了. 然后刚说完这句话就看见 thx 走了进 ...

  3. 【04】Python 深拷贝浅拷贝 函数 递归 集合

    1 深拷贝浅拷贝 1.1 a==b与a is b的区别 a == b    比较两个对象的内容是否相等(可以是不同内存空间) a is b  比较a与b是否指向同一个内存地址,也就是a与b的id是否相 ...

  4. Linux发行版和内核版本

    1./etc/issue 和 /etc/redhat-release都是系统安装时默认的发行版本信息,通常安装好系统后文件内容不会发生变化. 2.lsb_release -a :FSG(Free St ...

  5. poj 3352 : Road Construction 【ebcc】

    题目链接 题意:给出一个连通图,求最少加入多少条边可使图变成一个 边-双连通分量 模板题,熟悉一下边连通分量的定义.最后ans=(leaf+1)/2.leaf为原图中size为1的边-双连通分量 #i ...

  6. React native 平时积累笔记

    常用插件: react-native-check-box 复选框react-native-sortable-listview 列表拖拽排序 react-native-doc-viewer 预览组件 r ...

  7. CF543B Destroying Roads 枚举 + 思维 + BFS

    Code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s".in", ...

  8. [洛谷2257]YY的GCD 题解

    整理题目转化为数学语言 题目要我们求: \[\sum_{i=1}^n\sum_{i=1}^m[gcd(i,j)=p]\] 其中 \[p\in\text{质数集合}\] 这样表示显然不是很好,所以我们需 ...

  9. cf 118B

    B. Present from Lena time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. Layer Cake cf

    Layer Cake time limit per test 6 seconds memory limit per test 512 megabytes input standard input ou ...