Mybatis-Plus(二)--Mybatis-Plus方法大全

通用CRUD大全(Mybatis-Plus为我们提供了哪些操作)


还有在mybatis中遇到列名和属性名不一致等等的情况,在mybatis中xml中声明解决,在mybatis-plus中也都有对应的解决。

1.插入操作

//插入一条记录

//参数entity是实体对象

int insert(T entity)

【1】设置id生成策略
数据写入数据库,但是id并不是我们希望的自增长,而是随机生成了id的值写入到了数据库。

我们就要设置id生成策略来解决这个问题。

MP支持的id策略:

java 复制代码
package com.baomidou.mybatisplus.annotation;
//生成ID类型的枚举类
public enum IdType {
    //数据库id自增
    AUTO(0),
    //该类型为未设置为主键类型
    NONE(1),
    //用户输入ID,该类型可以通过自己注册自动填充插件来进行填充
    INPUT(2),
    //以下两种类型,只有当插入对象ID为空,才自动填充,全局唯一ID
    //其实我也不知道什么意思
    ASSIGN_ID(3),
    ASSIGN_UUID(4);


    private final int key;
    private IdType(int key) {
        this.key = key;
    }
    public int getKey() {
        return this.key;
    }
}

用法是在实体类的对象id属性上添加注解:

java 复制代码
@TableId(type = IdType.AUTO)
private Long id;

【2】@TableField

(1)对象中的属性名和字段名不一致的问题(非驼峰)

java 复制代码
@TableField(value = "email") //解决数据库中字段名与属性不一致
private String mail;

(2)对象中的属性字段在表中不存在的问题

java 复制代码
@TableField(exist = false)
private String address; //在数据库表中是不存在的

(3)字段不加入查询字段

java 复制代码
@TableField(exist = false)
private String address; //在数据库表中是不存在的

2.更新操作

在Mybatis-Plus中更新操作有2种,一种是根据id更新,另一种是根据条件更新。

【1】根据id更新

定义:

//根据ID修改

//@param entity 实体对象

int updateById(T entity);

使用:

java 复制代码
//根据id更新,更新不为null的字段
userMapper.updateById(user);

【2】根据条件更新

定义:

//根据whereEntity条件,更新记录

//@param entity 实体对象(set条件值,可以为null)

//@param updatewrapper 实体对象封装操作类(可以为null,里面的entity用于生成where语句)

int update(T entity,Wrapper<T> updateWrapper);

使用:

例1:

java 复制代码
User user=new User();
user.setAge(22);//更新的字段

//更新的条件
QueryWrapper<User> wrapper=new QueryWrapper<>();
wrapper.eq("id",6);

//执行更新操作
int result=this.userMapper.update(user,wrapper);
System.out.println("result = "+result);

例2:

java 复制代码
//更新的条件及字段
updateWrapper<User> wrapper=new UpdateWrapper<>();
wrapper.eq("id",6).set("age",23);

//执行更新操作
int result=this.userMapper.update(null,wrapper);
System.out.println("result = "+result);

3.删除操作

1.根据id删除

【1】deleteById

定义:int deleteById(Serializable id);

java 复制代码
int result=this.userMapper.deleteById(6L);//Long类型的6
System.out.println("result = "+result);

【2】deleteBatchIds

定义:int deleteBatchIds(Collection<? extends Serializable> idList);

java 复制代码
//根据id集合批量删除
int result=this.userMapper.deleteBatchIds(Arrays.asList(1L,10L,20L));

2.根据条件删除

【1】deleteByMap

定义:int deleteByMap(Map<String,Object> columnMap);

java 复制代码
Map<String,Object> columnMap=new HashMap<>();
columnMap.put("age",20);
columnMap.put("name","张三");

//将columnMap中的元素设置为删除的条件,多个之间为and关系
int result=this.userMapper.deleteByMap(columnMap);
System.out.println("result = "+result);

【2】delete

定义:int delete(Wrapper<T> wrapper);

java 复制代码
User user=new User();
user.setAge(20);
user.setName("张三");
//将实体对象进行包装,包装为操作条件
QueryWrapper<User> wrapper =new QueryWrapper<>(User);

int result=this.userMapper.delete(wrapper);

4.查询操作

1.根据id查询单条数据

定义:T selectById(Serializable id);

java 复制代码
//根据id查询数据
User user=this.userMapper.selectById(2L);

2.根据id集合批量查询

定义://根据ID批量查询

List<T> selectBatchIds(Collection<? extends Serializable> idList);

java 复制代码
//根据id集合批量查询
List<User> users=this.userMapper.selectBatchIds(Arrays.asList(2L,3L,10L));
for(User user:users){
    System.out.println(user);
}

3.根据条件查询单条数据

定义:T selectOne(wrapper<T> queryWrapper);

java 复制代码
QueryWrapper<User> wrapper =new QueryWrapper<User>();
wrapper.eq("name","李四");
//根据条件查询一条数据,如果结果超过一条会报错
User user = this.userMapper.selectOne(wrapper);

4.根据条件查询多条数据

定义:List<T> selectList(wrapper<T> queryWrapper);

java 复制代码
QueryWrapper<User> wrapper=new QueryWrapper<User>();
wrapper.gt("age",23);//年龄大于23岁
//根据条件查询数据
List<User> users=this.userMapper.selectList(wrapper);

5.根据条件查询总记录数

定义:Integer selectCount(Wrapper<T> queryWrapper);

java 复制代码
QueryWrapper<User> wrapper=new QueryWrapper<User>();
wrapper.gt("age",23);//年龄大于23岁
//根据条件查询数据条数
Integer count=this.userMapper.selectCount(wrapper);

6.分页查询

定义:IPage<T> selectPage(IPage<T> page,Wrapper<T> queryWrapper);

例:

配置分页插件:

java 复制代码
@Configuration
@MapperScan("cn.itcast.mp.mapper")
public class MybatisPlusConfig {
    //分页插件
    @Bean
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        return new PaginationInnerInterceptor();
    }
}

测试:

java 复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestMybatisSpringBoot {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void testSelectPage(){
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.gt("age",20);
        Page<User> page=new Page<>(1,1);
        //根据条件查询数据
        IPage<User> iPage=this.userMapper.selectPage(page,wrapper);

        System.out.println("数据总条数:"+iPage.getTotal());
        System.out.println("总页数:"+iPage.getPages());
        List<User> users=iPage.getRecords();
        for(User user:users){
            System.out.println("user = "+user);
        }
    }
}
相关推荐
讓丄帝愛伱9 分钟前
JPA QuerySyntaxException: unexpected token: xxx near
springboot
行走的山峰15 分钟前
etcd三节点,其中一个坏掉了的恢复办法
数据库·etcd
一只特立独行的猪6111 小时前
Java面试——集合篇
java·开发语言·面试
讓丄帝愛伱2 小时前
spring boot启动报错:so that it conforms to the canonical names requirements
java·spring boot·后端
weixin_586062022 小时前
Spring Boot 入门指南
java·spring boot·后端
shelby_loo3 小时前
通过 Docker 部署 MySQL 服务器
服务器·mysql·docker
ImomoTo3 小时前
HarmonyOS学习(十三)——数据管理(二) 关系型数据库
数据库·学习·harmonyos·arkts·鸿蒙
Dola_Pan5 小时前
Linux文件IO(二)-文件操作使用详解
java·linux·服务器
wang_book5 小时前
Gitlab学习(007 gitlab项目操作)
java·运维·git·学习·spring·gitlab
机器视觉知识推荐、就业指导6 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt