近期遇到特殊场景需要指定数据源进行数据新增查询操作。代码如下:
@Component
public class TempDbSource {
@Value("${lyc.datasource.url}")
private String url;
@Value("${lyc.datasource.type}")
private String type;
@Value("${lyc.datasource.driver-class-name}")
private String driverClass;
@Value("${lyc.datasource.username}")
private String username;
@Value("${lyc.datasource.password}")
private String password;
@Value("${lyc.datasource.pwd-public-key}")
private String pwdPublicKey;
private SqlSessionTemplate sqlSessionTemplate;
@PostConstruct
public void init() throws Exception {
DruidDataSource slaveDataSource = new DruidDataSource();
slaveDataSource.setUrl(url);
slaveDataSource.setDbType(type);
slaveDataSource.setFilters("config");
slaveDataSource.setDriverClassName(driverClass);
slaveDataSource.setUsername(username);
slaveDataSource.setPassword(password);
slaveDataSource.setConnectionProperties("config.decrypt=true;config.decrypt.key=" + pwdPublicKey);
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] mapperLocations = ArrayUtil.addAll(resolver.getResources("classpath*:mapper/com/lyc/BizMapper.xml"));
sqlSessionFactoryBean.setMapperLocations(mapperLocations);
sqlSessionFactoryBean.setDataSource(slaveDataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("com.lyc.entity");
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
}
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
}
业务service及实现:
public interface BizService {
List<ModuleBean> listTestDatas(QueryVo queryVo);
void insertBatch(List<ModuleBean> list);
}
@Service
public class BizServiceImpl implements BizService {
@Autowired
private TempDbSource tempDbSource;**
@Override
public List<ModuleBean> listTestDatas(QueryVo queryVo) {
return tempDbSource.getSqlSessionTemplate().selectList("com.lyc.dao.BizDao.listTestDatas", queryVo);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void insertBatch(List<ModuleBean> list) {
tempDbSource.getSqlSessionTemplate().insert("com.lyc.dao.BizDao.insertBatch", list);
}
}
BizDao接口:
public interface BizDao {
List<ModuleBean> listTestDatas(QueryVo queryVo);
void insertBatch(List<ModuleBean> list);
}
对应的BizMapper.xml文件(这个文件就是数据源配置类中扫描的mapper文件):
<?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.lyc.BizDao">
<resultMap id="BaseResultMap" type="com.lyc.entity.ModuleBean"/>
<select id="listTestDatas" parameterType="com.lyc.vo.QueryVo" resultMap="BaseResultMap">
SELECT
id,name,age
FROM
test_module_bean_table
<where>
<if test="name!= null and name!= '' ">
AND name=#{name}
</if>
</where>
</select>
<insert id="insertBatch" parameterType="com.lyc.entity.ModuleBean">
insert into test_module_bean_table(name,age,create_time,create_by) values
<foreach collection="list" item="item" separator=",">
(#{item.name},#{item.age},now(),'sys_test')
</foreach>
</insert>
</mapper>
ArrayUtil工具类的方法:
public static <T> T[] addAll(T[]... arrays) {
if (arrays.length == 1) {
return arrays[0];
}
int length = 0;
for (T[] array : arrays) {
if (array == null) {
continue;
}
length += array.length;
}
T[] result = newArray(arrays.getClass().getComponentType().getComponentType(), length);
length = 0;
for (T[] array : arrays) {
if (array == null) {
continue;
}
System.arraycopy(array, 0, result, length, array.length);
length += array.length;
}
return result;
}
public static <T> T[] newArray(Class<?> componentType, int newSize) {
return (T[]) Array.newInstance(componentType, newSize);
}
至此使用BizService调用接口方法就可以通过指定的数据源操作数据了。
|