修改misboot-mdata源代码
config目录下增加两个配置文件
- DefaultDataSourceConfig.java
package com.misboot.mdata.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@MapperScan(basePackages = "com.misboot.mdata.mapper.mdata", sqlSessionTemplateRef = "defaultSqlSessionTemplate")
public class DefaultDataSourceConfig {
@Bean(name = "defaultDataSource")
//下面这个注解控制哪个实例优先被注入,我们放在第一个数据源上面
@Primary
@ConfigurationProperties(prefix = "spring.datasource.default")
public DataSource dataSource() {
return new DruidDataSource();
}
@Bean(name = "defaultSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("defaultDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
Properties propertiess = new Properties();
propertiess.put("prefix", "");
propertiess.put("blobType", "BLOB");
propertiess.put("boolValue", "TRUE");
bean.setConfigurationProperties(propertiess);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/misboot/mapper/mdata/*.xml"));
//分页插件
PageInterceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
//数据库
properties.setProperty("helperDialect", "mysql");
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("count", "countSql");
interceptor.setProperties(properties);
bean.setPlugins(new PageInterceptor[] {interceptor});
return bean.getObject();
}
@Bean(name = "defaultTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("defaultDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "defaultSqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("defaultSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- SqlServerDataSourceConfig.java
package com.misboot.mdata.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@MapperScan(basePackages = "com.misboot.mdata.mapper.sqlserver", sqlSessionTemplateRef = "sqlserverSqlSessionTemplate")
public class SqlServerDataSourceConfig {
@Bean(name = "sqlserverDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sqlserverdatasource")
public DataSource dataSource() {
return new DruidDataSource();
}
// https://blog.csdn.net/qq_38941455/article/details/81085513
@Bean(name = "sqlserverSqlSessionFactory")
//@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
Properties propertiess = new Properties();
propertiess.put("prefix", "");
propertiess.put("blobType", "BLOB");
propertiess.put("boolValue", "TRUE");
bean.setConfigurationProperties(propertiess);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/misboot/mapper/sqlserver/*.xml"));
//分页插件
PageInterceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
//数据库
properties.setProperty("helperDialect", "sqlserver");
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("count", "countSql");
interceptor.setProperties(properties);
bean.setPlugins(new PageInterceptor[] {interceptor});
return bean.getObject();
}
@Bean(name = "sqlserverTransactionManager")
//@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("sqlserverDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlserverSqlSessionTemplate")
//@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlserverSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- 修改model文件夹
- 修改mapper文件
- 修改mapper.xml文件