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

网站登陆注册怎么做百度指数查询app

网站登陆注册怎么做,百度指数查询app,嵌入式软件开发工程师招聘,专业做医药招聘的网站目录 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/20815/

相关文章:

  • 装修公司全包项目seo搜索引擎实训心得体会
  • 爱站网是干什么的长沙关键词排名首页
  • wordpress 教垜四川seo推广公司
  • 东莞市阳光网青岛seo服务
  • 网站弹窗在中间位置企业培训师
  • 整站下载器 安卓版域名解析查询站长工具
  • 跨境自建站模板seo推广是做什么
  • 网站建设与网页设计报告网络营销师报名入口
  • 生成前端页面的网站东莞网络营销全网推广
  • 网站及单位网站建设情况免费男女打扑克的软件
  • 公司有网站有什么好处网上开店如何推广自己的网店
  • 海口网站建设策划关键词排名优化工具有用吗
  • 请问哪里可以做网站汕头seo
  • 访问国外网站速度慢苏州关键词seo排名
  • 做网站备案照片的要求谷歌seo教程
  • wordpress站点全屏新站如何让百度快速收录
  • wordpress 会议 主题推广排名seo
  • 源码开发网站建设sem与seo的区别
  • 如何查网站的空间防恶意点击软件
  • 单位网站建设收费标准互联网推广引流
  • 网站有中文源码加英文怎么做关键词歌词完整版
  • 建设网站企业银行做网站的平台
  • 如何进行网站建设分析网站推广app软件
  • 做ppt的软件模板下载网站网站服务公司
  • 网站icp备案认证怎么做谷歌网页版入口在线
  • 高安网站建设艺考培训
  • 主流的网站开发技术百度推广后台管理
  • 传奇网站模板免费下载优化网络搜索引擎
  • 提升学历报考什么专业比较好seosem顾问
  • 做违法网站犯法吗推广费用一般多少钱