Commit 56ef0c88 by lei wang

多数据源适配:部分代码

parent 0241dac4
...@@ -128,6 +128,11 @@ ...@@ -128,6 +128,11 @@
<artifactId>ojdbc6</artifactId> <artifactId>ojdbc6</artifactId>
<version>12.1.0.2</version> <version>12.1.0.2</version>
</dependency> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies> </dependencies>
......
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.brilliance.mda.support.mybatis.dync.mapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@Component
public class DbExecuteMapper {
@Autowired
@Resource(name = "sqlSessionTemplate")
SqlSessionTemplate sqlSessionTemplate;
@Autowired
@Resource(name = "sqlSessionTemplate2")
SqlSessionTemplate sqlSessionTemplate2;
final String FNAME = this.getClass().getName();
public DbExecuteMapper() {
}
public List<Map<String, Object>> dyncReadForMap(Map<String, Object> params) {
return this.sqlSessionTemplate.selectList(this.FNAME + ".dyncReadForMap", params);
}
public int dyncUpdateForMap(Map<String, Object> params) {
return this.sqlSessionTemplate.update(this.FNAME + ".dyncUpdateForMap", params);
}
}
package com.ceb.gjjs.mda.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
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 javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
@MapperScan(basePackages = "com.ceb.gjjs.mda.dao", sqlSessionTemplateRef = "sqlSessionTemplate")
public class DataSourceConfiguration {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid")
public DataSource getDataSource() throws SQLException {
return new DruidDataSource();
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory getSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean();
sqlfb.setDataSource(dataSource);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/orm/mda/*.xml"));
return sqlfb.getObject();
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager getTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplate")
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
\ No newline at end of file
package com.ceb.gjjs.mda.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
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 javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
@MapperScan(basePackages = "com.ceb.gjjs.mda.dao", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class DataSourceConfiguration2 {
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "spring.datasource2")
public DataSource getDataSource() throws SQLException {
return new DruidDataSource();
}
@Bean(name = "sqlSessionFactory2")
@Primary
public SqlSessionFactory getSqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean();
sqlfb.setDataSource(dataSource);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/orm/mda/*.xml"));
return sqlfb.getObject();
}
@Bean(name = "transactionManager2")
@Primary
public DataSourceTransactionManager getTransactionManager(@Qualifier("dataSource2")DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplate2")
@Primary
public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("sqlSessionFactory2")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.brilliance.mda.support.mybatis.count.mapper.CounterMapper">
<insert id="insertNewCounter" >
insert into COU(COUNAM,COUVAL,COUSTP) values(#{nam},#{start},#{stp})
</insert>
<select id="getCountValWithUpdate" resultType="java.lang.Integer">
select COUVAL from COU where COUNAM = #{nam} for update
</select>
<update id="updateCounter" >
update COU set COUVAL = COUVAL + COUSTP where COUNAM = #{nam}
</update>
<select id="seqNextval" resultType="java.lang.Integer" >
select ${value}.nextval from dual
</select>
<select id="dbCounter" resultType="java.lang.Integer">
select f_dbcounter(#{seqName}) from dual;
</select>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.brilliance.mda.support.mybatis.dync.mapper.DbExecuteMapper">
<select id="dyncReadForMap" resultType="java.util.LinkedHashMap">
${sql}
</select>
<update id="dyncUpdateForMap">
${sql}
</update>
<select id="dbDate" resultType="java.util.Date">
select sysdate from dual
</select>
</mapper>
\ No newline at end of file
...@@ -44,6 +44,12 @@ spring: ...@@ -44,6 +44,12 @@ spring:
port: 6379 port: 6379
password: password:
datasource2:
url: jdbc:mysql://localhost:3306/cfix?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
envConfig: envConfig:
rootPath: workRoot rootPath: workRoot
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
<itext-asian.version>5.2.0</itext-asian.version> <itext-asian.version>5.2.0</itext-asian.version>
<xmlworker.version>5.5.8</xmlworker.version> <xmlworker.version>5.5.8</xmlworker.version>
<mybatis.spring.boot.starter.version>2.1.4</mybatis.spring.boot.starter.version> <mybatis.spring.boot.starter.version>2.1.4</mybatis.spring.boot.starter.version>
<mysql.connector.java.version>5.1.29</mysql.connector.java.version> <mysql.connector.java.version>8.0.29</mysql.connector.java.version>
<poi.ooxml.version>4.1.0</poi.ooxml.version> <poi.ooxml.version>4.1.0</poi.ooxml.version>
<commons.io.version>2.6</commons.io.version> <commons.io.version>2.6</commons.io.version>
<commons.codec.version>1.10</commons.codec.version> <commons.codec.version>1.10</commons.codec.version>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment