MyBatis-Ext怎么用(云服务器、云主机、高防IP、高防服务器、香港服务器、美国服务器,开发技术)

时间:2024-05-03 08:23:19 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

最近在工作中,接触到了一个MyBatis扩展工具包MyBatis-Ext,可以说很大程度上减轻了使用mybatis时的工作量。

MyBatis-Ext是MyBatis的增强扩展,和我们平常用的Mybatis-plus非常类似,简化了MyBatis对单表增删改查的操作,提供通用的增删改查,支持函数式编程,支持分页查询,支持用户自定义通用方法,并且能够防止SQL注入。集成起来也非常简单,对MyBatis只做增强不做修改。

以spring-boot项目为例,集成非常简单。pom导入核心依赖:

<dependency><groupId>tech.wetech.mybatis</groupId><artifactId>mybatis-ext-core</artifactId><version>1.5.2</version></dependency><dependency><groupId>tech.wetech.mybatis</groupId><artifactId>mybatis-ext-spring-boot-starter</artifactId><version>1.5.2</version></dependency>

需要注意的是,引入mybatis-ext-spring-boot-starter后无需再引入mybatis-spring-boot-starter。
和以往一样,在application.yml配置一下数据源:

spring:datasource:username:daterpassword:123456url:jdbc:mysql://127.0.0.1:3306/datacenter?useUnicode=true&characterEncoding=utf-8driver-class-name:com.mysql.jdbc.Drivertype:com.alibaba.druid.pool.DruidDataSourcedruid:initial-size:8min-idle:1max-active:20mybatis:mapper-locations:classpath:mapping/*Mapper.xmltype-aliases-package:com.mybatis.ext.test.mybatisexttest.entityspring:datasource:username:daterpassword:123456url:jdbc:mysql://127.0.0.1:3306/datacenter?useUnicode=true&characterEncoding=utf-8driver-class-name:com.mysql.jdbc.Drivertype:com.alibaba.druid.pool.DruidDataSourcedruid:initial-size:8min-idle:1max-active:20mybatis:mapper-locations:classpath:mapping/*Mapper.xmltype-aliases-package:com.mybatis.ext.test.mybatisexttest.entity

创建一个映射的实体类:

@Data@Table(name="user")publicclassUser{@IdStringidentifycard;@Column(name="name")Stringname;Stringmoney;Stringcard;Stringphone;Stringrate;}

mybatis-ext使用了Jpa的注解,目前实现了@Table、@Id、@Column、@Transient、@Version。其中@Table、@Id是必须添加的注解,其他非必须添加。使用@Table指定数据表名,@Id指定数据表主键。

查询的Mapper接口继承BaseMapper接口,泛型中填写实体类:

publicinterfaceUserMapperextendsBaseMapper<User>{}

我们来看一下能够直接调用的方法,为在BaseMapper中内置了很多通用方法,可以直接调用,非常简便:

intdeleteByPrimaryKey(PKid);<SextendsT>intinsert(Srecord);<SextendsT>intinsertAll(Iterable<S>record);<SextendsT>intinsertSelective(Srecord);<SextendsT>SselectByPrimaryKey(PKid);<SextendsT>Optional<S>selectByPrimaryKeyWithOptional(IDid);<SextendsT>intupdateByPrimaryKey(Srecord);<SextendsT>intupdateByPrimaryKeySelective(Srecord);<SextendsT>List<S>selectAll();<SextendsT>List<S>selectList(Srecord);<SextendsT>SselectOne(Srecord);<SextendsT>SselectOneWithOptional(Srecord);booleanexistsByPrimaryKey(PKid);<SextendsT>intcount(Srecord);<SextendsT>List<S>selectByExample(Example<S,Object>example);<SextendsT>intcountByExample(Example<S,Object>example);<SextendsT>intdeleteByExample(Example<S,Object>example);<SextendsT>intupdateByExample(@Param("record")Srecord,@Param("example")Example<S,Object>example);<SextendsT>intupdateByExampleSelective(@Param("record")Srecord,@Param("example")Example<S,Object>example);

来进行一下接口调用的测试,先试一下selectAll方法:

@GetMapping("getUser")publicvoidgetUser(){List<User>users=userMapper.selectAll();for(Useruser:users){System.out.println(user.getName()+""+user.getIdentifycard());}}

测试结果:

MyBatis-Ext怎么用

这样,通过调用内置方法就实现了不写sql语句直接进行查询。同样,如果想根据主键进行查询也很简单,直接调用selectByPrimaryKey方法:

@PostMapping("getUserById")publicvoidgetUserByIdentifycard(@RequestBodyUseruser){UserretUser=userMapper.selectByPrimaryKey(user);System.out.println(retUser.toString());}

查询结果:

MyBatis-Ext怎么用

另外,还可以使用Optional包裹查询,修改一下上面主键查询的方法:

@PostMapping("getUserById")publicvoidgetUserByIdentifycard(@RequestBodyUseruser){UserretUser=userMapper.selectByPrimaryKeyWithOptional(user).orElseThrow(()->newRuntimeException("未查到数据"));System.out.println(retUser.toString());}

这样,在传入一个不存在的主键时,就会直接抛出自定义的异常:

还有其他很多简单的查询,大家可以根据上面列出api自行测试一下。此外,还可以使用Criteria,使用逻辑组合,进行函数式查询:

@GetMapping("criteriaTest")publicvoidtestCreateCriteria(){List<User>list=userMapper.createCriteria().andEqualTo(User::getName,"Trunks").andBetween(User::getMoney,100,300).andNotLike(User::getRate,"6").orIn(User::getCard,Arrays.asList("10")).selectList();list.forEach(user->{System.out.println(user.toString());});}

查询结果:

MyBatis-Ext怎么用

也可以使用Example进行查询:

@GetMapping("exampleTest")publicvoidtestExample(){Example<User>example=Example.of(User.class);example.createCriteria().andEqualTo(User::getName,"Trunks").andBetween(User::getMoney,100,300).andNotLike(User::getRate,"6").orIn(User::getCard,Arrays.asList("10"));example.setDistinct(true);List<User>list=userMapper.selectByExample(example);list.forEach(user->{System.out.println(user.toString());});}

结果与使用Criteria结果相同。另外,还可以将多个条件组合使用:

GetMapping("testExampleWithSub")publicvoidselectByExampleWithSub(){try(SqlSessionsession=sqlSessionFactory.openSession()){UserMapperuserMapper1=session.getMapper(UserMapper.class);Example<User>example=Example.of(User.class);example.and().andEqualTo(User::getName,"Trunks");example.and().andEqualTo(User::getCard,"10");example.and().andLessThanOrEqualTo(User::getRate,300);Criteria<User>criteria=newCriteria<>();criteria.andIsNotNull(User::getPhone);example.and(criteria);List<User>list=userMapper1.selectByExample(example);list.forEach(user->{System.out.println(user.toString());});}}

结果:

MyBatis-Ext怎么用

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:MyBatis-Ext怎么用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何使用Java策略模式取代if else下一篇:

12 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18