MP教程-CRUD

Posted deepminer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MP教程-CRUD相关的知识,希望对你有一定的参考价值。

CRUD

一、查询

    @Test
    public void UserMapperTest() {
        List<User> userList = userMapper.selectList(null);
        for(val user:userList){
            System.out.println(user.toString());
        }
    }
    
    @Test
    public void UserSelectByIdTest() {
        List<Integer> ids = new ArrayList();
        ids.add(1);
        ids.add(2);
        List<User> users = userMapper.selectBatchIds(ids);
        for (val user : users) {
            System.out.println(user.toString());
        }
    }

 

 

二、插入


@Test
    public void UserInsertTest() {
        User user = User.builder().name("曹操").password("123fdkfjk").age(33).build();
        
        int insert = userMapper.insert(user);
        System.out.println(insert);
        System.out.println(user);
    }

 

 

三、更新

3.1 使用 QueryWrapper
    @Test
    public void UserUpdateTest() {
        User user = User.builder().name("李四-修改").build();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", 1);
        int update = userMapper.update(user, queryWrapper);
        System.out.println(update);
    }

 

2020-03-14 21:26:41,627 INFO (StartupInfoLogger.java:61)- Started SpringBootMybatisPlusApplicationTests in 4.926 seconds (JVM running for 6.907)
2020-03-14 21:26:42,197 INFO (HikariDataSource.java:110)- HikariPool-1 - Starting...
2020-03-14 21:26:43,209 INFO (HikariDataSource.java:123)- HikariPool-1 - Start completed.
2020-03-14 21:26:43,223 DEBUG (BaseJdbcLogger.java:143)- ==>  Preparing: UPDATE tb_user SET user_name=? WHERE (id = ?) 
2020-03-14 21:26:43,284 DEBUG (BaseJdbcLogger.java:143)- ==> Parameters: 李四-修改(String), 1(Integer)
2020-03-14 21:26:43,316 DEBUG (BaseJdbcLogger.java:143)- <==    Updates: 1

 

3.2 使用UpdateWrapper

 @Test
    public void UserUpdateByUpdateWrapperTest() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
        updateWrapper.set("user_name", "李四-修改2");
        updateWrapper.eq("id", 1);
        
        int update = userMapper.update(null, updateWrapper);
        System.out.println(update);
    }

 


2020-03-14 21:32:23,135 INFO (StartupInfoLogger.java:61)- Started SpringBootMybatisPlusApplicationTests in 4.742 seconds (JVM running for 6.716)
2020-03-14 21:32:23,667 INFO (HikariDataSource.java:110)- HikariPool-1 - Starting...
2020-03-14 21:32:24,767 INFO (HikariDataSource.java:123)- HikariPool-1 - Start completed.
2020-03-14 21:32:24,782 DEBUG (BaseJdbcLogger.java:143)- ==>  Preparing: UPDATE tb_user SET user_name=? WHERE (id = ?) 
2020-03-14 21:32:24,845 DEBUG (BaseJdbcLogger.java:143)- ==> Parameters: 李四-修改2(String), 1(Integer)
2020-03-14 21:32:24,850 DEBUG (BaseJdbcLogger.java:143)- <==    Updates: 1

 

 

四、删除

    @Test
    public void UserDeleteByBatchIdsTest() {
        int delete = userMapper.deleteBatchIds(Arrays.asList(11L, 12L));
    }
    
    @Test
    public void UserDeleteByWrapper()
    {
        QueryWrapper<User> wapper = new QueryWrapper<User>();
        wapper.eq("id", 11).eq("user_name", "不存在的name");
        userMapper.delete(wapper);
    }

 

五、分页

  • 配置分页拦截器


package com.will.learn.springboot.config;
?
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
?
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
?
@Configuration
public class MybatisPlusConfig {
?
    @Bean
    public PaginationInterceptor Page() {
        return new PaginationInterceptor();
    }
}

 

  • 分页查询

@Test
    public void UserPageTest() {
        Page<User> page = new Page<>();
        page.setSize(3);
        page.setCurrent(0);
        Page<User> users = userMapper.selectPage(page, null);
        System.out.println("总页数=》" + users.getPages());
        System.out.println("当前页=》" + users.getCurrent());
        System.out.println("总数据=》" + users.getTotal());
        
        for (User user : users.getRecords()) {
            System.out.println(user);
        }
    }

 

2020-03-14 22:10:40,786 INFO (StartupInfoLogger.java:61)- Started SpringBootMybatisPlusApplicationTests in 4.908 seconds (JVM running for 6.705)
2020-03-14 22:10:41,291 INFO (HikariDataSource.java:110)- HikariPool-1 - Starting...
2020-03-14 22:10:42,418 INFO (HikariDataSource.java:123)- HikariPool-1 - Start completed.
2020-03-14 22:10:42,530 DEBUG (BaseJdbcLogger.java:143)- ==>  Preparing: SELECT COUNT(1) FROM tb_user 
2020-03-14 22:10:42,651 DEBUG (BaseJdbcLogger.java:143)- ==> Parameters: 
2020-03-14 22:10:42,757 DEBUG (BaseJdbcLogger.java:143)- ==>  Preparing: SELECT id,user_name AS name,password,age FROM tb_user LIMIT ?,? 
2020-03-14 22:10:42,760 DEBUG (BaseJdbcLogger.java:143)- ==> Parameters: 0(Long), 3(Long)
2020-03-14 22:10:42,775 DEBUG (BaseJdbcLogger.java:143)- <==      Total: 3
总页数=》1
当前页=》0
总数据=》3
User(id=1, name=李四-修改2, password=1234, age=20)
User(id=2, name=小黄, password=23454, age=11)
User(id=3, name=曹操, password=123fdkfjk, age=33)

 

以上是关于MP教程-CRUD的主要内容,如果未能解决你的问题,请参考以下文章

11SpringBoot-CRUD-thymeleaf公共页面元素抽取

Mybatis-plus入门教程

推荐使用mybatis-plus逆向生成crud代码,让你开发效率提升50%

推荐使用mybatis-plus逆向生成crud代码,让你开发效率提升50%

iOS,AVPlayer - 循环播放 MP3 片段

SpringBoot整合MyBatis-Plus3.1详细教程