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

MyBatis-通用Mapper-tk.mybatis的使用的更多相关文章

  1. 值得收藏的Mybatis通用Mapper使用大全。

    引言 由于小编的记性不太好,每次在写代码的时候总是把通用mapper的方法记错,所以今天把通用mapper的常用方法做一下总结,方便以后直接查看.好了,不废话啦. 引包 <!-- 通用Mappe ...

  2. Mybatis 通用 Mapper 和 Spring 集成

    依赖 正常情况下,在原有依赖基础上增加的 mapper-spring. <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spr ...

  3. SpringBoot 3.SpringBoot 整合 MyBatis 逆向工程以及 MyBatis 通用 Mapper

    一.添加所需依赖,当前完整的pom文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...

  4. Spring boot集成 MyBatis 通用Mapper

    配置 POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  5. Spring Boot MyBatis 通用Mapper插件集成 good

    看本文之前,请确保你已经在SpringBoot中集成MyBatis,并能正常使用.如果没有,那么请先移步 http://blog.csdn.net/catoop/article/details/505 ...

  6. spring boot集成MyBatis 通用Mapper 使用总结

    spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插 ...

  7. springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui

    前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...

  8. springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源

    本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...

  9. springboot学习笔记:11.springboot+shiro+mysql+mybatis(通用mapper)+freemarker+ztree+layui实现通用的java后台管理系统(权限管理+用户管理+菜单管理)

    一.前言 经过前10篇文章,我们已经可以快速搭建一个springboot的web项目: 今天,我们在上一节基础上继续集成shiro框架,实现一个可以通用的后台管理系统:包括用户管理,角色管理,菜单管理 ...

  10. Mybatis通用Mapper介绍和使用

    Mybatis通用Mapper介绍与使用 前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中,除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL. ...

随机推荐

  1. Nginx的常用配置

    Nginx配置文件结构 设置worker进程的用户,指的linux中的用户,会涉及到nginx操作目录或文件的一些权限,默认为 nobodyuser root; worker进程工作数设置,一般来说C ...

  2. Chrome 中的自动播放政策

    官方策略说明 Autoplay policy in Chrome 修改本机浏览器设置 修改 chrome 设置允许自动播放声音 electron 允许自动播放声音 issues/13525 具体就是这 ...

  3. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

  4. linux篇-linux 下tomcat服务每天定时启动

    1l先准备一个脚本 #!/bin/sh #./etc/profile export JAVA_HOME=/usr/java/jdk1.6.0_45 sh /home/tomcat-bingchuang ...

  5. unity---克隆/贴图/平移/旋转

    克隆 GameObject clone =Instantiate(gameObject,new Vector3(10,10,10),Quaternion.identity); Destroy(clon ...

  6. mmdetection源码阅读

    2021-11-23号更新 mmdetection中的hook函数 参考: 重难点总结: # step1: 根据官方文档,getattr(self,'name')等同于self.name # sept ...

  7. Probabilistic two-stage detection

  8. Spring Ioc源码分析系列--Bean实例化过程(一)

    Spring Ioc源码分析系列--Bean实例化过程(一) 前言 上一篇文章Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理已经完成了对 ...

  9. Simple, Fast Malicious Multiparty Private Set Intersection-解读

    文本记录阅读该论文的笔记. 这是文章框架,来自视频. 介绍 本文主要解决恶意攻击下安全的多方PSI,主要用到两大技术OPPRF和OKVS,构造合谋和不合谋的协议. 基础知识 OPPRF 这部分在OPR ...

  10. 看看CabloyJS工作流引擎是如何实现Activiti边界事件的

    CabloyJS内置工作流引擎的基本介绍 1. 由来 众所周知,NodeJS作为后端开发语言和运行环境,支持高并发.开发效率高,有口皆碑,但是大多用于数据CRUD管理.中间层聚合和中间层代理等工具场景 ...