项目集成seata和mybatis-plus,seata与mybatis-plus冲突问题(所有插件失效,自动填充失效,找不到mapper文件解决方案)

自动填充代码:

package com.from.mybatis.handler;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyBatisMetaObjectHandler implements MetaObjectHandler {
/**
* 自定义插入时填充规则
*/
@Override
public void insertFill(MetaObject metaObject) {
// 注意是类属性字段名称,不是表字段名称
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("delFlag", 0, metaObject);
}

/**
* 自定义更新时填充规则
*/
@Override
public void updateFill(MetaObject metaObject) {
String now = DateUtil.now();
// 注意是类属性字段名称,不是表字段名称
this.setFieldValByName("updateTime", new Date(), metaObject);
}

}

id生成器代码: package com.from.mybatis.config;

import java.util.Date;
import java.util.UUID;

/**
* compressed id generator, result id not great than 53bits before 2318-06-04.
*/
public class IdGenerator {

private static IdGenerator instance = new IdGenerator(0);

public static IdGenerator initDefaultInstance(int machineId) {
instance = new IdGenerator(machineId);
return instance;
}

public static IdGenerator getInstance() {
return instance;
}

public static long generateId() {
return instance.nextId();
}

// total bits=53(max 2^53-1:9007199254740992-1)

// private final static long TIME_BIT = 40; // max: 2318-06-04
private final static long MACHINE_BIT = 5; // max 31
private final static long SEQUENCE_BIT = 8; // 256/10ms

/**
* mask/max value
*/
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);

private final static long MACHINE_LEFT = SEQUENCE_BIT;
private final static long TIMESTMP_LEFT = MACHINE_BIT + SEQUENCE_BIT;

private long machineId;
private long sequence = 0L;
private long lastStmp = -1L;

private IdGenerator(long machineId) {
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
throw new IllegalArgumentException(
"machineId can't be greater than " + MAX_MACHINE_NUM + " or less than 0");
}
this.machineId = machineId;
}

/**
* generate new ID
*
* @return
*/
public synchronized long nextId() {
long currStmp = getTimestamp();
if (currStmp < lastStmp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}

if (currStmp == lastStmp) {
sequence = (sequence + 1) & MAX_SEQUENCE;
if (sequence == 0L) {
currStmp = getNextTimestamp();
}
} else {
sequence = 0L;
}

lastStmp = currStmp;

return currStmp << TIMESTMP_LEFT //
| machineId << MACHINE_LEFT //
| sequence;
}

private long getNextTimestamp() {
long mill = getTimestamp();
while (mill <= lastStmp) {
mill = getTimestamp();
}
return mill;
}

private long getTimestamp() {
// per 10ms
return System.currentTimeMillis() / 10;// 10ms
}

public static Date parseIdTimestamp(long id) {
return new Date((id >>> TIMESTMP_LEFT) * 10);
}

public static String uuid() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}
package com.from.mybatis.config;


import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.springframework.stereotype.Component;

@Component
public class CustomerIdGenerator implements IdentifierGenerator {
@Override
public Long nextId(Object entity) {
// 填充自己的Id生成器,
return IdGenerator.generateId();
}
}

具体代理配置:

package com.from.seata.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.from.mybatis.config.CustomerIdGenerator;
import com.from.mybatis.handler.MyBatisMetaObjectHandler;
import io.seata.rm.datasource.DataSourceProxy;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
@EnableConfigurationProperties({MybatisPlusProperties.class})
public class DataSourcesProxyConfig {
//自动填充注入
@Bean
public MyBatisMetaObjectHandler myBatisMetaObjectHandler() {
return new MyBatisMetaObjectHandler();
}
//id生成器注入
@Bean
public CustomerIdGenerator customerIdGenerator() {
return new CustomerIdGenerator();
}
//获取数据源
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}

//配种数据源
@Primary//@Primary标识必须配置在代码数据源上,否则本地事务失效
@Bean
public DataSourceProxy dataSourceProxy(DataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}

private MybatisPlusProperties properties;

public DataSourcesProxyConfig(MybatisPlusProperties properties) {
this.properties = properties;
}

//配置mybatisplus的数据源,所有插件会失效,所以注入进来,配置给代理数据源
@Bean
public MybatisSqlSessionFactoryBean sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
// 这里必须用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean,否则 MyBatisPlus 不会生效
MybatisSqlSessionFactoryBean sqlBean = new MybatisSqlSessionFactoryBean();
sqlBean.setDataSource(dataSourceProxy);
sqlBean.setTransactionFactory(new SpringManagedTransactionFactory());
try {
//注意:!!!!这个如果写的有sql,须有该配置,try cach以下,打开不捕获异常的化,没sql会报错
sqlBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:/mapper/**/*.xml"));
} catch (IOException ignored) {
}
MybatisConfiguration configuration = this.properties.getConfiguration();
if (configuration == null) {
configuration = new MybatisConfiguration();
}
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//向代理数据源添加分页拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
sqlBean.setPlugins(interceptor);
//代理数据源添加id生成器,字段自动填充
sqlBean.setGlobalConfig(new GlobalConfig()
.setMetaObjectHandler(myBatisMetaObjectHandler())
.setIdentifierGenerator(customerIdGenerator()));
sqlBean.setConfiguration(configuration);
return sqlBean;
}
}

关于seata服务端和客户端集成配置,可以看另一篇文章!

项目集成seata和mybatis-plus冲突问题解决方案:(分页插件失效, 自动填充失效, 自己注入的id生成器失效 找不到mapper文件解决方案)的更多相关文章

  1. Mybatis拦截器介绍及分页插件

    1.1    目录 1.1 目录 1.2 前言 1.3 Interceptor接口 1.4 注册拦截器 1.5 Mybatis可拦截的方法 1.6 利用拦截器进行分页 1.2     前言 拦截器的一 ...

  2. IDEA:Maven项目找不到mapper文件 无法自动映射

    如果你发现所有的功能都报找不到映射的错,有可能是因为mapper文件没有被编译 在eclipse中,把资源文件放在src下,是可以被编译的 但是在idea中,直接把资源文件放在src下,如果不进行设置 ...

  3. SpringBoot项目集成PageHelper使用

    SpringBoot项目集成PageHelper使用 一.开始 ​ 地址:https://github.com/pagehelper/Mybatis-PageHelper ​ 在spring boot ...

  4. spring boot集成mybatis(2) - 使用pagehelper实现分页

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  5. MyBatis学习总结_17_Mybatis分页插件PageHelper

    如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...

  6. MyBatis学习总结(17)——Mybatis分页插件PageHelper

    如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...

  7. Mybatis学习---Mybatis分页插件 - PageHelper

    1. Mybatis分页插件 - PageHelper说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,S ...

  8. Java框架之MyBatis 07-动态SQL-缓存机制-逆向工程-分页插件

    MyBatis 今天大年初一,你在学习!不学习做什么,斗地主...人都凑不齐.学习吧,学习使我快乐!除了诗和远方还有责任,我也想担当,我也想负责,可臣妾做不到啊,怎么办?你说怎么办,为啥人家能做到你做 ...

  9. 【SSM 5】Mybatis分页插件的使用

    一.添加maven依赖项 <span style="font-family:KaiTi_GB2312;font-size:18px;"><dependency&g ...

随机推荐

  1. 看动画学算法之:hashtable

    目录 简介 散列表的关键概念 数组和散列表 数组的问题 hash的问题 线性探测 二次探测 双倍散列 分离链接 rehash 简介 java中和hash相关并且常用的有两个类hashTable和has ...

  2. [cf461D]Appleman and Complicated Task

    假设该矩形是aij,那么有a(i,j)=a(i-1,j-1)^a(i-1,j+1)^a(i-2,j),不断递归下去可以发现a(i,j)=a(1,y-x+1)^a(1,y-x+3)^--^a(1,x+y ...

  3. [loj2586]选圆圈

    下面先给出比较简单的KD树的做法-- 根据圆心建一棵KD树,然后模拟题目的过程,考虑搜索一个圆 剪枝:如果当前圆[与包含该子树内所有圆的最小矩形]都不相交就退出 然而这样的理论复杂度是$o(n^2)$ ...

  4. Error occurred during initialization of VM Could not reserve enough space fo

    通过es的elasticsearch.bat 启动.发现错误:Error occurred during initialization of VM Could not reserve enough s ...

  5. 架构师必备:巧用Canal实现异步、解耦的架构

    本文介绍如何应用Canal实现异步.解耦的架构,后续有空再写文章分析Canal原理和源代码. Canal简介 Canal是用来获取数据库变更的中间件. 伪装自己为MySQL从库,拉取主库binlog并 ...

  6. Codeforces 605D - Board Game(树状数组套 set)

    Codeforces 题目传送门 & 洛谷题目传送门 事实上是一道非常容易的题 很容易想到如果 \(c_i\geq a_j\) 且 \(d_i\geq b_j\) 就连一条 \(i\to j\ ...

  7. Codeforces 704C - Black Widow(dp)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉这种题被评到 *2900 是因为细节太繁琐了,而不是题目本身的难度,所以我切掉这种题根本不能说明什么-- 首先题目中有一个非 ...

  8. IOI 2020 国家集训队作业

    \(\checkmark\) 试题一 完成情况 试题二 完成情况 试题三 完成情况 cf549E cf674G arc103_f \(\checkmark\) cf594E agc034_f agc0 ...

  9. 【Linux】非root安装Python3及其包管理

    1. Python 3.8.1安装 源码安装常规操作: wget -c https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz tar -xv ...

  10. python 新闻管理系统——启示

    mysql取整函数: mysql函数ceil.floor.round mysql 取整 1.ceil() / ceiling() 向上取整 ex: ceil(1.2) = 2 2.floor() 向下 ...