解决springboot在使⽤pagehelper-spring-boot-starter。。。spring boot 已经使⽤了⾃动分页插件 pagehelper-spring-boot-starter ,但是在⾃定义mybatis 的时候,并不能⽣效,原因就是,pagehelper ,进⾏拦截处理后,没有调⽤之后的,直接返回了⾃⼰的执⾏结果,导致之后的没有调⽤.
下⾯贴出我的代码,可以执⾏
添加⽂件:
MyBatisConfig.java
需要注意,添加了
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
springboot aoppackage com.fig;
import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.school.batis.MyBatisInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import t.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.List;
@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MyBatisConfig {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@PostConstruct
public void addMyInterceptor() {
MyBatisInterceptor e = new MyBatisInterceptor();
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
}
}
}
添加⽂件:
MyBatisInterceptor.java
package com.school.batis;
import com.school.oauth.aop.HttpAspect;
import llections.CollectionUtils;
import org.utor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.plugin.*;
import org.flection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.pe.TypeHandlerRegistry;
DateFormat;
import java.util.*;
import Matcher;
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class,
Object.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class,
Object.class, RowBounds.class, ResultHandler.class})})
@SuppressWarnings({"unchecked", "rawtypes"})
public class MyBatisInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
try
{
// 获取xml中的⼀个select/update/insert/delete节点,是⼀条SQL语句
MappedStatement mappedStatement = (Args()[0];
Object parameter = null;
// 获取参数,if语句成⽴,表⽰sql语句有参数,参数格式是map形式
if (Args().length > 1)
{
parameter = Args()[1];
}
String sqlId = Id(); // 获取到节点的id,即sql语句的id
System.out.println("sqlId = " + sqlId);
BoundSql boundSql = BoundSql(parameter); // BoundSql就是封装myBatis最终产⽣的sql类 Configuration configuration = Configuration(); // 获取节点的配置
String sql = getSql(configuration, boundSql, sqlId); // 获取到最终的sql语句
Map map= ();
String key=(("requestKey");
System.out.println("requestKey::"+key+" sql = " + sql);
}
catch (Exception e)
{
e.printStackTrace();
}
// 执⾏完上⾯的任务后,不改变原有的sql执⾏过程
return invocation.proceed();
}
// 封装了⼀下sql语句,使得结果返回完整xml路径下的sql语句节点id + sql语句
public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId)
{
String sql = showSql(configuration, boundSql);
/
/ StringBuilder str = new StringBuilder(100);
// str.append(sqlId);
// str.append(":");
// str.append(sql);
return sql;
}
// 如果参数是String,则添加单引号,如果是⽇期,则转换为时间格式器并加单引号;对参数是null和不是null的情况作了处理
private static String getParameterValue(Object obj)
{
String value = null;
if (obj instanceof String)
{
value = "'" + String() + "'";
}
else if (obj instanceof Date)
{
DateFormat formatter = DateTimeInstance(DateFormat.DEFAULT,
DateFormat.DEFAULT, Locale.CHINA);
value = "'" + formatter.format(new Date()) + "'";
}
else
{
if (obj != null)
{
value = String();
}
else
{
value = "";
}
}
return value;
}
/
/ 进⾏?的替换
public static String showSql(Configuration configuration, BoundSql boundSql)
{
// 获取参数
Object parameterObject = ParameterObject();
List<ParameterMapping> parameterMappings = ParameterMappings();
// sql语句中多个空格都⽤⼀个空格代替
String sql = Sql().replaceAll("[\\s]+", " ");
if (CollectionUtils.isNotEmpty(parameterMappings) && parameterObject != null)
{
// 获取类型处理器注册器,类型处理器的功能是进⾏java类型和数据库类型的转换
TypeHandlerRegistry typeHandlerRegistry = TypeHandlerRegistry();
// 如果根据Class()可以到对应的类型,则替换
if (typeHandlerRegistry.Class()))
{
sql = placeFirst("\\?",
Matcher.quoteReplacement(getParameterValue(parameterObject)));
}
else
{
// MetaObject主要是封装了originalObject对象,提供了get和set的⽅法⽤于获取和设置originalObject的属性值,主要⽀持对JavaBean、Collection、Map三种 MetaObject metaObject = wMetaObject(parameterObject);
for (ParameterMapping parameterMapping : parameterMappings)
{
String propertyName = Property();
if (metaObject.hasGetter(propertyName))
{
Object obj = Value(propertyName);
sql = placeFirst("\\?",
Matcher.quoteReplacement(getParameterValue(obj)));
}
else if (boundSql.hasAdditionalParameter(propertyName))
{
/
/ 该分⽀是动态sql
Object obj = AdditionalParameter(propertyName);
sql = placeFirst("\\?",
Matcher.quoteReplacement(getParameterValue(obj)));
}
else
{
// 打印出缺失,提醒该参数缺失并防⽌错位
sql = placeFirst("\\?", "缺失");
}
}
}
}
return sql;
}
@Override
public Object plugin(Object o) {
System.out.println("7777777777777777777777777777");
return Plugin.wrap(o, this);
}
@Override
public void setProperties(Properties properties) {
}
}
添加⽂件:
spring.factories
路径:resources/META-INF
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.fig.MyBatisConfig 启动类:
spring boot 启动类,需要注意,添加了
excludeFilters = @ComponentScan.Filter(
type = FilterType.REGEX,
pattern = "com.fig.MyBatisConfig")
OauthApplication.java
package com.school.oauth;
import com.school.oauth.db.DynamicDataSourceRegister;
import org.apache.catalina.filters.CorsFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import t.annotation.*;
import org.s.CorsConfiguration;
import org.s.UrlBasedCorsConfigurationSource;
import org.springframework.fig.annotation.CorsRegistry;
import org.springframework.fig.annotation.WebMvcConfigurerAdapter;
@Import(DynamicDataSourceRegister.class)
@SpringBootApplication
@ComponentScan(basePackages = {
"com.school.oauthmon",
"com.fig",
"com.ller",
"com.school.oauth.db",
"com.ity.pojo",
"com.school.oauth.mapper",
"com.school.oauth.service",
"com.school.oauth.util",
"com.school.oauth.aop"},
excludeFilters = @ComponentScan.Filter(
type = FilterType.REGEX,
pattern = "com.fig.MyBatisConfig"))
public class OauthApplication {
public static void main(String[] args) {
SpringApplication.run(OauthApplication.class, args);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论