首页 专题 文章 代码 归档
MybatisPlus通用CRUD
2020.02.10 12:33 2020.02.10 12:33

1. 插入操作

自增长ID设置:

public class User {

    @TableId(type = IdType.AUTO)
    private Integer id;
    private String user_name;
    private String password;
    private String email;
    private Integer age;
}

必须加入该@TableId(type = IdType.AUTO),让自增id才能够正常工作。


测试代码:

@Test
public void testInsert() {
    User user = new User();
    user.setAge(12);
    user.setEmail("admin@misiai.com");
    user.setPassword("12413");
    user.setUser_name("无道");
    int insert = userMapper.insert(user);
    System.out.println("insert = " + insert);
}

1.1. @TableField

@TableField(value="xxxx") // 指定数据库中实际的字段名

@TableField("email") // 指定数据库中实际的字段名
private String mail;

@TableField(exist = false) // 在数据库中不存在

@TableField(exist = false) // 在数据库中不存在
private String address;

@TableField(select = false) // 查询是不显示该字段的值

@TableField(select = false) // 查询是不显示该字段的值
private String password;

2. 更新操作

2.1. 根据id更新

@Test
public void testUpdate() {
    User user = new User();
    user.setId(1);
    user.setAge(20);
    int i = userMapper.updateById(user);
    System.out.println("i = " + i);
}

2.2. 根据条件更新

@Test
public void testUpdateByCondition() {
    User user = new User();
    user.setAge(20);
    user.setUser_name("无道道");
    user.setPassword("ewjalfdif");

    // 查询条件的更新
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();

    queryWrapper.eq("user_name", "张三");

    int update = userMapper.update(user, queryWrapper);
    System.out.println("update = " + update);


}

方法二:

@Test
public void testUpdateByCondition2() {
    User user = new User();
    user.setAge(21);
    user.setUser_name("无道");
    user.setPassword("e21312dif");

    // 查询条件的更新
    UpdateWrapper<User> wrapper = new UpdateWrapper<>();

    wrapper.set("age", 21).set("password", "999999")//更新字段
            .eq("user_name", "无道道");//更新条件

    int update = userMapper.update(user, wrapper);
    System.out.println("update = " + update);


}

3. 删除操作

3.1. 根据id删除

@Test
public void testDeleteById() {
    int i = userMapper.deleteById(11);
    System.out.println("i = " + i);
}

3.2. 根据map删除

@Test
public void testDeleteByMap() {
    Map<String, Object> map = new HashMap<>();
    map.put("user_name", "无道");
    map.put("age", 21);

    int i = userMapper.deleteByMap(map);
    System.out.println("i = " + i);
}

3.3. 根据wrap删除

@Test
public void testDeleteWrap() {
    // QueryWrapper<User> wrapper = new QueryWrapper<>();
    // wrapper.eq("user_name", "无道");


    // 用法二
    User user = new User();
    user.setAge(21);
    user.setUser_name("无道");
    user.setPassword("e21312dif");

    QueryWrapper<User> wrapper = new QueryWrapper<>(user);

    int delete = userMapper.delete(wrapper);
    System.out.println("delete = " + delete);

}

3.4. 根据多个id删除

@Test
public void testDeleteBatchId() {
    int i = userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
    System.out.println("i = " + i);
}

4. 查询操作

4.1. 查询一条

当我们查询的数据,实际有多条时,会报错!!

@Test
public void testSelectOne() {
    QueryWrapper<User> wrapper = new QueryWrapper<>();

    wrapper.eq("user_name", "赵六");
    User user = userMapper.selectOne(wrapper);
    // 当我们查询的数据,实际有多条时,会报错!!
    System.out.println("user = " + user);
}

4.2. 查询条数

@Test
public void testSelectCount() {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.gt("age", 20);
    Integer integer = userMapper.selectCount(wrapper);
    System.out.println("integer = " + integer);
}

4.3. 查询列表

@Test
public void testSelectList() {

    QueryWrapper<User> wrapper = new QueryWrapper<>();

    wrapper.like("password", "8");

    List<User> users = userMapper.selectList(wrapper);
    System.out.println("users = " + users);

}

4.4. 分页查询

1、配置

package com.misiai.mybatisplus.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    // 配置分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

2、查询

@Test
public void testSelectPage() {
    int current = 2;//当前页
    int row = 1;// 每页条数
    Page<User> page = new Page<>(current, row);



    QueryWrapper<User> wrapper = new QueryWrapper<>();

    wrapper.like("password", "8");

    Page<User> selectPage = userMapper.selectPage(page, wrapper);

    System.out.println("selectPage.getSize() = " + selectPage.getSize());
    System.out.println("selectPage.getTotal() = " + selectPage.getTotal());
    System.out.println("selectPage.getOrders() = " + selectPage.getOrders());
    System.out.println("selectPage.getCurrent() = " + selectPage.getCurrent());
    System.out.println("selectPage.getRecords() = " + selectPage.getRecords());

}
本节阅读完毕! (分享
二维码图片 扫描关注我们哟