当前位置: 首页 > news >正文

中山做网站哪家好东莞seo技术

中山做网站哪家好,东莞seo技术,网站接口设置,长沙做企业网站推广的公司目录 1.多数据源 2.事务配置 项目搭建参考: 从零开始搭建SpringBoot项目_从0搭建springboot项目-CSDN博客 SpringBoot学习笔记(二) 整合redismybatisDubbo-CSDN博客 1.多数据源 添加依赖 <dependencies><dependency><groupId>org.springframework.boot&…

目录

1.多数据源

2.事务配置


项目搭建参考:

从零开始搭建SpringBoot项目_从0搭建springboot项目-CSDN博客

SpringBoot学习笔记(二) 整合redis+mybatis+Dubbo-CSDN博客

1.多数据源

添加依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.5</version></dependency><!--MyBatis整合SpringBoot的起步依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><!--MySQL的驱动依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.31</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.10</version></dependency></dependencies>

增加数据库配置

spring.datasource.primary.jdbc-url=jdbc:mysql://xxxx/table01?useUnicode=true&autoReconnect=true&zeroDateTimeBehavior=convertToNull
spring.datasource.primary.username=xxxxx
spring.datasource.primary.password=xxxxx
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driverspring.datasource.second.jdbc-url=jdbc:mysql://xxxxx/table02?useUnicode=true&autoReconnect=true&&zeroDateTimeBehavior=convertToNull
spring.datasource.second.username=xxxxx
spring.datasource.second.password=xxxxx
spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver

添加配置类

PrimaryDbConfig.java

@Configuration
@MapperScan(basePackages = {"com.ziroom.dao.primary"}, sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDbConfig {@Primary   // 这里要添加@Primary,在匹配不到数据源时,primaryData会作为默认数据源@Bean(name = "primaryData")@ConfigurationProperties(prefix = "spring.datasource.primary")public DataSource financeData() {return DataSourceBuilder.create().build();}@Bean(name = "primarySqlSessionFactory")@Primarypublic SqlSessionFactory loanSqlSessionFactory(@Qualifier("primaryData") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));return bean.getObject();}@Bean(name = "primarySqlSessionTemplate")@Primarypublic SqlSessionTemplate loanSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
}// 事务配置@Bean(name = "primaryTransactionManager")@Primarypublic DataSourceTransactionManager masterDataSourceTransactionManager(@Qualifier("primaryData") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}

SecondDbConfig.java

@Configuration
@MapperScan(basePackages = {"com.ziroom.dao.second"}, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDbConfig {@Bean(name = "secondData")@ConfigurationProperties(prefix = "spring.datasource.second")public DataSource financeData() {return DataSourceBuilder.create().build();}@Bean(name = "secondSqlSessionFactory")public SqlSessionFactory loanSqlSessionFactory(@Qualifier("secondData") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));return bean.getObject();}@Bean(name = "secondSqlSessionTemplate")public SqlSessionTemplate loanSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
}// 事务配置
@Bean(name = "secondTransactionManager")public DataSourceTransactionManager masterDataSourceTransactionManager(@Qualifier("secondData") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}

添加mapper相关

启动类屏蔽DataSourceAutoConfiguration.java

注意:类似于SpringBoot学习笔记(二) 整合redis+mybatis+Dubbo-CSDN博客 中单数据源的情况,配置文件中配置了spring.datasource.* ,且@MapperScan(value = "com.xxxx.crm.demo.mapper")加到主类上,说明指定的dao关联了默认的spring.datasource.*, 这种情况则不能排除DataSourceAutoConfiguration.class

添加测试类

@ResponseBody@RequestMapping(value = "/testPrimary")public String testPrimary(){Budget budget = new Budget();List<Budget> budgets = budgetMapper.queryBudgetActualList(budget);log.info("Primary 数据源查询 size:{}", budgets.size());return "success";}-- 输出:Primary 数据源查询 size:30@ResponseBody@RequestMapping(value = "/testSecond")public String testSecond(){Invoice invoice = new Invoice();List<Invoice> invoices = invoiceMapper.selectListByParams(invoice);log.info("Second 数据源查询 size:{}", invoices.size());return "success";}
-- 输出:Second 数据源查询 size:40

2.事务配置

PrimaryDbConfig.java中增加

@Bean(name = "primaryTransactionManager")public DataSourceTransactionManager masterDataSourceTransactionManager(@Qualifier("primaryData") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}

SecondDbConfig.java中增加

@Bean(name = "secondTransactionManager")public DataSourceTransactionManager masterDataSourceTransactionManager(@Qualifier("secondData") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}

测试类 BudgetService.java

@Service
public class BudgetService {@Autowiredprivate BudgetMapper budgetMapper;@Autowiredprivate InvoiceMapper invoiceMapper;@Transactional(value="primaryTransactionManager",rollbackFor = RuntimeException.class)public void saveBudget(Budget budget) {budget.setBudgetYear(1);budget.setBudgetMonth(1);budget.setPartner("2");budgetMapper.insertSelective(budget);if(true){throw new RuntimeException("数据源1抛出异常");}}@Transactional(value="secondTransactionManager",rollbackFor = RuntimeException.class)public void saveInvoice(Invoice invoice) {invoice.setPostingDate(new Date());invoice.setAdvancePayment("x");invoice.setInvoiceDate(new Date());invoice.setJournalDate(new Date());invoice.setAllocateRowNo(1);invoiceMapper.insertSelective(invoice);if(true){throw new RuntimeException("数据源2抛出异常");}}
}

启动类加:@EnableTransactionManagement

代码详见https://github.com/lizhjian/SpringBootTest

http://www.hkea.cn/news/237851/

相关文章:

  • 施工企业会计制度收入确认规定百度自然排名优化
  • 校园网站建设意义网络营销的特点有哪些
  • 内江做网站哪里便宜google搜索关键词热度
  • 福建省建设银行招聘网站网络推广员压力大吗
  • 动态网站订单怎么做搜索引擎优化营销
  • html5行业网站最近有哪些新闻
  • 做网站业务的怎么寻找客户在哪里打广告效果最好
  • 广东深圳seo服务内容
  • 做网站怎么备案网络服务有限公司
  • 网站主页特效欣赏百度官网下载电脑版
  • php mysql开发网站开发任何小说都能搜到的软件
  • the7 wordpress主题宁波seo外包费用
  • 云南建筑培训网seo刷点击软件
  • 男女做暖网站h5页面制作平台
  • 可以做puzzle的网站百度关键词排名提升工具
  • 竞网网站建设南宁网站seo大概多少钱
  • 114黄页信息网宝鸡seo培训
  • 东南亚做棋牌网站挖掘爱站网
  • 中国工程建设招标网官方网站谷歌查询关键词的工具叫什么
  • wordpress管理员密码忘记成都seo招聘
  • 武汉企业建站系统模板下载官方正版百度
  • 上海做网站国际财经新闻
  • 用废旧盒子做家用物品网站seo排名工具
  • 企业铭做网站域名解析在线查询
  • 怎么注册自己的小程序网站优化分析
  • 荆州网站建设流程网站设计培训
  • 网站支付怎么做的seo职业技能培训班
  • 做csgo直播网站上海知名网站制作公司
  • 深圳住建局官方网站seo网站关键词优化快速官网
  • 网站建设需要php吗企业的互联网推广