添加依赖
<!--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
,
insert intouser
(id
,name
,age
,
insert intouser
(id
,name
,age
,
insert intouser
(id
,name
,age
,
insert intouser
(id
,name
,age
,
配置 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<User> 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<User> wrapper = new QueryWrapper<>(); wrapper.like("name","o"); //查询 name字段中包含‘o’的数据 List<User> users = this.userMapper.selectList(wrapper);//返回值是一个list集合 for (User u : users) { System.out.println(u); } } @Test public void testSelectByLe(){ QueryWrapper<User> wrapper = new QueryWrapper<>(); /** * eq : 等于 * ne : 不等于 * gt : 大于 * ge : 大于等于 * lt : 小于 * le : 小于等于 * 更多查看 https://mp.baomidou.com/guide/wrapper.html#abstractwrapper */ wrapper.le("age","20"); //设置年龄小于等于20 List<User> 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)