Springboot+Mybatisマルチデータソースの構成PageHelperページング
8753 ワード
前言
会社のプロジェクトは古いです.Netプロジェクト、現在のspringcloudに移行します.移行は比較的長いプロセスであり、企業の既存のデータソースSqlServerとMybatisの2つをもたらします.単一のデータ・ソースに対してPageHelperページング・プラグインは、ほとんど構成せずに使用できますが、デュアル・データ・ソースの切り替えにより構文エラーが発生します.
PageHelperページング・プラグインでは、デフォルトのデータベース・ソースを設定する必要がありますが、SqlServer、mysqlの一部の構文は異なります.たとえば、mysqlクエリを先に使用する場合、ページング・プラグインはデフォルトでmysql文を使用し、sqlserverを切り替えた後もmysql構文を使用してクエリに失敗します.
たとえば、最初の100匹の犬の名前を調べます.
mysql: SELECT name FROM tbl_dog LIMIT100;
sqlserver: SELECT TOP 100 name FROM tbl_dog;
解決方法:
複数のSqlSessionFactoryを構成し、複数のSqlSessionFactoryを構成して、異なるパケットパスの下にあるmapperが異なるデータソースを使用することを指定すると、異なるdaoレイヤが異なるデータソースにアクセスできます.
一.データ・ソースの構成
使用するテクノロジー:springboot、mybatis、druid、mysql、sqlserver.
2つの異なるデータベースがあることを確認します.ここではmysqlとsqlserverを例に挙げます(他のデータソースの原理は同じです).
❶ pom.xmlに関連依存プラグインをインポート
❷配置アプリケーション.yml
⑪mysql,sqlseverの構成
MySqlDataSourceConfig.JAvaファイル(mysql)
SqlServerDataSourceeConfig.JAvaファイル(sqlserver)
@MapperScanのbasePackagesで指定したmapperパスがローカルと一致していることを確認してください
❹ページングクエリー(mysqlを例に)
["VIP1","VIP2", "VIP3"]
これによりpagehelperが異なるパケットパス下のsqlに対して異なるsql処理を行うことができる.
❻アプリケーション起動クラスまずspringbootが持っているDataSourceAutoConfigurationを無効にします.アプリケーションが読み込まれるからです.propertiesファイルのspring.datasource.* プロパティを設定し、単一のデータ・ソースを自動的に構成します.@SpringBootApplication注記にexclude属性除外を追加します. システム起動時にPageHelperAutoConfigurationが自動的に登録されるため、@SpringBootApplication注記にexclude属性を追加して自動構成を除外します.
プロジェクトのアドレス:https://github.com/PureLeaves/springboot_datasource
会社のプロジェクトは古いです.Netプロジェクト、現在のspringcloudに移行します.移行は比較的長いプロセスであり、企業の既存のデータソースSqlServerとMybatisの2つをもたらします.単一のデータ・ソースに対してPageHelperページング・プラグインは、ほとんど構成せずに使用できますが、デュアル・データ・ソースの切り替えにより構文エラーが発生します.
PageHelperページング・プラグインでは、デフォルトのデータベース・ソースを設定する必要がありますが、SqlServer、mysqlの一部の構文は異なります.たとえば、mysqlクエリを先に使用する場合、ページング・プラグインはデフォルトでmysql文を使用し、sqlserverを切り替えた後もmysql構文を使用してクエリに失敗します.
たとえば、最初の100匹の犬の名前を調べます.
mysql: SELECT name FROM tbl_dog LIMIT100;
sqlserver: SELECT TOP 100 name FROM tbl_dog;
解決方法:
複数のSqlSessionFactoryを構成し、複数のSqlSessionFactoryを構成して、異なるパケットパスの下にあるmapperが異なるデータソースを使用することを指定すると、異なるdaoレイヤが異なるデータソースにアクセスできます.
一.データ・ソースの構成
使用するテクノロジー:springboot、mybatis、druid、mysql、sqlserver.
2つの異なるデータベースがあることを確認します.ここではmysqlとsqlserverを例に挙げます(他のデータソースの原理は同じです).
❶ pom.xmlに関連依存プラグインをインポート
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.alibaba
druid-spring-boot-starter
1.1.9
mysql
mysql-connector-java
5.1.39
runtime
com.microsoft.sqlserver
sqljdbc4
4.0
❷配置アプリケーション.yml
server:
port: 8079
tomcat:
uri-encoding: UTF-8
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
dmysql:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://( ip ):3306/vipdb?useUnicode=true&characterEncoding=UTF-8&useSSL=true
username: root
password: root
dsqlserver:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcUrl: jdbc:sqlserver://( ip ):49508;Databasename=VIPDB
username: root
password: root
⑪mysql,sqlseverの構成
MySqlDataSourceConfig.JAvaファイル(mysql)
@Configuration
@MapperScan(basePackages={"com.example.demo.mapper.*"}, sqlSessionFactoryRef="mysqlSessionFactory")
public class MySqlDataSourceConfig {
@Value("${spring.datasource.dmysql.type")
private String type;
@Value("${spring.datasource.dmysql.driverClassName}")
private String driverClass;
@Value("${spring.datasource.dmysql.jdbcUrl}")
private String url;
@Value("${spring.datasource.dmysql.username}")
private String username;
@Value("${spring.datasource.dmysql.password}")
private String password;
/**
*
*/
@Primary
@Bean(value = "mysqlDataSource")
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setDbType(type);
datasource.setDriverClassName(driverClass);
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
return datasource;
}
@Bean(name = "mysqlTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Primary
@Bean(name = "mysqlSessionFactory")
public SqlSessionFactory mysqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//
Interceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
//
properties.setProperty("helperDialect", "mysql");
//
properties.setProperty("reasonable", "false");
interceptor.setProperties(properties);
sessionFactory.setPlugins(new Interceptor[] {interceptor});
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
}
@Bean(name = "mysqlSessionTemplate")
@Primary
public SqlSessionTemplate mysqlSessionTemplate(@Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
SqlServerDataSourceeConfig.JAvaファイル(sqlserver)
@Configuration
@MapperScan(basePackages = {"com.example.demo.mssql.mapper.*"}, sqlSessionFactoryRef = "mssqlSessionFactory")
public class SqlServerDataSourceeConfig {
@Value("${spring.datasource.dsqlserver.type}")
private String type;
@Value("${spring.datasource.dsqlserver.driverClassName}")
private String driverClass;
@Value("${spring.datasource.dsqlserver.jdbcUrl}")
private String url;
@Value("${spring.datasource.dsqlserver.username}")
private String username;
@Value("${spring.datasource.dsqlserver.password}")
private String password;
@Bean(value = "mssqlDataSource")
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
datasource.setDbType(type);
datasource.setDriverClassName(driverClass);
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
return datasource;
}
@Bean(name = "mssqlTransactionManager")
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean(name = "mssqlSessionFactory")
public SqlSessionFactory mssqlSessionFactory(@Qualifier("mssqlDataSource") DataSource dataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//
Interceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
//
properties.setProperty("helperDialect", "sqlserver2012");
//
properties.setProperty("reasonable", "false");
interceptor.setProperties(properties);
sessionFactory.setPlugins(new Interceptor[] {interceptor});
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
}
@Bean(name = "mssqlSessionTemplate")
public SqlSessionTemplate mssqlSessionTemplate(@Qualifier("mssqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@MapperScanのbasePackagesで指定したmapperパスがローカルと一致していることを確認してください
❹ページングクエリー(mysqlを例に)
//controller
@RestController
public class VipController {
@Autowired
private VipService vipService;
@GetMapping("/vip/name")
public Object hello() {
List list = vipService.findVipNameAll();
return list;
}
}
//service
@Service
public class VipService {
@Autowired
private VipMapper vipMapper;
public List findVipNameAll() {
// , 3 , id
PageInfo pageInfo = PageHelper.startPage(1, 3,"id asc").doSelectPageInfo(()
-> vipMapper.getVipNameAll());
return pageInfo.getList();
}
}
//dao /mapper( )
@Mapper
public interface VipMapper {
@Select("SELECT name from ap_vip")
List getVipNameAll();
}
["VIP1","VIP2", "VIP3"]
これによりpagehelperが異なるパケットパス下のsqlに対して異なるsql処理を行うことができる.
❻アプリケーション起動クラス
プロジェクトのアドレス:https://github.com/PureLeaves/springboot_datasource