MyBatis-通用Mapper-tk.mybatis的使用
MyBatis-通用Mapper[更新中]
tk.mybatis的使用
前言
使用MyBatis开发,如果是普通是同MyBatis进行开发,那么就需要在xml文件中编写大量的SQL。当数据库表结构发生改动时,对应的所有的SQL及其实体类都需要更改,这样开发的效率就有点低。
什么是Mapper
为了解决这个问题,使用通用Mapper;通用Mapper基于MyBatis的插件,开发人员不需要编写SQL,不需要在DAO中增加对应的方法,只要写好实体类就能支持自动添加响应的增删查改方法。
如何使用Mapper
在Spring Boot 中使用tk.mapper:
Maven依赖:在Maven工程的commom子工程中设置。
<!-- 通用Mapper插件 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
逆向工程
使用逆向工程创建数据库对应的实体类pojo、mapper下的xml文件。
--见另外的文章--
mapper继承了tk.mapper后,便拥有了Mapper的所有通用方法
Mapper通用SQL
Select
方法:List<T> select(T record);
说明:根据实体中的属性值进行查询,查询条件使用等号
方法:T selectByPrimaryKey(Object key);
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
方法:List<T> selectAll();
说明:查询全部结果,select(null)方法能达到同样的效果
方法:T selectOne(T record);
说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
方法:int selectCount(T record);
说明:根据实体中的属性查询总数,查询条件使用等号
Insert
方法:int insert(T record);
说明:保存一个实体,null的属性也会保存,不会使用数据库默认值
方法:int insertSelective(T record);
说明:保存一个实体,null的属性不会保存,会使用数据库默认值
Update
方法:int updateByPrimaryKey(T record);
说明:根据主键更新实体全部字段,null值会被更新
方法:int updateByPrimaryKeySelective(T record);
说明:根据主键更新属性不为null的值
Delete
方法:int delete(T record);
说明:根据实体属性作为条件进行删除,查询条件使用等号
方法:int deleteByPrimaryKey(Object key);
说明:根据主键字段进行删除,方法参数必须包含完整的主键属性
Example方法
方法:List<T> selectByExample(Object example);
说明:根据Example条件进行查询
重点:这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列
方法:int selectCountByExample(Object example);
说明:根据Example条件进行查询总数
方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
说明:根据Example条件更新实体record包含的全部属性,null值会被更新
方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
说明:根据Example条件更新实体record包含的不是null的属性值
方法:int deleteByExample(Object example);
说明:根据Example条件删除数据
如何使用
vo类:实体类,负责传输数据。这里以工程中的 renderSixNewItems()
方法为例,方法的功能是,查询类别下的6个items。由于Mapper提供的SQL方法是不足的,所以需要使用Example来配合使用,搭建复杂的SQL。
@Override
public SixNewItemVo renderSixNewItems(Integer rootCatId){
// category 获取实体类
// categoryMapper.selectByPrimaryKey(rootCatId); 通过主键从数据库中查询该实体类
Category category = categoryMapper.selectByPrimaryKey(rootCatId);
//SixNewItemVo -- vo -- value object 值对象 用于业务层之间的数据传输。仅仅是包含数据,就是提取实体类的部分数据进行传输。
SixNewItemVo sixNewItemVo = new SixNewItemVo();
sixNewItemVo.setRootCatName(category.getName());
sixNewItemVo.setSlogan(category.getSlogan());
sixNewItemVo.setBgColor(category.getBgColor());
sixNewItemVo.setCatImage(category.getCatImage());
//获取到 getName、getSlogan、getBgColor、getCatImage
/**
* 实体类
* 使用Example来实现其他SQL
* 当前的需求是:根据category的rootCatId来查询同一类别下的items,即一个大类下的商品
* */
//example 使用Items的类名,即表示我这个example是为Items服务的,接收的数据也就是Items类
Example example = new Example(Items.class);
//createCriteria 创建查询条件、 andEqualTo 某个列等于***
example.createCriteria().andEqualTo("rootCatId",rootCatId);
System.out.println("id : " + rootCatId);
//排序
example.orderBy("sellCounts").desc();
PageHelper.startPage(1,6);
//把example作为查询条件放入Mapper提供的selectByExample查询
//items 为查询结果
List<Items> items = itemsMapper.selectByExample(example);
List<ItemsVo> itemsVos = new ArrayList<>();
for (Items item : items) {
ItemsVo itemsVo = new ItemsVo();
itemsVo.setItemId(item.getId());
itemsVo.setItemName(item.getItemName());
ItemsImg itemsImg = new ItemsImg();
itemsImg.setItemId(item.getId());
itemsImg.setIsMain(1);
List<ItemsImg> itemsImgs = itemsImgMapper.select(itemsImg);
itemsVo.setItemUrl(itemsImgs.get(0).getUrl());
itemsVos.add(itemsVo);
System.out.println("itemsVo_id : " + itemsVo.getItemId());
}
sixNewItemVo.setItemsVoList(itemsVos);
return sixNewItemVo;
}
引用链接:
Mybatis通用Mapper(tk.mybatis)的使用:https://blog.csdn.net/lijingjingchn/article/details/84819536
Java 的 VO类: https://blog.csdn.net/jikefzz1095377498/article/details/79237618
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?