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

备案信息网站被注册创网网络

备案信息网站被注册,创网网络,商城类网站风格,网站建设课程的建议配置多数据源 模拟多库场景 适用于多种场景: 多库(操作的表分布在不同数据库当中),读写分离(有的数据库负责查询的功能,有的数据库负责增删该的功能),一主多从,混合模式等 第一步: 模拟多库,在mybatis_plus数据库中创建user表,在mybatis_plus_1数据库中创建product表 --创建…

配置多数据源

模拟多库场景

适用于多种场景: 多库(操作的表分布在不同数据库当中),读写分离(有的数据库负责查询的功能,有的数据库负责增删该的功能),一主多从,混合模式等

第一步: 模拟多库,在mybatis_plus数据库中创建user表,在mybatis_plus_1数据库中创建product表

--创建mybatis_plus数据库
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
--创建user表
CREATE TABLE `t_user` (`id` bigint(20) NOT NULL COMMENT '主键ID',`name` varchar(30) DEFAULT NULL COMMENT '姓名',`age` int(11) DEFAULT NULL COMMENT '年龄',`email` varchar(50) DEFAULT NULL COMMENT '邮箱',PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--向user表中插入数据
INSERT INTO t_user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
--创建mybatis_plus_1数据库
CREATE DATABASE `mybatis_plus_1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus_1`;
--创建product表
CREATE TABLE t_product(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名称',
price INT(11) DEFAULT 0 COMMENT '价格',
version INT(11) DEFAULT 0 COMMENT '乐观锁版本号',
PRIMARY KEY (id)
);
--向product表插入数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人笔记本', 100);

第二步:在pom.xml文件中引入多数据源使用的依赖

<!--配置多数据源需要使用的依赖-->
<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version>
</dependency>

第三步: 在appilcation.yaml文件中添加配置多数据源的配置选项

spring:# 配置数据源信息datasource:dynamic:# 设置默认的数据源或者数据源组,默认值即为masterprimary: master# 严格匹配数据源(默认false),true表示如果未匹配到指定数据源时抛异常,false表示如果未匹配到指定的数据源就使用默认的数据源strict: falsedatasource:master: # 默认数据源url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=falsedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 123456slave_1: # 其他数据源url: jdbc:mysql://localhost:3306/mybatis_plus_1?characterEncoding=utf-8&useSSL=falsedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 123456

第四步: 创建实体类封装表中查询的数据

//封装用户信息的实体类
@Data
@TableName("t_user")
public class User {private Integer id;private String name;private Integer age;private String email;
}
//封装产品信息的实体类
@Data
@TableName("t_product")
public class Product {private Integer id;private String name;private Integer price;private Integer version;
}

第五步: 编写User和Product对应的mapper接口和service接口

@Repository
public interface ProductMapper extends BaseMapper<Product> {
}@Repository
public interface UserMapper extends BaseMapper<User> {
}public interface UserService extends IService<User> {}public interface ProductService extends IService<Product> {}

第六步: 创建User和Product实体类对应的service接口的实现类,使用@DS注解指定当前类或方法要操作的数据源

  • @Ds注解使用在方法上或类上(使用在类上表示类中所有的方法默认都有该注解)
  • @Ds注解在类和方法上同时存在则遵循就近原则,方法上指定操作的数据源优先于类上指定操作的数据源
注解结果
没有@DS注解操作默认的数据源
@DS(“dsName”)dsName为要操作的数据源名称
@Service
@DS("master") //t_user表在master数据源当中
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implementsUserService {
}@Service
@DS("slave_1")//t_product表在slave_1数据源当中
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}

第七步: 编写Spring Boot主程序的启动类

@SpringBootApplication
@MapperScan("com.atguigu.mybatisplus.mapper")// 扫描mapper接口所在的包
public class MybatisPlusDatasourceApplication {public static void main(String[] args) {SpringApplication.run(MybatisPlusDatasourceApplication.class, args);}
}

第八步: 由于t_user表和t_product在两个不同的数据库(数据源)中,如果通过一个测试方法可以分别获取到用户数据与商品数据成功说明多库模拟成功

@SpringBootTest
public class MybatisPlusDatasourceApplicationTests {@Autowiredprivate UserService userService;@Autowiredprivate ProductService productService;@Testpublic void testDynamicDataSource(){// 输出查询出来的对象System.out.println(userService.getById(1));System.out.println(productService.getById(1));}
}

模拟读写分离

实现读写分离的效果

  • 写操作: 增删改操作的方法加上@DS注解指定操作主库数据源,因为只有主数据源才可以把更新的操作同步到从数据源
  • 读操作: 查询操作的方法指定操作从库数据源
http://www.yayakq.cn/news/865197/

相关文章:

  • 制作 网站导航 下拉菜单苏州网站推广优化
  • 北京优化网站公司什么软件能自己做网站
  • 仁寿网站建设绿色wordpress主题模板下载
  • 自动化营销网站建设黄岩做网站公司电话
  • 胶州企业网站建设毕业设计代做网站php
  • 关于建设单位网站的方案电子商务平台经营者制定平台服务协议和交易规则时
  • 上饶网站制作需要多少钱广州大型网站制作公司
  • 大型网站集群怎么做帝国cms王猛微博
  • 用python网站开发太原seo优化公司
  • 做app网站的软件叫什么名字吗网站建设平台怎么做
  • 科普互联网站建设wordpress博客的搭建
  • 电子商务网站建设核心是泰州营销型网站
  • 易语言做试用点击网站重庆建设工程施工安全管理平台
  • 二手书网站建设的目的镇江网站搭建
  • 哪个网站学做凉皮网站建设遇到问题解决方案
  • 出入库管理软件 免费淄博网站文章优化
  • 凡科做的网站怎么改壁纸软件开发需要学多久
  • 网页设计网站方案关键词优化是什么意思?
  • 扬州做网站有人利用婚恋网站做微商
  • 旅游网站建设的概念湖南建设资质申请网站
  • 网站建设简述app拉新一手渠道商
  • 本地网站可以做吗?四川建设网有限责任公司招聘
  • 上海市网站建设电话号码wordpress截取
  • 微餐饮网站建设官网河源市住房和城乡建设局网站
  • 郑州网站制作设计最好网站设计案例
  • 山西城乡和建设厅网站首页鄂州网站建设网络公司
  • 网站js幻灯片代码中国银行官网登录入口
  • 郑州网站开发公司哪家好现在有哪些免费推广平台
  • 做毕业设计资料网站网页制作软件哪个好用
  • 贵州城乡建设厅施工员报名网站建网站设