mybatis 介绍
拦截器 的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法。
mybatis 是一款优秀的持久层框架,它支持定制化 sql、存储过程以及高级映射。mybatis 避免了几乎所有的 jdbc 代码和手动设置参数以及获取结果集。mybatis 可以使用简单的 xml 或注解来配置和映射原生类型、接口和 java 的 pojo(plain old java objects,普通老式 java 对象)为数据库中的记录。
mybatis架构图
我们这个demo实现就是基于mybatis的插件模块(主要实现mybatis的interceptor接口)
interceptor接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org.apache.ibatis.plugin;
import java.util.properties;
/** * @author clinton begin */ public interface interceptor {
object intercept(invocation invocation) throws throwable;
object plugin(object target);
void setproperties(properties properties); } |
demo实现
主要技术 spring boot + mybatis
pom.xml
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version> 1.3 . 2 </version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> |
数据库 ddl
1 2 3 4 5 |
create table user ( id int auto_increment primary key, username varchar( 20 ) null ); |
核心代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
@override public object intercept(invocation invocation) throws throwable { logger.info( "进入拦截器" ); object[] args = invocation.getargs(); mappedstatement mappedstatement = (mappedstatement) args[ 0 ];
//获取参数 object param = invocation.getargs()[ 1 ]; boundsql boundsql = mappedstatement.getboundsql(param); object parameterobject = boundsql.getparameterobject();
/** * 判断是否是继承pagevo来判断是否需要进行 分页 */ if (parameterobject instanceof pagevo) { //强转 为了拿到分页数据 pagevo pagevo = (pagevo) param; string sql = boundsql.getsql();
//获取相关配置 configuration config = mappedstatement.getconfiguration(); connection connection = config.getenvironment().getdatasource().getconnection();
//拼接查询当前条件的sql的总条数 string countsql = "select count(*) from (" + sql + ") a" ; preparedstatement preparedstatement = connection.preparestatement(countsql); boundsql countboundsql = new boundsql(config, countsql, boundsql.getparametermappings(), boundsql.getparameterobject()); parameterhandler parameterhandler = new defaultparameterhandler(mappedstatement, parameterobject, countboundsql); parameterhandler.setparameters(preparedstatement); //执行获得总条数 resultset rs = preparedstatement.executequery(); int count = 0 ; if (rs.next()) { count = rs.getint( 1 ); } //拼接分页sql string pagesql = sql + " limit " + pagevo.getlimit() + " , " + pagevo.getoffset(); //重新执行新的sql donewsql(invocation, pagesql);
object result = invocation.proceed(); connection.close(); //处理新的结构 pageresult<?> pageresult = new pageresult<list>(pagevo.page, pagevo.rows, count, (list) result); list<pageresult> returnresultlist = new arraylist<>(); returnresultlist.add(pageresult);
return returnresultlist; } return invocation.proceed(); } |
测试结果
github地址: https://github测试数据/xinyanjiang/mybaits-plugs
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
查看更多关于mybatis拦截器与分页插件实例教程的详细内容...