关键词:
程序员都是些折了翼的天使
来看看我们这个项目中是如何对mybatis动态生成sql进行改进的吧
spring boot在配置了MapperScan之后会自动扫描相关的包,并对有@Mapper标记的类进行注册
@Configuration
@EnableAutoConfiguration
@ServletComponentScan
@ComponentScan("xxxx.scm","xxxxx.base")
@MapperScan("xxxxx.dao","xxxxx.mapper")
public class Application
/**
* 主函数入口
*
* @param args 参数
*/
public static void main(String[] args)
Security.addProvider(new BouncyCastleProvider());
SpringApplication.run(Application.class, args);
在dao中可以预定义基础类,比如拦截器、基础Mapper、基础Provider等。
//拦截器
@Intercepts(
@Signature(type = Executor.class, method = "query", args = MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class),
@Signature(type = Executor.class, method = "query", args = MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class),
@Signature(type = Executor.class, method = "update", args = MappedStatement.class, Object.class),
)
@Component
public class BaseInterceptor implements Interceptor
@Override
public Object intercept(Invocation invocation) throws Throwable
StatementUtils.genCommonStatement((MappedStatement)(invocation.getArgs()[0])); // 生成通用查询语句
return invocation.proceed();
@Override
public Object plugin(Object o)
return Plugin.wrap(o, this);
@Override
public void setProperties(Properties properties)
这个是mybatis拦截器的标准写法,实现Interceptor接口的三个方法。可以拦截的接口一共有四种,用@Signature注解进行标记。因为Executor接口的query方法有两种不同的实现方式,所以需要拦截两种。具体拦截的内容实现放在StatementUtils中,避免因代码过长而产生维护上面的困难。
public static synchronized void genCommonStatement(MappedStatement mappedStatement)
Configuration configuration = mappedStatement.getConfiguration();//取出当前sql语句的Configuration
String msId = mappedStatement.getId();//取出当前msid
int lastIndex = msId.lastIndexOf(".");
String methodName = msId.substring(lastIndex + 1);//获取当前sql的方法名
String interfaceName = msId.substring(0, lastIndex);//获取接口名(Mapper)
Class<?> mapperClass = null;
try
mapperClass = Class.forName(interfaceName);
catch (Exception ex)
ex.printStackTrace();
Class entityClass = findEntityFromMapper(mapperClass);//获取实体
List<SqlNode> sqlNodes = null;
// 替换 ResultMap
setResultMap(entityClass, mappedStatement);
// 替换通用查询语句的 sqlSource
switch (methodName)
case "add":
sqlNodes = getInsert(entityClass, mappedStatement);
break;
case "update":
sqlNodes = getUpdate(entityClass, mappedStatement);
break;
case "find":
sqlNodes = getSelect(entityClass, mappedStatement, false);
case "findOne":
sqlNodes = getSelect(entityClass, mappedStatement, true);
break;
case "delete":
sqlNodes = getDelete(entityClass, mappedStatement);
break;
case "count":
sqlNodes = getCount(entityClass, mappedStatement);
break;
default:
break;
// TODO 注入通用参数
// TODO 注入用户自定义参数
if (sqlNodes == null)
return;
DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(configuration, new MixedSqlNode(sqlNodes));
MetaObject metaObject = SystemMetaObject.forObject(mappedStatement);
metaObject.setValue("sqlSource", dynamicSqlSource);
这段代码通过使用反射技术,从传入参数中拿到sqlnode,然后根据条件对sqlnode进行重构,然后重新放入到被拦截的执行语句中。从而实现了动态生成sql。值得一提的是,如果sql事先不存在,将会产生空指针异常的错误。由于生产环境是并发环境,所以该方法是一个同步方法,有点消耗性能。不过,由于同一时间被拦截语句可能有多条,这么做也是为了避免插入混乱的情况。如果后期数据存取方面性能有所影响,这里需要修改。为了避免空指针异常的问题,需要给生成默认的sql,拦截器只能修改不能无中生有,这是需要注意的一点。
用拦截器动态生成sql的缺点:不是所有的情况都很简单,有些情况比较复杂,无法和普通情况下的一起进行拦截
springboot集成mybatis通用mapper使用总结
springboot集成MyBatis通用Mapper使用总结2019年 参考资料:Springboot集成MyBatis通用MapperSpringBoot框架之通用mapper插件(tk.mybatis)springboot如何优雅的使用mybatis-spring-boot-starter三分钟让你看懂Springboot中Mybatis的使用SpringB 查看详情
springboot使用·下篇(springboot集成mybatis+日志打印+mybatis-plus)(代码片段)
文章目录SpringBoot的使用SpringBoot集成MyBatis日志打印MyBatis-plus的简单搭建SpringBoot的使用我们在SpringBoot使用·上篇说到了SpringBoot注解、SpringBoot集成JDBC、SpringBoot集成druid(德鲁伊)数据源和sql监控,这篇文章我们会写到Sp... 查看详情
springboot集成mybatis
SpringBoot 集成MyBatis1、创建数据库和表,创建对应的domain对象 2、添加依赖<!--mybatisspringboot集成--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spri 查看详情
springboot集成mybatis
1.在pom中添加依赖:#mybatis依赖<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version& 查看详情
springboot系列教程七:springboot集成mybatis
一.创建项目 项目名称为“springboot_mybatis_demo”,创建过程中勾选“Web”,“MyBatis”,“MySQL”,第一次创建Maven需要下载依赖包(耐心等待) 二.实现2.1创建User类1packagecom.woniu.bean;234publicclassUser{5pr... 查看详情
springboot整合mybatis开发
1创建项目,在启动类上添加映射扫描注解 2导入依赖,添加mybatis generator自动生成代码插件<!--mybatisgenerator自动生成代码插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybat 查看详情
springboot集成mybatis
Springboot集成mybatis 一、导入Mybatis、JDBC、MySQL的starter 二、导入dbcp2或者c3p0的连接池依赖<dependency><groupId>org.apache.commons</groupId><artifactId>commons-dbcp2</artifactId& 查看详情
springboot集成mybatis简单过程
springboot集成mybatis简单过程:1、在pox.xml中加入mybatis的依赖。<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2 查看详情
springboot+mybatis集成搭建
本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了xml的其实也可以用xml来实现的,实现上具体用什么方式并不重要,主要是搭建一... 查看详情
springboot-简单集成mybatis
SpringBoot集成Mybatis:---------------------------------------1、新建一个新的MavenProject;2、需要在pom.xml文件添加相应的依赖,比如:mysql驱动;PageHelper分页插件,需要版本号;3、编写启动类,大部分和之前的代码是一样的,需要的注意的是... 查看详情
springboot集成mybatis进行开发
1.首先创建springboot项目点击:http://start.spring.io/ 可以在线创建springboot项目2.加入mybatis的pom文件<dependency><groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-st 查看详情
springboot集成mybatis完整实例
步骤:添加Mybatis依赖:添加数据库依赖:配置属性文件:(具体的属性名称可以在jar包中找到)内容:建表sql:Mapper文件的头:集成Mybatis的配置文件中的具体内容可以在mybatis.spring.boot:mybatis-spring-boot-autoconfigure的jar包中找到:Ma... 查看详情
springboot学习----springboot集成mybatis
springboot作为一个一站式开发工具怎么能少了与其他框架的兼容。这里将mybatis和springboot整合起来。 1.新建一个项目 这里我们的选择如下: 如果已经有一个项目,我们可以在pom.xml文件中添加以下依赖。 <... 查看详情
springboot集成mybatis
1,添加依赖<!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version>&l 查看详情
springboot集成mybatis
配置pom.xml:<dependency><!--mybatis--><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version&g 查看详情
springboot(2.1.9.release)集成mybatis
这篇文章主要讲解SpringBoot集成MyBatis实现一个最基本的增删改查功能,并连接访问数据库。整合之前你需要随便准备一个数据表就行。SpringBoot集成MyBatis非常简单,不需要Spring繁琐的配置,也不需要配置类就能够快速集成。&n... 查看详情
springboot集成mybatis+pagehelper
1、Springboot项目引入mysql和mybatis的依赖:<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.0</versio 查看详情
springboot(11):集成mybatis
一、添加依赖<!--mybatis--><dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId>&n 查看详情