搭建使用环境(SpringBoot)Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis
的基础上只做增强不做改变,避免了我们重复CRUD语句。
1.创建一个简单点的maven工程
2.在pom.xml中引入依赖
org.springframework.boot spring-boot-starter-parent2.3.0.RELEASE 4.0.0 MybatisPlusDemoUTF-8 UTF-8 1.8 3.3.2 true org.springframework.boot spring-boot-startercom.h2database h2runtime com.baomidou mybatis-plus-boot-starter${mybatisplus.version} org.assertj assertj-coretest org.projectlombok lombokprovided org.springframework.boot spring-boot-starter-testtest com.github.pagehelper pagehelper5.1.11 org.springframework.boot spring-boot-maven-plugin
3.在资源目录下创建application.yml配置文件和目录
在application.yml中写上数据库的配置和Mybatis扫描
# DataSource Config spring: datasource: driver-class-name: org.h2.Driver schema: classpath:db/schema-h2.sql data: classpath:db/data-h2.sql url: jdbc:h2:mem:test username: root password: test # Logger Config logging: level: com.da: debug # 配置mybatis plus mybatis-plus: type-aliases-package: com.da.domain #别名搜索 mapper-locations: classpath:/mapper ASSIGN_ID(3)
排除实体类中非表字段
使用 @TableField(exist = false) 注解
@Test public void insert(){ User user=new User(); user.setId(26L); user.setName("我是insert"); user.setAge(23); user.setEmail("da@163.com"); int insert = userMapper.insert(user); if(insert>0){ System.out.println("插入成功!!!"); Listusers = userMapper.selectList(null); users.forEach(user1 -> { System.out.println(user1); }); }else{ System.out.println("插入失败!!"); } }
结果:
@Test public void select(){ //查询全部 Listusers = userMapper.selectList(null); users.forEach(user ->{ System.out.println(user); }); System.out.println("===========================根据条件查询====================="); //根据条件查询 List users1 = userMapper.selectList(new QueryWrapper ().like("name", "J")); users1.forEach(user ->{ System.out.println(user); }); //根据条件查询一个 System.out.println("===========================根据条件查询一个====================="); User user = userMapper.selectOne(new QueryWrapper ().eq("name", "Biie")); System.out.println(user); //根据主键查询 System.out.println("===========================根据主键查询一个====================="); User user1 = userMapper.selectById(2); System.out.println(user1); }
结果
@Test public void update(){ User user=new User(); user.setId(5L); user.setName("我修改后的数据"); user.setAge(230); user.setEmail("dada@163.com"); //根据id修改 System.out.println("根据id修改"); userMapper.updateById(user); User user1 = userMapper.selectById(5); System.out.println(user1); System.out.println("=======================根据条件修改方式1==============================="); userMapper.update(null, Wrappers.update().set("email","@update.com").like("name","B")); List users = userMapper.selectList(new QueryWrapper ().like("name", "B")); users.forEach(user11 -> { System.out.println(user11); }); user.setName("根据条件修改方式2"); System.out.println("=======================根据条件修改方式2==============================="); userMapper.update(user, Wrappers. update().eq("id","5")); List users1 = userMapper.selectList(new QueryWrapper ().eq("id", "5")); users1.forEach(user11 -> { System.out.println(user11); }); }
结果
@Test public void delete(){ //根据Id删除 int i = userMapper.deleteById(1); if(i>0){ System.out.println("删除成功!!!"); userMapper.selectList(null).forEach(user -> { System.out.println(user); }); } //根据条件删除方式1 int delete = userMapper.delete(new QueryWrapper().like("name", "J")); if(delete>0){ System.out.println("根据条件删除方式1删除成功"); userMapper.selectList(null).forEach(user -> { System.out.println(user); }); } //根据条件删除方式2 int delete1 = userMapper.delete(Wrappers. query().like("name", "T")); if(delete1>0){ System.out.println("根据条件删除方式2删除成功"); userMapper.selectList(null).forEach(user -> { System.out.println(user); }); } }
结果
在pom.xml中引入pageHelper分页依赖
com.github.pagehelper pagehelper5.1.11
在config包中新键分页配置类
MybatisPlusConfig
@Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { // 开启 count 的 join 优化,只针对 left join !!! return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true)); } @Bean public PageInterceptor pageInterceptor() { return new PageInterceptor(); } }Mybatis Plus 内置分页
@Test public void page(){ System.out.println("----baseMapper自带分页"); //第一个参数是当前页,第二参数是每页数 Pagepage=new Page<>(1,5); IPage userPage = userMapper.selectPage(page, new QueryWrapper ()); System.out.println("当前页码:"+userPage.getCurrent()); System.out.println("每页数:"+userPage.getSize()); System.out.println("总条数"+userPage.getTotal()); //获取分页的结果 userPage.getRecords().forEach(user ->{System.out.println(user);}); }
结果
自定义xml分页
application.yml配置文件
# 配置mybatis plus mybatis-plus: type-aliases-package: com.da.domain #别名搜索 mapper-locations: classpath:/mapper public IPageselectUserByPage(@Param("p") IPage page, @Param("c") User conditioin); public List selectUserByPage2(User u); }
UserMapper.xml映射文件
select * from user2
测试
@Test public void testPage(){ //自定义XML分页查询 Pagepage=new Page<>(1,5); User user=new User(); user.setEmail("te"); user.setAge(331); IPage userPage = userMapper.selectUserByPage(page, user); System.out.println("当前页码:"+userPage.getCurrent()); System.out.println("每页数:"+userPage.getSize()); System.out.println("总条数"+userPage.getTotal()); //获取分页的结果 userPage.getRecords().forEach(user1 ->{System.out.println(user1);}); }
结果
开启扫描
UserMapper.xml映射文件
测试
@Test public void pageHelper(){ //自定义XML分页查询 User user=new User(); user.setEmail("te"); user.setAge(331); PageInfopage= PageHelper.startPage(1,5).doSelectPageInfo( ()-> userMapper.selectUserByPage2(user)); System.out.println("总行数=" + page.getTotal()); System.out.println("当前页=" + page.getPageNum()); System.out.println("每页行数=" + page.getPageSize()); System.out.println("总页数=" + page.getPages()); System.out.println("起始行数=" + page.getStartRow()); System.out.println("是第一页=" + page.isIsFirstPage()); System.out.println("是最后页=" + page.isIsLastPage()); System.out.println("还有下一页=" + page.isHasNextPage()); System.out.println("还有上一页=" + page.isHasPreviousPage()); System.out.println("页码列表" + Arrays.toString(page.getNavigatepageNums())); page.getList().forEach(System.out::println); }
结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)