一.jdbcTemplate的配置
package com.djhu.followup.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.djhu.api.util.MybatisInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
-
@Author zw
-
@DATE 2019/5/9 15:18
-
@VERSION 1.0.0
*/
@Configuration
@MapperScan(basePackages = FollowUpDbConfig.PACKAGE,sqlSessionFactoryRef=“followUp_SqlSessionFactory”)
public class FollowUpDbConfig {
static final String PACKAGE = “com.djhu.followup.dao.followup”;
static final String MAPPER_LOCATION = "classpath:followup/*.xml";
static final String CONFIG_LOCATION = “classpath:mybatis-config.xml”;
@Value("${jdbc.url}")
private String url;@Value("${jdbc.username}")
private String user;@Value("${jdbc.password}")
private String password;@Value("${jdbc.driver}")
private String driverClass;@Bean(name = “followUpDataSource”)
public DataSource followUpDataSource() {
// try {
// Thread.sleep(3000L);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
DruidDataSource followUpDataSource = new DruidDataSource();
followUpDataSource.setDriverClassName(driverClass.trim());
followUpDataSource.setUrl(url.trim());
followUpDataSource.setUsername(user.trim());
followUpDataSource.setPassword(password.trim());
followUpDataSource.setInitialSize(5);
followUpDataSource.setMinIdle(1);
followUpDataSource.setMaxActive(10);
followUpDataSource.setPoolPreparedStatements(false);
return followUpDataSource;
}
//注入模板类
@Bean(name = “followupJdbcTemplate”)
public JdbcTemplate getJdbcTemplate(@Qualifier(“followUpDataSource”) DataSource followUpDataSource){
return new org.springframework.jdbc.core.JdbcTemplate(followUpDataSource);
}
@Bean(name = "followUp_SqlSessionFactory")
public SqlSessionFactory followUpClusterSqlSessionFactory(@Qualifier("followUpDataSource") DataSource followUpDataSource)
throws Exception {
SqlSessionFactoryBean followUp_SqlSessionFactory = new SqlSessionFactoryBean();
followUp_SqlSessionFactory.setDataSource(followUpDataSource);
followUp_SqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MAPPER_LOCATION));
followUp_SqlSessionFactory.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(CONFIG_LOCATION));
Interceptor[] interceptors= {new MybatisInterceptor()};
followUp_SqlSessionFactory.setPlugins(interceptors);
return followUp_SqlSessionFactory.getObject();
}
}
二.方法内部使用
public static final String TABLE_NAME_PREFIX = “patient_info”;
@Autowired
private JdbcTemplate jdbcTemplate;
String tableId = this.baseMapper.patientTableId();//
String table = Joiner.on("_").join(TABLE_NAME_PREFIX,tableId);// “自定义”
log.info("创建随访人员表 ,表名是 {}", table);
File file = new File("./config/create.sql");
InputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
String line = IOUtils.toString(fileInputStream);
String createSql = String.format(line, table);
createSql = createSql.replaceAll("\r|\n", "");
log.info("创建随访人员表,建表语句是 :");
log.info(createSql);
jdbcTemplate.execute(createSql);
log.info("创建随访人员表成功.");
return true;
} catch (Exception e) {
log.error("创建表失败!!!,table name is {}",table);
return false;
} finally {
IOUtils.closeQuietly(fileInputStream);
}