添加依赖

    <!--mybatis-plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>

创建数据库和表

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

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

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

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

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

配置 application.yml

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

创建pojo类

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

创建 UserMapper接口

public interface UserMapper extends BaseMapper<User> {
}

注意:BaseMapper接口引入的包是

com.baomidou.mybatisplus.core.mapper.BaseMapper

编写 SpringBoot 启动类

@MapperScan("cn.mybatisplus.mapper")
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}

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

编写测试代码

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTest {
@Autowired
private UserMapper userMapper; @Test
public void testList(){
List&lt;User&gt; users = this.userMapper.selectList(null);
for (User user: users) {
System.out.println(user);
}
} @Test
public void testSelectById(){
User user = this.userMapper.selectById(3L); //根据id查数据
System.out.println(user);
} @Test
public void testSelectByLike(){
QueryWrapper&lt;User&gt; wrapper = new QueryWrapper&lt;&gt;();
wrapper.like(&quot;name&quot;,&quot;o&quot;); //查询 name字段中包含‘o’的数据
List&lt;User&gt; users = this.userMapper.selectList(wrapper);//返回值是一个list集合
for (User u : users) {
System.out.println(u);
}
} @Test
public void testSelectByLe(){
QueryWrapper&lt;User&gt; wrapper = new QueryWrapper&lt;&gt;();
/**
* eq : 等于
* ne : 不等于
* gt : 大于
* ge : 大于等于
* lt : 小于
* le : 小于等于
* 更多查看 https://mp.baomidou.com/guide/wrapper.html#abstractwrapper
*/
wrapper.le(&quot;age&quot;,&quot;20&quot;); //设置年龄小于等于20
List&lt;User&gt; users = this.userMapper.selectList(wrapper);
for (User u : users) {
System.out.println(u);
}
}
@Test
public void testSave() {
User user = new User();
user.setAge(25);
user.setEmail("zhangsan@qq.com");
user.setName("zhangsan");
int count = this.userMapper.insert(user);
System.out.println("新增数据成功 count ==> "+count);
}
@Test
public void testDelete(){
this.userMapper.deleteById(6L);
System.out.println("删除成功");
} @Test
public void testUpdate(){
User user = new User();
user.setId(5L);
user.setName("Lisi");
int i = this.userMapper.updateById(user);
System.out.println(" 有"+i+"条数据被修改");
}

}

分页查询

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

@MapperScan("cn.itcast.mybatisplus.mapper")
@SpringBootApplication
public class MyApplication {
/*
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
} public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}

}

测试

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

运行结果

总页数 ----> 5
当前页数 ----> 2
当前每页显示数 ----> 2
User(id=3, name=Tom, age=28, email=test3@qq.com)
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. springboot 集成oss

    集成aliyun oss 结构如下: pom.xml <dependency> <groupId>org.springframework.boot</groupId> ...

  2. 在国外搭建 Web 服务器 - Linode VPS

    在国外搭建 Web 服务器 - Linode VPS 买一台虚拟服务器(VPS),把你网站放在上面跑跑,找找感觉,平时也可以用它来练习.前几天,搜索到了有人推荐 Linode 的 VPS,昨天又有朋友 ...

  3. 在浏览器下载pdf,或者txt文档是会直接打开

    window.location.href = url会直接打开,解释大概是因为浏览器自身可以解析.pdf或者txt.解决方法如下: 本来就要用a标签里面加上download属性的,结果发现不行,就算了 ...

  4. 【leetcode】1109. Corporate Flight Bookings

    题目如下: There are n flights, and they are labeled from 1 to n. We have a list of flight bookings.  The ...

  5. Java——static

    [static] <1>static成员变量存储在内存data segment区域,不是存放在堆中. <2>静态成员变量属于整个类,任何一个对象都可以访问这个值:如果没有对象, ...

  6. LCA【Tarjan】

    首先,我们先来了解LCA. LCA 是树上两个点最近的公共祖先. 比如说,在如图的树中,3与4的公共祖先有“2”,“1”,但最近的祖先是“2”. 显然,暴力可以做O(n),但是我们希望更快. 现在,有 ...

  7. 解决报错(Could not create connection to database server.)

    org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory 尝试多种方法后发现是由于mysq ...

  8. Laya 首日红点逻辑

    Laya 首日红点逻辑 @author ixenos 2019-08-26 10:50:27 1.原理:显然,首日红点意味着包含进程销毁的情况,那么就要持久化存储信息,这里我们使用LocalStora ...

  9. php curl文件上传

    <?php /** * 这是一个自动化部署的类, 非常简单,思想就是压缩,上传,然后解压覆盖,所以请小心使用. * @author liuchao <249757247@qq.com> ...

  10. MapServer教程3

    Compiling on Unix Compiling on Win32 PHP MapScript Installation .NET MapScript Compilation IIS Setup ...