我没有在父工程下新建子模块,所以坐标有所不同,下面是我的模块结构:
所需要的Maven坐标:pom.xml文件如下(注意看我的注释)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 你自己的模块组名和项目名 -->
<groupId>com.qinple</groupId>
<artifactId>qin_test01</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 这里我没有指定父工程,有父工程引用了这个坐标,就不需要重复引用 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.1.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>0.9.0</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
</dependencies>
</project>
配置类如下:
package com.qinple.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import io.seata.rm.datasource.DataSourceProxy;
import org.apache.ibatis.plugin.Interceptor;
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;
/**
* @author MyPC
*/
@Configuration
@EnableConfigurationProperties({MybatisPlusProperties.class})
public class DataSourcesProxyConfig
{
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource()
{
return new DruidDataSource();
}
/**
* 创建代理数据源
* 注解: @Primary 标识必须配置在代码数据源上,否则本地事务失效
*
* @param druidDataSource
* @return
*/
@Primary
@Bean
public DataSourceProxy dataSourceProxy(DataSource druidDataSource)
{
return new DataSourceProxy(druidDataSource);
}
private MybatisPlusProperties properties;
public DataSourcesProxyConfig(MybatisPlusProperties properties)
{
this.properties = properties;
}
/**
* 替换SqlSessionFactory的DataSource
*
* @param dataSourceProxy
* @param paginationInterceptor
* @return
* @throws Exception
*/
@Bean
public MybatisSqlSessionFactoryBean sqlSessionFactory(DataSourceProxy dataSourceProxy, PaginationInterceptor paginationInterceptor) throws Exception
{
// 这里必须用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean,否则 MyBatisPlus 不会生效
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
mybatisSqlSessionFactoryBean.setDataSource(dataSourceProxy);
mybatisSqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
//配置包扫描
mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/*.xml"));
MybatisConfiguration configuration = this.properties.getConfiguration();
if (configuration == null)
{
configuration = new MybatisConfiguration();
}
mybatisSqlSessionFactoryBean.setConfiguration(configuration);
//添加分页拦截器, 否则分页插件不生效
Interceptor[] plugins = {paginationInterceptor};
mybatisSqlSessionFactoryBean.setPlugins(plugins);
return mybatisSqlSessionFactoryBean;
}
}
其它模块要导入该模块的依赖坐标,还要有配置类才能生效:
package com.qinple.xxxx.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author MyPC
* 引用seata模块必备配置类
*/
@Configuration
@ComponentScan("com.heima.seata.config")
public class SeataConfig
{
}
其它模块的启动引导类代码展示(没有分页插件就算上面配了也不一定能成功分页,当然我没有试过):
package com.qinple.xxxxx;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
/**
* @author MyPC
* 注意排除依赖, 否则依赖会构成一个圈爆什么 cycle 圈圈错误, 除非经验丰富,否则很难解决的错误
*/
@SpringBootApplication(scanBasePackages = "com.heima.article",exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@MapperScan("com.qinple.xxxxx.mapper")
public class ArticleApplication
{
public static void main(String[] args)
{
SpringApplication.run(ArticleApplication.class,args);
}
/**
* mybatis-plus分页插件, 不要漏掉
*/
@Bean
public PaginationInterceptor paginationInterceptor()
{
return new PaginationInterceptor();
}
}
报错信息部分展示如下:
如果有什么不对的地方请指出,我会修改或删除。(如有雷同,纯属巧合)