初始环境配置流程

创建SpringBoot工程,引入MyBatis-Plus起步依赖

 <!--mybatisplus起步依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>

配置文件
配置文件

pojo类

@Data
@TableName("tb_user") //如果数据库中的表名称与实体名称一致那么此处可以不写tableName注解来对应
public class User {
    @TableId(type = IdType.AUTO) //自增
    private Long id;
    //@TableField("user_name") //可以不写 //不会有问题 遵循驼峰式命名
    private String userName;
    private String password;
    private String name;
    private Integer age;
    private String email;
    /*@TableField(exist = false) //不根据某个字段查询或者插入
    private List<Role> roleList;*/
}

接口实现BaseMapper<>

@Repository
public interface UserMapper extends BaseMapper<User> {
}

1.添加元素

@Test
    public void insertUser() {
        User user = new User();
        user.setUserName("刘能");
        user.setPassword("888888");
        int insert = userMapper.insert(user);
        System.out.println(insert);
    }

2.根据id删除元素

 @Test
    public void delById(){
        int i = userMapper.deleteById(6);
    }

3.根据id集合批量删除

@Test
    public void delByIds(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        int i = userMapper.deleteBatchIds(list);
    }

4.根据map组装条件删除

 @Test
    public void delByMap() {
        //map中没有条件----》DELETE FROM tb_user
        //map中存在userName和password------》DELETE FROM tb_user WHERE password = ? AND user_name = ?
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_name","sunqi");
        map.put("password","123456");
        int i = userMapper.deleteByMap(map);
    }

5.根据id修改

 @Test
    public void update(){
        User user = new User();
        user.setId(3L);
        user.setUserName("zhaosi");
        //UPDATE tb_user SET user_name=? WHERE id=?
        int i = userMapper.updateById(user);
    }

6.根据id查询

    @Test
    public void selectById(){
        User user = userMapper.selectById(3);
        System.out.println(user);
    }

7.根据id集合批量查询

   @Test
    public void selectByIds(){
        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(4);
        List<User> userList = userMapper.selectBatchIds(list);
    }

8.根据map组装条件查询

    @Test
    public void selectByMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("user_name", "zhaosi");
        map.put("password", "123456");
        List<User> users = userMapper.selectByMap(map);
    }

9.分页查询

配置-分页拦截器

@Configuration
public class PageConfig {

    /**
     * 3.4.0之前的版本用这个
     * @return
     */
   /* @Bean
    public PaginationInterceptor paginationInterceptor(){
        return  new PaginationInterceptor();
    }*/

    /**
     * 3.4.0之后提供的拦截器的配置方式
     * @return
     */
   @Bean
   public MybatisPlusInterceptor mybatisPlusInterceptor(){
       MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
       mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
       return mybatisPlusInterceptor;
   }
}

    @Test
    public void queryForPage() {
        int current = 1; //当前页
        int size = 2; //页大小
        Page<User> page = new Page<>(current,size);
        userMapper.selectPage(page, null);
        List<User> userList = page.getRecords();
        System.out.println("查询结果: " + userList);
        long pages = page.getPages();
        System.out.println("总页数: " + pages);
        long total = page.getTotal();
        System.out.println("总记录数:" + total);
    }

10.构造查询条件对象

    @Test
    public void wrapper1(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name","zhaosi")
                    .or()
                    .eq("user_name","zhaoliu");
        List<User> userList = userMapper.selectList(queryWrapper);
        System.out.println(userList);
    }

11.like条件查询

    @Test
    public void wrapperLike(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //queryWrapper.like("name", "李"); //此处需要注意会自动拼接 %,不需要手动拼接---》%李%
        queryWrapper.likeLeft("name", ""); //仅在左边添加百分号--》%李
        //queryWrapper.likeRight("name", "李");//仅在右边添加百分号--》李%
        userMapper.selectList(queryWrapper);
    }

12.排序+分页

    @Test
    public void queryForPageAndOrder() {
        int current = 1; //当前页
        int size = 2; //页大小
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.orderByDesc("id");
        Page<User> page = new Page<>(current, size);
        userMapper.selectPage(page, userWrapper);
        List<User> userList = page.getRecords();
        System.out.println("查询结果: " + userList);
        long pages = page.getPages();
        System.out.println("总页数: " + pages);
        long total = page.getTotal();
        System.out.println("总记录数:" + total);
    }

13.根据wrapper构造条件删除

    @Test
    public void delByWrapper(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", "zhaosi");
        userMapper.delete(wrapper);
    }

14.根据wrapper构造条件修改

    @Test
    public void updateByWrapper01() {
        UpdateWrapper<User> userQueryWrapper = new UpdateWrapper<>();
        userQueryWrapper.eq("user_name", "zhaoliu")
                .set("name", "刘能")
                .set("user_name", "liuneng");
        userMapper.update(null, userQueryWrapper);
    }

    @Test
    public void updateByWrapper02(){
        // UPDATE tb_user SET password=?, age=? WHERE (user_name = ?)
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_name", "liuneng"); //条件
        User user = new User();
        user.setPassword("666666");
        user.setAge(45);
        userMapper.update(user, wrapper);
    }
Last modification:February 22nd, 2021 at 02:27 am
我从来都不喜欢钱,我也没碰过钱,我对钱没兴趣