MybatisPlus使用详解

网友投稿 1266 2022-11-19

MybatisPlus使用详解

MybatisPlus使用详解

参考内容

​​MybatiesPlus 官网​​​​SQL基本语法​​​​Mybaties3官网​​​​mybatis-plus快速入门并使用​​​​mybatis-plus中wrapper的用法(详细)​​​​MyBatis-Plus自动生成mapper service controller​​​​MybatisPlus:泛型方法使用listObjs(Function<? super Object, V> mapper)​​

搭建工程继承测试

​​搭建工程​​​​集成测试​​

插件介绍

项目基本文件我是用该插件生成的

方法上Alt + Enter

idea 连接数据 快速生成

Mybaties 的简单案例

mapper

// 简单实现两个接口@Mapperpublic interface GoodsMapper extends BaseMapper { Goods getOne(@Param("id") Integer id); List getMulGoods(@Param("Goods") Goods good);}

xml

MybatiesPlus 原生方法

单个对象和多个对象的保存

@Testpublic void test1(){ ConfigDO c1 = new ConfigDO(); c1.setConfigType(2); c1.setConfig("1#1"); ConfigDO c2 = new ConfigDO(); c2.setConfigType(3); c2.setConfig("1#1"); ConfigDO c3 = new ConfigDO(); c3.setConfigType(4); c3.setConfig("1#1"); ArrayList pList = new ArrayList<>(); pList.add(c1); pList.add(c2); pList.add(c3); configService.saveBatch(pList); }

更新

// 如果有就更新 没有就保存 @Test public void test1(){ ConfigDO c1 = new ConfigDO(); c1.setConfigType(2); c1.setConfig("11#11"); c1.setId(3L); configService.saveOrUpdate(c1); }

@Test public void test1(){ ConfigDO c1 = new ConfigDO(); c1.setConfigType(2); c1.setConfig("11#11"); UpdateWrapper configDOUpdateWrapper = new UpdateWrapper<>(); // 先尝试更新 如果无法更新一般是不存在 就尝试插入保存 configDOUpdateWrapper.eq("config_type",2).set("config","11111#55"); configService.saveOrUpdate(c1,configDOUpdateWrapper); }

函数式编程

//函数其实过滤 对获得的结果进一步处理 List listObjs(Wrapper queryWrapper, Function mapper);QueryWrapper configDOQueryWrapper = new QueryWrapper<>(); // 第一个参数是处理前的参数 第二个是处理后的返回对象 configService.listObjs(null, new Function() { @Override public Long apply(Object id) { //进行一系列转换操作 ,最终返回我们所需要的类型V , 这里只做简单演示。 return Long.valueOf(id.toString()); } });

void mpTest() {//你这里function 的入参就是 tableId 那个字段啊,我还以为是entity对象呢List list = userService.list();list.stream().forEach(System.out::println);System.out.println("--------------------------------");List longs = userService.listObjs(null, new Function() { @Override public UserVo apply(Object id) { System.out.println("apply的参数"+id.toString()); UserVo userVo = new UserVo(); User user = userService.getById(Integer.valueOf(id.toString())); copyProperties(user,userVo); return userVo; }});longs.stream().forEach(System.out::println);}

链式查询

链式编程是为了匹配多个条件 链式column Person::getAge 查询用list 的

QueryChainWrapper configDOQueryChainWrapper = new QueryChainWrapper<>(configMapper);List config_type = configDOQueryChainWrapper.eq("config_type", "9").list();System.out.println(JSON.toJSON(config_type));

匿名查询

kt 是给 kotin 使用的

LambdaQueryWrapper lambda3 = Wrappers.lambdaQuery();lambda3.eq(ConfigDO::getConfigType,6);final List configDOList = configMapper.selectList(Wrappers.lambdaQuery().eq(ConfigDO::getConfigType,9));System.out.println(JSON.toJSONString(configDOList));final List list = this.service.lambdaQuery().eq(Student::getSid, "02").list();System.out.println(JSON.toJSONString(list));

匿名更新

@Testpublic void test3() { boolean update = service.lambdaUpdate().eq(Student::getSid, "02").update(new Student("112", "Kidd", LocalDateTime.now(), "e"));}

listByMap

@Testpublic void test4() { HashMap hashMap = new HashMap(); hashMap.put("Sname","Kidd"); // 带map 其实是返回数据对应的列名 final List students = this.service.listByMap(hashMap); System.out.println(JSON.toJSONString(students));}@Testpublic void test7(){ final List> maps = service.listMaps(); List list = service.list(); System.out.println(JSON.toJSONString(maps)); System.out.println(JSON.toJSONString(list));}{"Ssex":"男","Sname":"赵雷","Sage":631123200000,"SID":"01"},{"Ssex":"e","Sname":"Kidd","Sage":1638964099000,"SID":"112"},{"Ssex":"男","Sname":"孙风","Sage":643129200000,"SID":"03"},{"Ssex":"男","Sname":"李云","Sage":649868400000,"SID":"04"},{"Ssex":"女","Sname":"周梅","Sage":691516800000,"SID":"05"},{"Ssex":"女","Sname":"吴兰","Sage":699379200000,"SID":"06"},{"Ssex":"女","Sname":"郑竹","Sage":615222000000,"SID":"07"},{"Ssex":"女","Sname":"王菊","Sage":632764800000,"SID":"08"}][{"sage":"1990-01-01T00:00:00","sid":"01","sname":"赵雷","ssex":"男"},{"sage":"2021-12-08T19:48:19","sid":"112","sname":"Kidd","ssex":"e"},{"sage":"1990-05-20T00:00:00","sid":"03","sname":"孙风","ssex":"男"},{"sage":"1990-08-06T00:00:00","sid":"04","sname":"李云","ssex":"男"},{"sage":"1991-12-01T00:00:00","sid":"05","sname":"周梅","ssex":"女"},{"sage":"1992-03-01T00:00:00","sid":"06","sname":"吴兰","ssex":"女"},{"sage":"1989-07-01T00:00:00","sid":"07","sname":"郑竹","ssex":"女"},{"sage":"1990-01-20T00:00:00","sid":"08","sname":"王菊","ssex":"女"}]

listObjs接口

​​其实就是将返回值参数转换成对应类型​​

List longs = configService.listObjs(null, new Function() { @Override public Long apply(Object id) { //进行一系列转换操作 ,最终返回我们所需要的类型V , 这里只做简单演示。 return Long.valueOf(id.toString()); } });

分页查询

配置分页插件

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * @author yantingrui * @date 2021/11/25 11:29 */ @Configuration @EnableTransactionManagement @MapperScan("com.koala.mapper") public class MybatisPlusConfig { private static final int LIMIT = 10000; /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); // 开启 count 的 join 优化,只针对部分 left join paginationInterceptor.setLimit(LIMIT); paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; } }

实现案例

IPage page = new Page<>(2, 5); IPage goodsIPage = goodsMapper.selectPage(page, null);

自己写mapper 文件

Wrapper 子类介绍

匿名查询和链式查询的区别

结论

链式都用list 查询 非链式都用普通查询要放入service 链式直接查询 匿名都用函数传入行名称 非匿名都用字符串传入行名称 匿名查询无法分页

代码

@Test public void test9(){ // 非链式普通 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("SID","01"); //wrapper.eq(Student::getSid,"01"); List list = this.service.list(wrapper); System.out.println(JSON.toJSONString(list)); } @Test public void test10(){ // 非链式匿名 LambdaQueryWrapper lamdbaWrapper = new LambdaQueryWrapper<>(); // 匿名用的是函数式 LambdaQueryWrapper wrapper = lamdbaWrapper.eq(Student::getSid, "01"); List lamdbaList = this.service.list(wrapper); System.out.println(JSON.toJSONString(lamdbaList)); } @Test void test11(){ // 链式普通 QueryChainWrapper query = this.service.query(); query.eq("SID","01").like("Sname","*雷*"); final List list = query.list();// List list = this.service.list(query); System.out.println(JSON.toJSONString(list)); } @Test void test12(){ // 链式匿名 LambdaQueryChainWrapper query = this.service.lambdaQuery(); query.eq(Student::getSid,"01").like(Student::getSname,"*雷"); final List list = query.list();// List list = this.service.list(query); System.out.println(JSON.toJSONString(list)); }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HttpConnection 无法识别localhost,拒绝连接的问题
下一篇:Use of undefined constant FTP_BINARY - assumed 'FTP_BINARY
相关文章

 发表评论

暂时没有评论,来抢沙发吧~