springboot使用多数据源以及配置
1. 首先在application中配置数据源地址
my.datasource.koi.type=com.alibaba.druid.pool.DruidDataSource
my.datasource.koi.url=jdbc:mysql://localhost:3306/?useUnicode=true&characterEncoding=utf-8&useSSL=true
my.datasource.koi.username=root
my.datasource.koi.password=123456
my.datasource.koi.driverClassName=com.mysql.jdbc.Driver
2. 在@Bean中 new DataSource的地址空间
@Configuration
public class KOIDataSourceConfig { @Resource
private DataSourceFactory dataSourceFactory; @Bean(name = "koiDataSource")
@ConfigurationProperties(prefix = "my.datasource.koi")
public DruidDataSource createDataSource() {
return dataSourceFactory.create();
}
// dataSourceFactory.create() : DruidDataSource dataSource = new DruidDataSource(); @Bean(name = "jdbcTemplate")
public JdbcTemplate createSqlSessionTemplate(@Qualifier("koiDataSource") DruidDataSource druidDataSource) {
return new JdbcTemplate(druidDataSource);
}
}
3. 在Service中直接使用即可
@Service
public class KOISchedulerImpl implements KOIScheduler{ @Resource
private JdbcTemplate jdbcTemplate; @Transactional
@Override
public void set_task_and_run(SQLPool pool, String config_path) { ExecuteSQL.prepare_and_execut_sql(pool, config_path); } @Transactional
@Override
public void startSchedulerTaskByDruid(String configPath) {
jdbcTemplate.execute("INSERT INTO mydata.`test` (id) VALUE(1);");
}
}
读取application中的配置信息:
1. 在application中配置 spring.sql.path = E://z 2. 定义一个Component类 @Component
public class QualityRecode { @Value("${spring.sql.path}")
private String sqlPath; public QualityRecode() {} public String getSqlPath() {
return sqlPath;
} public void setSqlPath(String sqlPath) {
this.sqlPath = sqlPath;
}
} 3. 在Controller中实例化该类, 获取数据 @Resource
private QualityRecode recode; recode.getSqlPath()
4. 使用Mapper读取 写入数据
1. 先在application配置数据源信息 2. 配置数据源config类 @Configuration
@MapperScan(basePackages = "com.nio.mapper.dcs", sqlSessionTemplateRef = "dcsSqlSessionTemplate")
public class DcsDataSourceConfig { @Resource
private DataSourceFactory dataSourceFactory; @Bean(name = "dcsDataSource")
@ConfigurationProperties(prefix = "my.datasource.dcs")
public DruidDataSource createDataSource() {
return dataSourceFactory.create();
} @Bean(name = "dcsSqlSessionFactory")
public SqlSessionFactory createSqlSessionFactory(@Qualifier("dcsDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/dcs/*.xml"));
return bean.getObject();
} // @Bean(name = "cmsTransactionManager")
// public DataSourceTransactionManager createTransactionManager(@Qualifier("cmsDataSource") DataSource dataSource) {
// return new DataSourceTransactionManager(dataSource);
// } @Bean(name = "dcsSqlSessionTemplate")
public SqlSessionTemplate createSqlSessionTemplate(@Qualifier("dcsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
} 3. 创建实体类 接收表传来的数据 4. 编辑Mapper类, 创建查询-插入方法 public interface CmsMapper {
List<PostEntity> getPostEntityList();
List<PostEntity> getLatestPostEntityList(@Param("yesterdayTimeStamp") Long yesterdayTimeStamp,
@Param("currentTimeStamp") Long currentTimeStamp); void insertError(@Param("TaskLogRecordEntity") List<TaskLogRecord> taskLogRecords);
} 5. 实现Mapper类 <select id="getPostEntityList" resultType="com.nio.entities.PostEntity">
SELECT
up.account_id AS accountId,
up.create_time AS updateTime,
up.content,
up.like_count AS likeCount,
up.comment_count AS commentCount,
lu.`name` AS nickName
FROM
lifestyle_prod.user_post up
LEFT JOIN lifestyle_prod.livestream_user lu ON up.account_id = lu.user_id
WHERE
up.STATUS = 1
AND up.resource_type = 'user_post'
AND lu.supplier = 'rongyun'
AND up.create_time >= #{yesterdayTimeStamp}
AND up.create_time <![CDATA[<=]]> #{currentTimeStamp}
</select> <insert id="insertError">
insert into `task_log_record`
(
id,task_type,execute_time,error_info
)
values
<foreach collection="TaskLogRecordEntity" item="error" separator=",">
(
#{error.id},
#{error.task_type},
#{error.execute_time},
#{error.error_info}
)
</foreach>
</insert>
6. 需要注意的问题:
1. 当查询一个原表中不存在, 但需要写在中间实体的, 指标查询表时:
例如, MySQL 该表只有一个字段 id , 在计算之后, 生成
public class TestEntity {
private long id2;
private long id3;
}
Mapper应写成:
<select id="getTest" resultType="com.nio.entities.TestEntity">
select t.id + 3 id2, t.id +1 id3 from
(select 2 id from test) t
</select>
## select 的值, 必须与实体类名一致, 而不是表中字段的名;
springboot使用多数据源以及配置的更多相关文章
- springBoot与多数据源的配置
http://www.cnblogs.com/shenlanzhizun/p/5846475.html 最近有点忙,更新有点慢.今天进来说说一说springBoot中如何配置多数据源. 第一,新建一个 ...
- Springboot+Mybatis+Pagehelper+Aop动态配置Oracle、Mysql数据源
本文链接:https://blog.csdn.net/wjy511295494/article/details/78825890 Springboot+Mybatis+Pagehelper+Aop ...
- springboot添加多数据源连接池并配置Mybatis
springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018 ...
- springboot之多数据源配置JdbcTemplate
springboot多数据源配置,代码如下 DataSourceConfig package com.rookie.bigdata.config; import org.springframework ...
- springboot+ibatis 多数据源配置
这个是boot基本版本包,因为我用的打包方式是war所以去除掉了boot内置的tomcat,但是为了方便测试又引入了内置tomcat,只要添加<scope>provided</sco ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- 基于springboot的多数据源配置
发布时间:2018-12-11 技术:springboot1.5.1 + maven3.0.1+ mybatis-plus-boot-starter2.3.1 + dynamic-datasour ...
- Springboot spring data jpa 多数据源的配置01
Springboot spring data jpa 多数据源的配置 (说明:这只是引入了多个数据源,他们各自管理各自的事务,并没有实现统一的事务控制) 例: user数据库 global 数据库 ...
- springboot集成activiti6.0多数据源的配置
最近公司开始开发springboot的项目,需要对工作流进行集成.目前activiti已经发布了7.0的版本,但是考虑到6.0版本还是比较新而且稳定的,决定还是选择activiti6.0的版本进行集成 ...
随机推荐
- [MySQL] timestamp和datetime的区别和大坑
1.timestamp占用4个字节;datetime占用8个字节2.timestamp范围1970-01-01 00:00:01.000000 到 2038-01-19 03:14:07.999999 ...
- SQL使用总结
本文为转载:对于SQL的学习与使用,推荐大家去这儿,讲的很系统: http://www.w3school.com.cn/sql/index.asp 练习SQL的使用,推荐大家去这里: https:// ...
- 用node.js express设置路径后 子路径下的页面访问静态资源路径出问题
在routes/news_mian.js 设置了访问news_main.html 的路径 '/',通知设置一个访问news-page.html的子路径'/newspage'子路径.但是在访问loacl ...
- Python中文词频统计
以下是关于小说的中文词频统计 这里有三个文件,分别为novel.txt.punctuation.txt.meaningless.txt. 这三个是小说文本.特殊符号和无意义词 Python代码统计词频 ...
- java-HashMap默认机制
HashMap:键值对(key-value): 通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value. 默认是1:1关系: 存在则覆盖,当key已经存在,则利用新的value ...
- JAVA程序员学PHP
工作之余,趁着五一假期学习下PHP,都说PHP是世界上最美的语言,而且现在应用的有这么广泛,在短期时间内在编程的市场上打得火热,好奇心趋势我去学习一下,下面便是我学习PHP记录下来的过程,和大家分享一 ...
- Timeline Style
from: https://freefrontend.com/css-timelines/ https://bootstrapthemes.co/items/resources/timeline/ h ...
- 【代码笔记】Web-CSS-CSS盒子模型
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 如何解决angular不自动生成spec.ts文件
"schematics":{ "@schematics/angular:component": { "styleext": ...
- 国人如此浮躁为哪般? --- 我看2018年度AI圈八大造假事件华人独占6件
国人如此浮躁为哪般? --- 我看2018年度AI圈八大造假事件华人独占6件 人工智能在2018年发展很快,取得了很多成绩:很多新的产品,框架,软硬件系统,层出不穷:以七巨头为首的业界头部企业也取得 ...