加油,新时代打工人!
MyBatis-Plus快速入门–环境搭建
MyBatis-Plus–三种方式整合之一 Mybatis+MP整合
MyBatis-Plus–三种方式整合之二 Spring+MP整合
MyBatis-Plus–三种方式整合之三 Spring Boot+MP整合
数据库脚本和更多MP整合方式,参考以上文章
以下文章环境基于MyBatis-Plus–三种方式整合之三 Spring Boot+MP整合以上进行了扩展
新的分页插件package itboywh.mp; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("itboywh.mp.mapper") public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); paginationInnerInterceptor.setDbType(DbType.MYSQL); paginationInnerInterceptor.setOverflow(true); interceptor.addInnerInterceptor(paginationInnerInterceptor); return interceptor; } @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setUseDeprecatedExecutor(false); } }Spring Boot+Mybatis Plus 详细CRUD和分页查询
package itboywh.mp; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import itboywh.mp.mapper.UserMapper; import itboywh.mp.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @SpringBootTest @RunWith(SpringRunner.class) public class UserAdd { @Autowired private UserMapper userMapper; @Test public void add() { User user = new User(); user.setAge(20); user.setName("小明"); user.setMail("itboywh@github.com"); user.setUserName("xiaoming"); user.setPassword("123456"); int insert = userMapper.insert(user); System.out.println("受影响行数" + insert); System.out.println("id>=" + user.getId()); } @Test public void selecById() { User select = userMapper.selectById(6); System.out.println("更根据用户id查询" + select); } @Test public void update1() { User user = new User(); user.setAge(25); QueryWrapperqueryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", 6); int updateById = userMapper.update(user, queryWrapper); System.out.println("更新条数 " + updateById); } @Test public void update2() { UpdateWrapper UpdateWrapper = new UpdateWrapper<>(); UpdateWrapper.eq("id", 6).set("age", 5); int updateById = userMapper.update(null, UpdateWrapper); System.out.println("更新条数 " + updateById); } @Test public void deletebyId() { int deleteById = userMapper.deleteById(5); System.out.println("受影响行数 " + deleteById); } @Test public void deletebyMap() { Map columnMap = new HashMap (); columnMap.put("name", "小豪"); columnMap.put("age", 20); //将columnMap中的元素设置为删除的条件,多个之间为and关系 int i = userMapper.deleteByMap(columnMap); System.out.println("受影响行数 " + i); } @Test public void delete() { User user = new User(); user.setName("小华"); user.setAge(20); QueryWrapper queryWrapper = new QueryWrapper (); int delete = userMapper.delete(queryWrapper); System.out.println("受影响行数" + delete); } @Test public void deleteBatchIds() { // 删除(根据ID 批量删除) int deleteBatchIds = userMapper.deleteBatchIds(Arrays.asList(15, 17)); System.out.println("受影响行数 " + deleteBatchIds); } @Test public void seletebyId() { User selectById = userMapper.selectById(16); System.out.println("根据ID查询 " + selectById); } @Test public void selectBatchIds() { List users = userMapper.selectBatchIds(Arrays.asList(16, 18)); for (User user : users) { System.out.println(user); } } @Test public void live() { System.out.println("愿我们的十二月份如烟花一样灿烂 "); } @Test public void selectOne() { QueryWrapper queryWrapper = new QueryWrapper (); queryWrapper.eq("name", "小明"); User selectOne = userMapper.selectOne(queryWrapper); System.out.println("根据添加查询" + selectOne); } @Test public void selectCount() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.gt("age", 20); //年龄大于20 Integer selectCount = userMapper.selectCount(queryWrapper); System.out.println("查询是总数量" + selectCount); } @Test public void selectList() { List users = this.userMapper.selectList(null); for (User user : users) { System.out.println(user); } } @Test public void selectPage() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.like("age", 2); //like模糊查询 Page page = new Page<>(1, 1); IPage iPage = userMapper.selectPage(page, queryWrapper); System.out.println("数据总条数" + iPage.getTotal()); System.out.println("总页数" + iPage.getPages()); System.out.println("当前页数" + iPage.getCurrent()); List users = iPage.getRecords(); for (User user : users) { System.out.println(user); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)