Commit cab46ca7 by gechengyang

提交多数据源runtime切换初版

parent af8e55c1
......@@ -155,6 +155,11 @@
<scope>system</scope>
<systemPath>${pom.basedir}/lib/ojdbc6-12.1.0.2.jar</systemPath>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
</dependencies>
......
package com.brilliance.mda.runtime.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.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.*;
/**
* 数据库配置
*/
@Configuration
public class DataSourceConfig {
@Value("${spring.datasource.eibs.jdbc-url}")
private String eibsdbUrl;
@Value("${spring.datasource.eibs..username}")
private String eibsusername;
@Value("${spring.datasource.eibs..password}")
private String eibspassword;
@Value("${spring.datasource.eibs.driver-class-name}")
private String eibsdriverClassName;
/**
* 数据源1
* spring.datasource.db1:application.properteis中对应属性的前缀
* @return
*/
@Bean(name = "datasource_eibs")
// @ConfigurationProperties(prefix = "spring.datasource.eibs")
// @Bean(name = "db1")
// @ConfigurationProperties(prefix = "spring.datasource.db1")
// public DataSource dataSourceEibs() {
// return DataSourceBuilder.create().build();
// }
public DataSource dataSourceEibs(){
System.out.println("----------------主配" + eibsdbUrl);
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(eibsdbUrl);
datasource.setUsername(eibsusername);
datasource.setPassword(eibspassword);
datasource.setDriverClassName(eibsdriverClassName);
// datasource.setValidationQuery(validationQuery);
// setDruidOptions(datasource); // 设置druid数据源的属性
return datasource;
}
/**
* 数据源2
* spring.datasource.db2:application.properteis中对应属性的前缀
* @return
*/
//@Bean(name = "db2")
//@ConfigurationProperties(prefix = "spring.datasource.db2")
// @ConfigurationProperties(prefix = "spring.dataource.swd")
// public DataSource dataSourceSwd() {
//
// return DataSourceBuilder.create().build();
//// System.out.println("swdDataSource="+properties);
//// return DataSourceBuilder.create(properties.getClassLoader())
//// //.type(HikariDataSource.class)
//// .driverClassName(properties.determineDriverClassName())
//// .url(properties.determineUrl())
//// .username(properties.determineUsername())
//// .password(properties.determinePassword())
//// .build();
// }
@Value("${spring.datasource.swd.jdbc-url}")
private String swdUrl;
@Value("${spring.datasource.swd..username}")
private String swdusername;
@Value("${spring.datasource.swd..password}")
private String swdpassword;
@Value("${spring.datasource.swd.driver-class-name}")
private String swddriverClassName;
@Bean(name = "datasource_swd")
public DataSource dataSourceSwd() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(swdUrl);
datasource.setUsername(swdusername);
datasource.setPassword(swdpassword);
datasource.setDriverClassName(swddriverClassName);
// datasource.setValidationQuery(validationQuery);
// setDruidOptions(datasource); // 设置druid数据源的属性
return datasource;
}
/**
* 数据源3
* spring.datasource.db2:application.properteis中对应属性的前缀
* @return
*/
//@Bean(name = "db2")
//@ConfigurationProperties(prefix = "spring.datasource.db2")
@Value("${spring.datasource.swd.jdbc-url}")
private String fxdUrl;
@Value("${spring.datasource.swd..username}")
private String fxdusername;
@Value("${spring.datasource.swd..password}")
private String fxdpassword;
@Value("${spring.datasource.swd.driver-class-name}")
private String fxddriverClassName;
@Bean(name = "datasource_fxd")
public DataSource dataSourceFxd() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(fxdUrl);
datasource.setUsername(fxdusername);
datasource.setPassword(fxdpassword);
datasource.setDriverClassName(fxddriverClassName);
// datasource.setValidationQuery(validationQuery);
// setDruidOptions(datasource); // 设置druid数据源的属性
return datasource;
}
// @ConfigurationProperties(prefix = "spring.dataource.fxd")
// public DataSource dataSourceFxd() {
// //System.out.println("fxdDataSource="+properties);
//
// return DataSourceBuilder.create().build();
//
// }
/**
* 动态数据源: 通过AOP在不同数据源之间动态切换
* @return
*/
@Primary
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 默认数据源
dynamicDataSource.setDefaultTargetDataSource(dataSourceEibs());
// 配置多数据源
Map<Object, Object> dsMap = new HashMap<>();
dsMap.put("eibs", dataSourceEibs());
dsMap.put("swd", dataSourceSwd());
dsMap.put("fxd", dataSourceFxd());
dynamicDataSource.setTargetDataSources(dsMap);
return dynamicDataSource;
}
/**
* 配置多数据源后IOC中存在多个数据源了,事务管理器需要重新配置,不然器不知道选择哪个数据源
* 事务管理器此时管理的数据源将是动态数据源dynamicDataSource
* 配置@Transactional注解
* @return
*/
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dynamicDataSource());
}
@Bean(name = "SqlSessionFactory_eibs")
@Primary
public SqlSessionFactory getEibs(@Qualifier("datasource_eibs") 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"));
//sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
sqlfb.setMapperLocations(resolveMapperLocations());
return sqlfb.getObject();
}
// @Bean(name = "transactionManager")
// @Primary
// public DataSourceTransactionManager getTransactionManager1(@Qualifier("datasource_eibs")DataSource dataSource) {
// return new DataSourceTransactionManager(dataSource);
// }
@Bean(name = "eibs")
@Primary
public SqlSessionTemplate getSqlSessionTemplate1(@Qualifier("SqlSessionFactory_eibs")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
//-----------------------------------------
@Bean(name = "SqlSessionFactory_swd")
public SqlSessionFactory getSqlSessionFactory2(@Qualifier("datasource_swd") 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"));
// sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
sqlfb.setMapperLocations(resolveMapperLocations());
return sqlfb.getObject();
}
//
// @Bean(name = "transactionManager2")
// public DataSourceTransactionManager getTransactionManager2(@Qualifier("datasource_swd")DataSource dataSource) {
// return new DataSourceTransactionManager(dataSource);
// }
@Bean(name = "swd")
public SqlSessionTemplate getSqlSessionTemplate2(@Qualifier("SqlSessionFactory_swd")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "SqlSessionFactory_fxd")
public SqlSessionFactory getSqlSessionFactory3(@Qualifier("datasource_fxd") DataSource dataSource) throws Exception{
SqlSessionFactoryBean sqlfb = new SqlSessionFactoryBean();
sqlfb.setDataSource(dataSource);
// sqlfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sqlfb.setMapperLocations(resolveMapperLocations());
// sqlfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return sqlfb.getObject();
}
//
// @Bean(name = "transactionManager3")
// public DataSourceTransactionManager getTransactionManager3(@Qualifier("datasource_fxd")DataSource dataSource) {
// return new DataSourceTransactionManager(dataSource);
// }
@Bean(name = "fxd")
public SqlSessionTemplate getSqlSessionTemplate3(@Qualifier("SqlSessionFactory_fxd")SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
private Resource[] resolveMapperLocations() {
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<String> mapperLocations = new ArrayList<>();
mapperLocations.add("classpath:META-INF/orm/mda/*.xml");
mapperLocations.add("classpath:mapper/*.xml");
List<Resource> resources = new ArrayList();
if (mapperLocations != null) {
for (String mapperLocation : mapperLocations) {
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
// ignore
}
}
}
return resources.toArray(new Resource[resources.size()]);
}
}
package com.brilliance.mda.runtime.mda.config;
import com.brilliance.mda.runtime.mda.util.DataSourceUtil;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 动态数据源类
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceUtil.getDB();
}
}
package com.brilliance.mda.runtime.mda.util;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
/**
* 数据源切换工具
*/
public class DataSourceUtil {
/**
* 默认数据源
*/
//TODO 这里默认数据源需要动态生成
public static final String DEFAULT_DS = "eibs";
/**
* 数据源属于一个公共的资源
* 采用可以保证在多线程情况下线程隔离
*/
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
/**
* 设置数据源名
* @param dbType
*/
public static void setDB(String dbType) {
contextHolder.set(dbType);
}
/**
* 获取数据源名
* @return
*/
public static String getDB() {
String dbName= (contextHolder.get()) ;
if(MdaUtils.isEmpty(dbName))
return DEFAULT_DS;
return dbName;
}
/**
* 清除数据源名
*/
public static void clearDB() {
contextHolder.remove();
}
}
\ No newline at end of file
package com.brilliance.mda.support.mybatis;
import com.brilliance.mda.runtime.mda.*;
import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.impl.AbstractDaoSession;
import com.brilliance.mda.runtime.mda.impl.Argument;
import com.brilliance.mda.runtime.mda.util.DataSourceUtil;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.support.mybatis.count.CounterService;
import com.brilliance.mda.support.mybatis.dync.mapper.DbExecuteMapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.*;
import static com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.AdaType.DELETE;
import static com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.AdaType.SELECT;
import static com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.HOLDER_SQL;
/**
* 基于Spring SqlSessionTemplate的实现.需要满足以下要求:
* 1,src/main/resources/mapper中的xml文件需要加入到mapper文件的扫描路径中
* 2,所有mybatis的mapper.xml文件namespace命名规则遵循
* "[namespacePackage].[BO.class.getSimpleName()]Mapper"
* namespacePackage : 类属性
* BO.class.getSimpleName(): BO类的名称
* <p>
* 3,所有的mybatis的mapper.xml文件中包含以下两类sql模板
* a,基于Mybatis官方Generator生成的: insert,updateByPrimaryKey,delete
* b,特殊的:
* deleteByIds(采用in方式删除主键匹配)
* <delete id="deleteByIds" parameterClass="java.util.List">
* sql...
* </delete>
* <p>
* dyncRead(实现Argument方式的动态查询)
* <select id="dyncRead" >
* ${sql}
* </select>
* <p>
* dyncDelete(实现Argument方式的动态删除)
* <delete id="dyncDelete" >
* ${sql}
* </delete>
*
* @author gechengyang
*/
@Component
public class MultiMyBatisDaoSession extends AbstractDaoSession implements IDaoSession {
private static Logger log = LoggerFactory.getLogger(MultiMyBatisDaoSession.class);
private static ThreadLocal<ResultSet> resultLocal = new ThreadLocal<>();
//定义mapper文件中的sql语句id
private static final String SQLID_INSERT = "insert";
private static final String SQLID_UPDATE = "updateByPrimaryKey";
private static final String SQLID_DELETE = "deleteByPrimaryKey";
private static final String SQLID_DELBYIDS = "deleteByIds";
private static final String SQLID_DYNCREAD = "dyncRead";
private static final String SQLID_DYNCDEL = "dyncDelete";
@Autowired
CounterService counter;
@Autowired
DbExecuteMapper dbExecuteMapper;
// @Autowired
// SqlSessionTemplate sqlSessionTemplate;
@Autowired
@Resource(name = "eibs")
@Qualifier
SqlSessionTemplate eibsSqlSessionTemplate;
@Autowired
@Resource(name = "swd")
SqlSessionTemplate swdSqlSessionTemplate;
@Autowired
@Resource(name = "fxd")
SqlSessionTemplate fxdSqlSessionTemplate;
public SqlSessionTemplate getSqlSessionTemplate() {
Class cls= this.getClass();
Field ff[]=cls.getFields();
String currentDbName= DataSourceUtil.getDB();
ff= cls.getDeclaredFields();
for (Field f:ff)
{
if(f.getType()== SqlSessionTemplate.class)
{
String dbName=f.getName();
if(!MdaUtils.isEmpty(dbName) && dbName.startsWith(currentDbName))
{
try {
// System.out.println(f.get(this));
SqlSessionTemplate sqlSessionTemplate= (SqlSessionTemplate) f.get(this);
// this.dbConnect();
return sqlSessionTemplate;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return null;
}
/**
* mapper.xml文件中namespace指向类的package
*/
private String namespacePackage = "com.ceb.gjjs.mda.dao";
// private String namespacePackage = "com.ceb.gjjs.mapper";
public void setNamespacePackage(String namespacePackage) {
this.namespacePackage = namespacePackage;
}
/**
* mybatis sqlid 格式化模板,
* %s: namespackPackage
* %s: BO类名
* %s:具体sqlId
*/
private String sqlIdTemplate = "%s.%sMapper.%s";
public void setSqlIdTemplate(String sqlIdTemplate) {
this.sqlIdTemplate = sqlIdTemplate;
}
@Override
public int dbCounter(String seqname) {
return this.counter.dbCounter(seqname);
}
@Override
public int dbInsert(IModule module) {
String sqlId = getSqlId(module.getClass(), SQLID_INSERT);
return executeUpdate(sqlId, module);
}
@Override
public int dbUpdate(IModule module, Object v1, String whereCol1) {
return 0;
}
@Override
public int dbUpdate(IModule module, Object v1, String whereCol1, Object v2, String whereCol2) {
return 0;
}
@Override
public <T extends IModule> int dbUpdate(T module) {
String sqlId = getSqlId(module.getClass(), SQLID_UPDATE);
return executeUpdate(sqlId, module);
}
@Override
public int dbDelete(IModule module) {
String sqlId = getSqlId(module.getClass(), SQLID_DELETE);
return executeUpdate(sqlId, module);
}
@Override
public int dbDelete(IModule m, Argument<? extends Object>... args) {
Class clazz = (Class) m.getClass();
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(DELETE, clazz, args);
return executeUpdate(getSqlId(clazz, SQLID_DYNCDEL), adapter.getSqlParams());
}
public <T extends IModule> int dbDelete(T module, Serializable... id) {
Class<T> clazz = (Class<T>) module.getClass();
String sqlId = getSqlId(clazz, SQLID_DELBYIDS);
return executeUpdate(sqlId, Arrays.asList(id));
}
public <T extends IModule> int dbDelete(Class<T> clazz, Map paramMap) {
throw new UnsupportedOperationException("暂不支持该方法!");
}
@Override
public int dbDelete(IModule module, Object val, String col) {
return 0;
}
@Override
public int dbDelete(IModule module, Object val, String col, Object val2, String col2) {
return 0;
}
@Override
public <T extends IModule> T dbRead(T module, Argument... args) {
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, module.getClass(), args);
T entity = this.dyncReadOne(module.getClass(), adapter);
if (entity != null) {
MdaDriver.copyValues(module, entity);
}
return entity;
}
@Override
public <T extends IModule> T dbRead(T module, Object... args) {
return null;
}
@Override
public <T extends IModule> T dbReadNoData(T module, Object... args) {
return null;
}
@Override
public <T extends IModule> T dbReadByArguments(T module, Argument<?>... args) {
return null;
}
@Override
public <T extends IModule> T dbReadHold(T module, Object... args) {
return null;
}
@Override
public <T extends IModule> int dbReadSet(IModuleList<T> list, Object... args) {
return 0;
}
@Override
public <T extends IModule> int dbReadset(IModuleList<T> list, int limitSize, Argument... args) {
list.clear();
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, list.getDataClass(), args);
String temp = adapter.getSqlTemplate();
Map<String, Object> params = adapter.getSqlParams();
if (limitSize == 0) {
List<T> resultList = this.dyncRead(list.getDataClass(), params);
list.addAll(resultList);
} else {
//TODO 实现分页查询
// Page<T> page = new Page<>(1,limitSize);
// IPage<T> pageData = baseMapper.selectPage(page, wrapper);
// list.addAll(pageData.getRecords());
}
return list.size();
}
@Override
public <T extends IModule> int dbReadset(IModuleList<T> list, Object... args) {
list.clear();
if (args instanceof Argument[]) {
this.dbReadset(list, 0, (Argument[]) args);
}
return list.size();
}
@Override
public <T extends IModule> int dbReadset(IModuleList<T> list, String whereSql, Object[] objects) {
list.clear();
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, list.getDataClass(), whereSql, objects);
List<T> result = this.dyncRead(list.getDataClass(), adapter);
list.addAll(result);
return list.size();
}
@Override
public <T extends IModule> int dbReadset(IModuleList<T>[] lists, int maxSize, String whereClause, Object[] datas) {
List<Class<? extends IModule>> clazzList = new ArrayList<>();
for (IModuleList iModule : lists) {
clazzList.add(iModule.getDataClass());
}
MybatisArgumentAdapter adapter = new MybatisArgumentAdapter(SELECT, clazzList, whereClause, datas);
for (IModuleList iModule : lists) {
List<T> result = this.dyncRead(iModule.getDataClass(), adapter);
iModule.addAll(result);
}
return lists[0].size();
}
@Override
public void dbReadset(IModuleList[] lists, String whereClause, Object[] datas) {
}
@Override
public void dbReadset(IModuleList list, int maxSize, String sql) {
}
/**
* 方法存在SQL注入风险,不建议使用
*/
@Deprecated
@Override
public <T extends IModule> int dbReadset(IModuleList<T> list, String whereSql) {
return this.dbReadset(list, whereSql, null);
}
@Override
public <T extends IModule> int dbReadSetByArguments(IModuleList<T> list, Argument<?>... args) {
return 0;
}
@Override
public void dbConnect() {
getSqlSessionTemplate().getSqlSessionFactory().openSession(false);
}
@Override
public void dbConnect(String db) {
DataSourceUtil.setDB(db);
this.dbConnect();
}
@Override
public void dbDisconnect() {
try {
SqlSessionTemplate sqlSessionTemplate=getSqlSessionTemplate();
if(sqlSessionTemplate!=null)
sqlSessionTemplate.getConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void dbExecuteSQL(IStream stm) {
}
@Override
public void dbExecuteSQL(IStream sql, IStream retstream) {
}
@Override
public String dbName() {
return getSqlSessionTemplate().getConfiguration().getDatabaseId();
}
@Override
public void dbExecuteSQL(String sql, Object... objects) {
Map<String, Object> map = new HashMap<>();
if (objects != null) {
sql = MybatisArgumentAdapter.convertPlaceHolder(sql, objects, map);
}
map.put(HOLDER_SQL, sql);
if (sql.toUpperCase().startsWith("SELECT")) {
List<Map<String, Object>> result = dbExecuteMapper.dyncReadForMap(map);
if (result == null || result.size() == 0) {
setNoMoreRow();
return;
}
resultLocal.set(new ResultSet(result));
} else {
int cnt = dbExecuteMapper.dyncUpdateForMap(map);
}
}
@Override
public void dbFetch(IModule module) {
}
@Override
public void dbFetchStream(IStream args) {
}
@Override
public void dbFetchFields(Object... arguments) {
if (arguments == null || arguments.length == 0) return;
if (resultLocal.get() == null) {
log.error("must execute dbExecuteSQL first !");
setNoMoreRow();
return;
}
if (!resultLocal.get().hasNext()) {
setNoMoreRow();
return;
}
Map<String, Object> row = resultLocal.get().next();
for (Object o : arguments) {
if (o instanceof Argument) {
Argument argument = (Argument) o;
setArgumentValue(argument, row.get(argument.getFieldName().toUpperCase()));
}
}
}
@Override
public void dbFetchFields(Argument<?>... arguments) {
}
private void setArgumentValue(Argument<? extends Object> argument, Object obj) {
if (obj == null) return;
Argument<Object> arg = (Argument<Object>) argument;
arg.value = obj;
}
@Override
public void dbCloseCursor() {
//mybatis方式不直接访问结果集,不需要关闭结果集
}
@Override
public String dbSqlDate(Date endDat) {
return null;
}
@Override
public void dbFreeAll() {
}
@Override
public void setWaitCursor() {
}
@Override
public void dbHold(IModule module) {
}
@Override
public void dbFree(IModule module) {
}
@Override
public void dbSelect(IModule module, String string, String sql, Object... val) {
}
@Override
public void DBOpen(IModule module) {
}
@Override
public void dbSelectCursor(IModule module, String whereSql, Object... params) {
}
@Override
public void putDao(Class<?> key, Object dao) {
throw new UnsupportedOperationException("不支持的方法!");
}
private <T> String getSqlId(Class<T> clazz, String sid) {
return String.format(sqlIdTemplate, namespacePackage, clazz.getSimpleName(), sid);
}
private <T> T dyncReadOne(Class<? extends IModule> clazz, MybatisArgumentAdapter adapter) {
String sqlId = getSqlId(clazz, SQLID_DYNCREAD);
T entity = getSqlSessionTemplate().selectOne(sqlId, adapter.getSqlParams());
if (entity == null) {
setNoMoreRow();
}
return entity;
}
private <T> List<T> dyncRead(Class<T> clazz, MybatisArgumentAdapter adapter) {
String sqlId = getSqlId(clazz, SQLID_DYNCREAD);
List<T> list = getSqlSessionTemplate().selectList(sqlId, adapter.getSqlParams());
if (list == null || list.size() == 0) {
setNoMoreRow();
}
return new ArrayList<>(list);
}
private <T> List<T> dyncRead(Class<T> clazz, Map params) {
String sqlId = getSqlId(clazz, SQLID_DYNCREAD);
List<T> list = getSqlSessionTemplate().selectList(sqlId, params);
if (list == null || list.size() == 0) {
setNoMoreRow();
}
return new ArrayList<>(list);
}
public static class ResultSet {
private int index;
private List<Map<String, Object>> data;
public ResultSet(List<Map<String, Object>> data) {
this.index = 0;
this.data = data;
}
public Map<String, Object> next() {
if (index < data.size()) {
return data.get(index++);
}
return null;
}
public boolean hasNext() {
return data != null && index < data.size();
}
}
private void setNoMoreRow() {
MdaEnv.getContext().setErrorCode(Constants.NO_MORE_ROW);
}
private int executeUpdate(String mybatisSqlId, Object params) {
int cnt = getSqlSessionTemplate().update(mybatisSqlId, params);
return cnt;
}
}
......@@ -49,7 +49,7 @@ import static com.brilliance.mda.support.mybatis.MybatisArgumentAdapter.HOLDER_S
*
* @author hulei
*/
@Component
//@Component
public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession {
private static Logger log = LoggerFactory.getLogger(MyBatisDaoSession.class);
......@@ -273,6 +273,11 @@ public class MyBatisDaoSession extends AbstractDaoSession implements IDaoSession
sqlSessionTemplate.getSqlSessionFactory().openSession(false);
}
@Override
public void dbConnect(String s) {
}
@Override
public void dbDisconnect() {
......
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