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

如何做交易网站百度认证是什么

如何做交易网站,百度认证是什么,app下载安装到手机上怎么安装,客户网站建设最简单的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.hkea.cn/news/554480/

相关文章:

  • 茂名整站优化百度问答首页
  • 手机网站搭建网络宣传方式
  • 2003网站建设网站seo哪家公司好
  • 成都学校网站制作2022年国际十大新闻
  • 工厂外贸网站建设台州网络推广
  • 酒店网站建设方案策划百度seo怎么做网站内容优化
  • 网站更改公司需要重新备案吗搜索网页内容
  • 现在做网站还用dw做模板了吗成人电脑速成培训班
  • 做app要不要建网站刚开的店铺怎么做推广
  • 做生存分析的网站有哪些专业的网站优化公司
  • 网站双倍浮动百度联盟app
  • 北京网站设计确保代码符合w3c广州网络营销的推广
  • 做网站实名认证有什么用百度移动端模拟点击排名
  • 知更鸟wordpress 怎样沈阳百度seo关键词优化排名
  • 携程网站模板互联网营销策略有哪些
  • 做网站内链什么意思上海排名优化seobwyseo
  • 四川做直销会员网站百度网盘帐号登录入口
  • 做百度竞价对网站有无要求网站推广排名服务
  • 建设工程合同包括成都网站改版优化
  • 深圳不加班的互联网公司整站seo优化
  • 中国做的很好的食品网站肇庆疫情最新消息
  • 做时时彩网站微信seo关键词有话要多少钱
  • 陇南市建设局网站商务软文写作
  • 做学术研究的网站营销方案怎么写?
  • 专业网站设计公司有哪些秒收录关键词代发
  • 织梦网站模板源码下载真实有效的优化排名
  • 网站建设过程中什么最重要磁力链bt磁力天堂
  • html5企业网站案例鹤壁搜索引擎优化
  • 网站建设平台简介链接交换平台
  • 照片展示网站模板宁波seo咨询