依赖

  正常情况下,在原有依赖基础上增加的 mapper-spring。

<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring</artifactId>
<version>1.0.5</version>
</dependency>

  如果想使用其他版本的依赖文件,可以在Maven仓库上搜索“tk.mybatis”。

配置 

  MapperScannerConfigurer xml

<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="tk.mybatis.mapper.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
</value>
</property>
</bean>

  @MapperScan 注解

  Spring Boot 环境中使用 application.properties] 配置文件

  在 Spring Boot 中使用 Mapper 时,如果选择使用注解方式,可以不引入 mapper-starter 依赖。

  特别提醒:Spring Boot 中常见的是配置文件方式,使用环境变量或者运行时的参数都可以配置,这些配置都可以对通用 Mapper 生效。

  在 propertie 配置中:

mapper.mappers=tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.Mapper2
mapper.not-empty=true

  tk.mybatis.mapper.session.Configuration 配置

  使用要求:MyBatis (3.4.0+) 和 mybatis-spring (1.3.0+)

  注意该类的包名,这个类继承了 MyBatis 的 Configuration 类,并且重写了 addMappedStatement 方法,如下:

@Override
public void addMappedStatement(MappedStatement ms) {
try {
super.addMappedStatement(ms);
//在这里处理时,更能保证所有的方法都会被正确处理
if (this.mapperHelper != null) {
this.mapperHelper.processMappedStatement(ms);
}
} catch (IllegalArgumentException e) {
//这里的异常是导致 Spring 启动死循环的关键位置,为了避免后续会吞异常,这里直接输出
e.printStackTrace();
throw new RuntimeException(e);
}
}

  tk.mybatis.mapper.session.Configuration 提供了 3 种配置通用 Mapper 的方式,如下所示:

/**
* 直接注入 mapperHelper
*
* @param mapperHelper
*/
public void setMapperHelper(MapperHelper mapperHelper) {
this.mapperHelper = mapperHelper;
} /**
* 使用属性方式配置
*
* @param properties
*/
public void setMapperProperties(Properties properties) {
if (this.mapperHelper == null) {
this.mapperHelper = new MapperHelper();
}
this.mapperHelper.setProperties(properties);
} /**
* 使用 Config 配置
*
* @param config
*/
public void setConfig(Config config) {
if (mapperHelper == null) {
mapperHelper = new MapperHelper();
}
mapperHelper.setConfig(config);
}

  这里直接配置一个 tk 中提供的 Configuration,然后注入到 SqlSessionFactoryBean 中。

  使用 tk.mybatis.mapper.session.Configuration 和 Spring 集成

@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
//创建 Configuration,设置通用 Mapper 配置
tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration();
//有 3 种配置方式
configuration.setMapperHelper(new MapperHelper());
sessionFactory.setConfiguration(configuration); return sessionFactory.getObject();
}

代码演示  

  创建一个超类接口,继承通用mapper内部的所有方法,然后就可以直接调用了。

public interface BaseMapper<T> extends InsertSelectiveMapper<T>, UpdateByExampleSelectiveMapper<T>, UpdateByPrimaryKeySelectiveMapper<T>,
SelectOneMapper<T>, SelectByPrimaryKeyMapper<T>, SelectMapper<T>, SelectByExampleMapper<T>, SelectByExampleRowBoundsMapper<T>,
SelectCountByExampleMapper<T> {}

  下面介绍一下通用Mapper的内置方法

  countByExample --- 根据条件查询数量 

int countByExample(UserExample example);
//完整案例
UserExample example=new UserExample();
Criteria criteria = example.createCriteria();
criteria.andAgeEqualTo(23);
int count=userDAO.countByExample(example);
//相当于:select count(*) from user where age=23;

  deleteByExample  --- 根据条件删除多条

int deleteByExample(AccountExample example);

//完整的案例

UserExample example = new UserExample();

 Criteria criteria = example.createCriteria();

 criteria.andUsernameEqualTo("joe");

 userDAO.deleteByExample(example);

 //相当于:delete from user where username='joe'

  deleteByPrimaryKey ---根据主键删除

int deleteByPrimaryKey(Integer id);

//完整案例
userDAO.deleteByPrimaryKey(101); //相当于:delete from user where id=101

  insertSelective --- 插入数据

int insertSelective(Account record);
//完整的案例
User user = new User();
user.setUsername("test");
user.setPassword("123456")
user.setEmail("674531003@qq.com");
userDAO.insertSelective(user);
//相当于:insert into user(username,password,email) values('test','123456','674531003@qq.com');

  selectByExample --- 根据条件查询数据

 List<Account> selectByExample(AccountExample example);
//完整的案例
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?> list = userDAO.selectByExample(example);
//相当于:select * from user where username = 'joe' and username is null order by username asc,email desc
//注:在myBatis 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。

  selectByPrimaryKey --- 根据主键查询数据

Account selectByPrimaryKey(Integer id);
//相当于select * from user where id = id

  updateByExampleSelective --- 按条件更新值不为null的字段

int updateByExampleSelective(@Param("record") Account record, @Param("example") AccountExample example);
 //完整的案列
UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
User user = new User();
user.setPassword("123"); userDAO.updateByPrimaryKeySelective(user,example);
//相当于:update user set password='123' where username='joe'

  updateByPrimaryKeySelective --- 根据主键更新

int updateByPrimaryKeySelective(Account record);
 //完整的案例 
User user = new User();
user.setId(101);
user.setPassword("joe");
userDAO.updateByPrimaryKeySelective(user);
//相当于:update user set password='joe' where id=101

最后补一张从网上盗的关于Example的图

 

Mybatis 通用 Mapper 和 Spring 集成的更多相关文章

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

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

  2. Spring boot集成 MyBatis 通用Mapper

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

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

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

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

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

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

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

  6. (一 、上)搭建简单的SpringBoot + java + maven + mysql + Mybatis+通用Mapper 《附项目源码》

    最近公司一直使用 springBoot 作为后端项目框架, 也负责搭建了几个新项目的后端框架.在使用了一段时间springBoot 后,感觉写代码 比spring 更加简洁了(是非常简洁),整合工具也 ...

  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. Mybatis通用Mapper介绍和使用

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

随机推荐

  1. 洛谷P1258 小车问题(题解)

    https://www.luogu.org/problemnew/show/P1258(题目传送) 看题的第一眼就把题归为二分题,一直向着二分的方向走,却忽略了数学的推理.推理一番后(看了题解),发现 ...

  2. 映像文件工具srec

    目录 映像文件工具srec 介绍与帮助 常用例子 常用选项 一个实际的例子 hex转bin 数据填充 文件合并 文件分割 加入CRC 查看信息 使用命令集合的文本 详细文件格式的描述 附录:MDK的例 ...

  3. linux usb总线驱动(一)

    目录 linux usb总线驱动框架 USB 介绍 传输类型 控制器接口 2440接口 基本流程 alloc_dev choose_address hub_port_init usb_get_devi ...

  4. I/O模型之一:Unix的五种I/O模型

    目录: <I/O模型之一:Unix的五种I/O模型> <I/O模型之二:Linux IO模式及 select.poll.epoll详解> <I/O模型之三:两种高性能 I ...

  5. spring boot打包

    1.application继承SpringBootServletInitializer  重写configure方法如下图 2.去掉项目中多余的main方法 3.将pom.xml打包改为war包 4. ...

  6. Shiro 系列: 简单命令行程序示例

    在本示例中, 使用 INI 文件来定义用户和角色. 首先学习一下 INI 文件的规范. =======================Shiro INI 的基本规范================== ...

  7. 数据结构Java实现03----栈:顺序栈和链式堆栈

    一.堆栈的基本概念: 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...

  8. 深入理解 LINQ to SQL 生成的 SQL 语句

    Ø  简介 在 C# 中与数据交互最常用的语句就是 LINQ 了,而 LINQ to SQL 是最直接与数据库打交道的语句,它可以根据 LINQ 语法生成对应的 SQL 语句,在数据库中去执行.本文主 ...

  9. 使用PHP中的ajax做登录页面、验证用户名是否可用、动态调用数据库

    1.ajax的基础知识 ajax是结合了jquery.php等几种技术延伸出来的综合运用的技术,不是新的内容.ajax也是写在<script>标签里面的. 如果使用ajax一定是要有1个处 ...

  10. 【归纳】正则表达式及Python中的正则库

    正则表达式 正则表达式30分钟入门教程 runoob正则式教程 正则表达式练习题集(附答案) 元字符\b代表单词的分界处,在英文中指空格,标点符号或换行 例子:\bhi\b可以用来匹配hi这个单词,且 ...