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

唐山网站制作方案兰州起点网站建设

唐山网站制作方案,兰州起点网站建设,wordpress字体调整,机票小代理做网站最简单的SpringBootMyBatis多数据源实现1.数据库准备2.环境准备3.代码部分3.1多数据源配置2.测试随着应用用户数量的增加,相应的并发请求的数量也会跟着不断增加,慢慢地,单个数据库已经没有办法满足频繁的数据库操作请求了,在某些…

最简单的SpringBoot+MyBatis多数据源实现

    • 1.数据库准备
    • 2.环境准备
    • 3.代码部分
      • 3.1多数据源配置
      • 2.测试

image-20200708160944615

随着应用用户数量的增加,相应的并发请求的数量也会跟着不断增加,慢慢地,单个数据库已经没有办法满足频繁的数据库操作请求了,在某些场景下,可能会需要配置多个数据源,使用多个数据源(例如实现数据库的读写分离)来缓解系统的压力等,同样的,Springboot官方提供了相应的实现来帮助开发者们配置多数据源,一般分为两种方式(目前所了解到的),分包和AOP。

考虑到mybatis是java开发使用较为频繁的数据库框架,所以使用Springboot+Mybatis来实现多数据源的配置。

1.数据库准备

既然是配置多数据源,那么自然就要先把相应的数据源给准备好,本地新建了两个数据库,如下表:

数据库数据表字段
testdatasource1sys_useruser_id(int), user_name(varchar) user_age(int)
testdatasource2sys_user2同上

并分别插入两条记录,为了方便对比,其中testdatasource1为芳年25岁的张三, testdatasource2为芳年30岁的李四。

2.环境准备

首先新建一个Springboot项目,这里版本是2.1.7.RELEASE,并在pom文件中引入相关依赖:关键依赖如下:

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version>
</dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

3.代码部分

3.1多数据源配置

首先呢,在Springboot的配置文件中配置的datasourse,和以往不一样的是,因为有两个数据源,所以要指定相关数据库的名称,其中主数据源为primary,次数据源为secondary如下:

#配置主数据库
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/testdatasource1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver##配置次数据库
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/testdatasource2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driverspring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

需要注意的是,Springboot2.0 在配置数据库连接的时候需要使用jdbc-url,如果只使用url的话会报
jdbcUrl is required with driverClassName.错误。

新建一个配置类PrimaryDataSourceConfig,用于配置的主数据库相关的bean,代码如下:

@Configuration
//basePackages:接口文件的包路径
@MapperScan(basePackages = "com.jdkcb.mybatisstuday.mapper.one", sqlSessionFactoryRef = "PrimarySqlSessionFactory")
public class PrimaryDataSourceConfig {@Bean(name = "PrimaryDataSource")// 表示这个数据源是默认数据源@Primary//这个一定要加,如果两个数据源都没有@Primary会报错@ConfigurationProperties(prefix = "spring.datasource.primary")//配置文件中的前缀public DataSource getPrimaryDateSource() {return DataSourceBuilder.create().build();}@Bean(name = "PrimarySqlSessionFactory")@Primarypublic SqlSessionFactory primarySqlSessionFactory(@Qualifier("PrimaryDataSource") DataSource datasource)throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(datasource);bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/one/*.xml"));return bean.getObject();// 设置mybatis的xml所在位置}@Bean("PrimarySqlSessionTemplate")// 表示这个数据源是默认数据源@Primarypublic SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("PrimarySqlSessionFactory") SqlSessionFactory sessionfactory) {return new SqlSessionTemplate(sessionfactory);}}

注解说明:

@MapperScan :配置mybatis的接口类放的地方

@Primary :表示使用的是默认数据库,这个一个要加,否则会因为不知道哪个数据库是默认数据库而报错

@ConfigurationProperties:读取application.properties中的配置参数映射成为一个对象,其中prefix表示参数的前缀

大功告成~ ~ 了吗?并没有,然后配置的第二个数据源的配置类,代码如下:

@Configuration
@MapperScan(basePackages = "com.jdkcb.mybatisstuday.mapper.two", sqlSessionFactoryRef = "SecondarySqlSessionFactory")
public class SecondaryDataSourceConfig {@Bean(name = "SecondaryDataSource")@ConfigurationProperties(prefix = "spring.datasource.secondary")public DataSource getSecondaryDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "SecondarySqlSessionFactory")public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("SecondaryDataSource") DataSource datasource)throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(datasource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/two/*.xml"));return bean.getObject();// 设置mybatis的xml所在位置}@Bean("SecondarySqlSessionTemplate")public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("SecondarySqlSessionFactory") SqlSessionFactory sessionfactory) {return new SqlSessionTemplate(sessionfactory);}

剩下的就是编写相应的xml文件和接口类了,代码如下:

@Component
@Mapper
public interface PrimaryUserMapper {List<User> findAll();
}
@Component
@Mapper
public interface SecondaryUserMapper {List<User> findAll();
}

相关的xml文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jdkcb.mybatisstuday.mapper.one.PrimaryUserMapper"><select id="findAll" resultType="com.jdkcb.mybatisstuday.pojo.User">select * from sys_user;</select>
</mapper><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jdkcb.mybatisstuday.mapper.two.SecondaryUserMapper"><select id="findAll" resultType="com.jdkcb.mybatisstuday.pojo.User">select * from sys_user2;</select></mapper>

注:其中xml文件在本实例中目录为:resources/mapping

2.测试

编写一个Controller用于测试,因为是测试实例且代码相对来说较为简单,所以这里就不写Service层了。

代码如下:

@RestController
public class UserController {@Autowiredprivate PrimaryUserMapper primaryUserMapper;@Autowiredprivate SecondaryUserMapper secondaryUserMapper;@RequestMapping("/primary")public Object primary(){List<User> list = primaryUserMapper.findAll();return list;}@RequestMapping("/secondary")public Object secondary  (){List<User> list = secondaryUserMapper.findAll();return list;}}

在浏览器分别输入:http://127.0.0.1:8080/primary 和 http://127.0.0.1:8080/secondary

结果如下:

[{"user_id":1,"user_name":"张三","user_age":25}] //primary 
[{"user_id":1,"user_name":"李四","user_age":30}] //secondary
http://www.yayakq.cn/news/122360/

相关文章:

  • 旅游网站的设计思路网站风格包括什么
  • 湖州微信网站建设佛山网站优化建设
  • 免费网站建设知识先做产品网站还是app
  • 自动建站源码中宁企业网络推广联系人
  • 做零食的网站涂料网站模板
  • 十堰市有几家网站公司wordpress 4.9 google
  • 网站制作的相关术语亚马逊雨林女性部落
  • 山东平台网站建设推荐网站建设管理经验做法
  • php 企业网站 后台图片上传外贸网站建设公司如何
  • 学校网站建设怎么样网站标识描述可以填关键词吗
  • 两学一做知识竞答网站域名提交收录
  • 适合网站设计的gif图片dede酒业企业网站模板
  • 西安免费做网站多少钱网站后台更新了 前台不现实
  • 手机网站 微网站长春网站制作公司哪个好
  • 中国建设服务信息网站三网合一网站开发是什么
  • 庄河市城乡规划建设局网站没企业可以做网站吗
  • 网站购买云空间wordpress 3.5
  • 杭州模板建站软件模板搭建
  • 原江苏省建设厅网站揭阳城乡建设局网站
  • 大型网站开发项目合同网站开发一个人可以完成吗
  • 江西城乡建设网站手机网页制作工具下载
  • 什么网站做电气自动化兼职深圳网站建设列表网
  • php网站开发简介wordpress 中文模块
  • wordpress全站音乐九江网站建设制作
  • 网站建设优化现状图表超级优化
  • 做网站大概需要多少钱网站搬家
  • 官方网站百度一下外贸公司网站建设费会计科目
  • 网站优化是什么意思怎么在vmware上做网站
  • 中国装饰公司十大排名单纯做seo能否提升网站流量
  • 小网站推广my63777免费域名查询2023年