Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
那么它是怎么增强的呢?其实就是它已经封装好了单表crud方法,我们不需要再定义方法了,直接调用这些方法就行
环境搭建
1.pom添加myBatis-plus
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
2.UserMapper去继承myBatis-plus提供的父类
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
3.实体和表做局部映射:
/*
* 默认实体和表名一样 主键属性名和主键字段名一样 其它属性名和字段名一致:啥都不需要加注解
* 1:实体名和表名不一致 局部设置@TableName(value = "映射的表名") 也可以全局设置(局部>全局)
* 2:主键属性名和主键字段名不一致 @TableId(value = "主键字段名",type=IdType.AUTO(局部、全局))
* 3:其它属性名和其它字段名不一致 @TableField(value = "字段名")
* 4:如果当前实体中有属性不参与封装映射 可以使用注解排除 @TableField(exist =false)
*
* */
@Data
@TableName("tab_user")
public class User implements Serializable {
@TableId(value = "id",type = IdType.AUTO)
private Integer ids;
private String userName; //数据库字段是_ 对应实体属性是驼峰
@TableField("password")
private String passwords;
private String sex;
private String address;
@TableField(exist = false) //不参数数据库的映射
private String info;
}
或者。yml做全局配置
mybatis-plus:
global-config:
db-config:
table-prefix: tb_ #全局匹配表映射
id-type: auto #全局设置主键自增
configuration: #打印sql日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatisplus的CRUD操作
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MPTest {
@Autowired
private UserMapper userMapper;
// 查询所有
@Test
public void test01() throws Exception {
List<User> list = userMapper.selectList(null);
list.forEach(user -> {
System.out.println(user);
});
}
// 根据id查询
@Test
public void test02() throws Exception {
User user = userMapper.selectById(5);
System.out.println(user);
}
// 新增
@Test
public void test03() throws Exception {
User user = new User();
user.setUserName("heihei");
user.setPassword("666666");
userMapper.insert(user);
}
// 修改
@Test
public void test04() throws Exception {
User user = new User();
user.setId(31);
user.setUserName("hehe");
user.setPassword("888888");
userMapper.updateById(user);
}
// 删除
@Test
public void test05() throws Exception {
//删除单条
userMapper.deleteById(32);
//删除多条
List<Integer> list=new ArrayList<>();
list.add(28);
list.add(29);
list.add(31);
userMapper.deleteBatchIds(list);
}
}
分页查询
1.启动类上配置mybatisplus的分页插件
@SpringBootApplication //启动类
public class SpringBootRun {
public static void main(String[] args) {
SpringApplication.run(SpringBootRun.class,args);
}
@Bean //mybatisplus分页插件
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
2.实现分页查询
@Test
public void test4(){
int currentPage=1;
int pageSize=3;
IPage<User> page = new Page<>(currentPage,pageSize);
page=userMapper.selectPage(page,null);
List<User> records = page.getRecords();
long pages = page.getPages();
long total = page.getTotal();
System.out.println("总页数:"+pages);
System.out.println("总条数:"+total);
for (User user : records) {
System.out.println(user);
}
}
带条件的CRUD
示例:
// 全查询
@Test
public void test1(){
QueryWrapper<User> qw = new QueryWrapper<>();
List<User> list = userMapper.selectList(qw);
for (User user : list) {
System.out.println(user);
}
}
//多条件查询 (and关系)
@Test
public void test2(){
QueryWrapper<User> qw = new QueryWrapper<>();
qw.eq("sex","男")
.gt("id",2)
.in("address","天津","北京");
List<User> list = userMapper.selectList(qw);
for (User user : list) {
System.out.println(user);
}
}
//排序查询
@Test
public void test5(){
QueryWrapper<User> qw = new QueryWrapper<>();
qw.eq("sex","男").or()
.gt("id",2).or()
.in("address","天津","北京")
.orderByDesc("id");
List<User> list = userMapper.selectList(qw);
for (User user : list) {
System.out.println(user);
}
}
//select:指定要查询的字段返回
@Test
public void test6(){
QueryWrapper<User> qw = new QueryWrapper<>();
qw.eq("sex","男").or()
.gt("id",2).or()
.in("address","天津","北京")
.orderByDesc("id")
.select("user_name","sex");
List<User> list = userMapper.selectList(qw);
for (User user : list) {
System.out.println(user);
}
}
//分页+条件查询
@Test
public void test11(){
int currentPage=1;
int pageSize=3;
IPage<User> page = new Page<>(currentPage,pageSize);
QueryWrapper<User> qw = new QueryWrapper<>();
qw.like("user_name","b");
page=userMapper.selectPage(page,qw);
List<User> records = page.getRecords();
long pages = page.getPages();
long total = page.getTotal();
System.out.println("总页数:"+pages);
System.out.println("总条数:"+total);
for (User user : records) {
System.out.println(user);
}
}
mybatisplus提供的api:
1 可以根据id做查询( selectById(id) ) 也可以根据各种条件做查询(selectList(qw) / selectOne(qw) )
2 可以根据id做删除(deleteById(id) /deleteBatchIds(id集合) ) 也可以根据条件做删除(delete(qw) )
3 可以根据id做修改(updateById(对象实体) ) 也可以根据条件做修改( update(对象实体,uw) )
4 分页查询可做可不做条件 selectPage(page,qw);