这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
# 建数据库
CREATE DATABASE first_db;
# 选中数据库
use first_db;
# 建表
CREATE TABLE IF NOT EXISTS `seller`(
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`product_num` INT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 新增三条记录
insert into seller (name, product_num) values ('seller1', 1111);
insert into seller (name, product_num) values ('seller2', 2222);
insert into seller (name, product_num) values ('seller3', 3333);
# 建数据库
CREATE DATABASE second_db;
# 建表
CREATE TABLE buyer(
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
order_num int NOT NULL
);
# 新增两条记录
insert into buyer (name, order_num) values ('buyer1', 100);
insert into buyer (name, order_num) values ('buyer2', 200);
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<!-- JDBC库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>
<!-- hibernate库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>
<!-- postgresql库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<!-- mysql库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
<!-- 单元测试库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
# first-db的配置,下面五个配置项在application.properties文件中
quarkus.hibernate-orm.log.sql=true
quarkus.datasource.db-kind=mysql
quarkus.datasource.jdbc.max-size=8
quarkus.datasource.jdbc.min-size=2
quarkus.hibernate-orm.packages=com.bolingcavalry.multidb.entity.firstdb
# first-db的配置,下面三个配置项在application-test.properties文件中,即test环境下fitst-db的数据库地址、账号、密码等信息
quarkus.datasource.username=root
quarkus.datasource.password=123456
quarkus.datasource.jdbc.url=jdbc:mysql://192.168.50.43:3306/first_db
# second_db的配置,下面五个配置项在application.properties文件中
quarkus.hibernate-orm.second_db.log.sql=true
quarkus.datasource.second_db.db-kind=postgresql
quarkus.datasource.second_db.jdbc.max-size=8
quarkus.datasource.second_db.jdbc.min-size=2
quarkus.hibernate-orm.second_db.datasource=second_db
quarkus.hibernate-orm.second_db.packages=com.bolingcavalry.multidb.entity.seconddb
# second_db的配置,下面三个配置项在application-test.properties文件中,即test环境下second_db的数据库地址、账号、密码等信息
quarkus.datasource.second_db.username=quarkus
quarkus.datasource.second_db.password=123456
quarkus.datasource.second_db.jdbc.url=jdbc:postgresql://192.168.50.43:15432/second_db
package com.bolingcavalry.multidb.entity.firstdb;
import javax.persistence.*;
@Entity
@Table(name = "seller")
@NamedQuery(name = "Seller.findAll", query = "SELECT f FROM Seller f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cacheable
public class Seller {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column(name = "product_num")
private int productNum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProductNum() {
return productNum;
}
public void setProductNum(int productNum) {
this.productNum = productNum;
}
}
package com.bolingcavalry.multidb.entity.seconddb;
import javax.persistence.*;
@Entity
@Table(name = "buyer")
@NamedQuery(name = "Buyer.findAll", query = "SELECT f FROM Buyer f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cacheable
public class Buyer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column(name = "order_num")
private int orderNum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrderNum() {
return orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
}
@ApplicationScoped
public class SellerService {
@Inject
EntityManager entityManager;
public List<Seller> get() {
return entityManager.createNamedQuery("Seller.findAll", Seller.class)
.getResultList();
}
public Seller getSingle(Integer id) {
return entityManager.find(Seller.class, id);
}
@Transactional
public void create(Seller seller) {
entityManager.persist(seller);
}
@Transactional
public void update(Integer id, Seller seller) {
Seller entity = entityManager.find(Seller.class, id);
if (null!=entity) {
entity.setName(seller.getName());
}
}
@Transactional
public void delete(Integer id) {
Seller entity = entityManager.getReference(Seller.class, id);
if (null!=entity) {
entityManager.remove(entity);
}
}
}
package com.bolingcavalry.multidb.service;
import com.bolingcavalry.multidb.entity.seconddb.Buyer;
import io.quarkus.hibernate.orm.PersistenceUnit;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;
@ApplicationScoped
public class BuyerService {
@Inject
@PersistenceUnit("second_db")
EntityManager entityManager;
public List<Buyer> get() {
return entityManager.createNamedQuery("Buyer.findAll", Buyer.class)
.getResultList();
}
public Buyer getSingle(Integer id) {
return entityManager.find(Buyer.class, id);
}
@Transactional
public void create(Buyer buyer) {
entityManager.persist(buyer);
}
@Transactional
public void update(Integer id, Buyer buyer) {
Buyer entity = entityManager.find(Buyer.class, id);
if (null!=entity) {
entity.setName(buyer.getName());
}
}
@Transactional
public void delete(Integer id) {
Buyer entity = entityManager.getReference(Buyer.class, id);
if (null!=entity) {
entityManager.remove(entity);
}
}
}
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MultiDBTest {
/**
* first_db的seller表中,初始记录数
*/
private static final int FIRST_DB_EXIST_RECORDS_SIZE = 3;
/**
* second_db的buyer表中,初始记录数
*/
private static final int SECOND_DB_EXIST_RECORDS_SIZE = 2;
/**
* import.sql中,第一条记录的id
*/
private static final int EXIST_FIRST_ID = 1;
/**
* 在Fruit.java中,id字段的SequenceGenerator指定了initialValue等于10,
* 表示自增ID从10开始
*/
private static final int ID_SEQUENCE_INIT_VALUE = 10;
@Inject
SellerService sellerService;
@Inject
BuyerService buyerService;
@Test
@DisplayName("list")
@Order(1)
public void testGet() {
List<Seller> sellerList = sellerService.get();
// 判定非空
Assertions.assertNotNull(sellerList);
// seller表初始化时新增了3条记录
Assertions.assertEquals(FIRST_DB_EXIST_RECORDS_SIZE, sellerList.size());
List<Buyer> buyerList = buyerService.get();
// 判定非空
Assertions.assertNotNull(buyerList);
// buyer表初始化时新增了2条记录
Assertions.assertEquals(SECOND_DB_EXIST_RECORDS_SIZE, buyerList.size());
}
@Test
@DisplayName("getSingle")
@Order(2)
public void testGetSingle() {
// 用第二条记录吧,第一条在执行testUpdate方法时被更改了
Seller seller = sellerService.getSingle(EXIST_FIRST_ID+1);
// 判定非空
Assertions.assertNotNull(seller);
// buyer表的第一条记录
Assertions.assertEquals("seller2", seller.getName());
// 用第二条记录吧,第一条在执行testUpdate方法时被更改了
Buyer buyer = buyerService.getSingle(EXIST_FIRST_ID+1);
// 判定非空
Assertions.assertNotNull(buyer);
// buyer表的第二条记录
Assertions.assertEquals("buyer2", buyer.getName());
}
@Test
@DisplayName("update")
@Order(3)
public void testUpdate() {
// 验证first_db的操作
String newName = LocalDateTime.now().toString();
Seller seller = new Seller();
seller.setName(newName);
// 更新数据库
sellerService.update(EXIST_FIRST_ID, seller);
Seller sellerFromDB = sellerService.getSingle(EXIST_FIRST_ID);
// 从数据库取出的对象,其名称应该等于修改的名称
Assertions.assertEquals(newName, sellerFromDB.getName());
// 验证second_db的操作
Buyer buyer = new Buyer();
buyer.setName(newName);
// 更新数据库
buyerService.update(EXIST_FIRST_ID, buyer);
Buyer buyerFromDB = buyerService.getSingle(EXIST_FIRST_ID);
// 从数据库取出的对象,其名称应该等于修改的名称
Assertions.assertEquals(newName, buyerFromDB.getName());
}
@Test
@DisplayName("create")
@Order(3)
public void testCreate() {
Seller seller = new Seller();
seller.setName("seller4");
sellerService.create(seller);
// 创建成功后,记录主键肯定是大于3的
Assertions.assertTrue(seller.getId()>FIRST_DB_EXIST_RECORDS_SIZE);
// 记录总数应该等于已有记录数+1
Assertions.assertEquals(FIRST_DB_EXIST_RECORDS_SIZE+1, sellerService.get().size());
Buyer buyer = new Buyer();
buyer.setName("buyer3");
buyerService.create(buyer);
// 创建成功后,记录主键肯定是大于3的
Assertions.assertTrue(buyer.getId()>SECOND_DB_EXIST_RECORDS_SIZE);
// 记录总数应该等于已有记录数+1
Assertions.assertEquals(SECOND_DB_EXIST_RECORDS_SIZE+1, buyerService.get().size());
}
@Test
@DisplayName("delete")
@Order(5)
public void testDelete() {
List<Seller> sellers = sellerService.get();
// 先记删除前的总数
int numBeforeDelete = sellers.size();
// 删除最后一条记录
sellerService.delete(sellers.get(numBeforeDelete-1).getId());
// 记录数应该应该等于删除前的数量减一
Assertions.assertEquals(numBeforeDelete-1, sellerService.get().size());
List<Buyer> buyers = buyerService.get();
// 先记删除前的总数
numBeforeDelete = buyers.size();
// 删除最后一条记录
buyerService.delete(buyers.get(numBeforeDelete-1).getId());
// 记录数应该应该等于删除前的数量减一
Assertions.assertEquals(numBeforeDelete-1, buyerService.get().size());
}
}
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
# 建数据库
CREATE DATABASE first_db;
# 选中数据库
use first_db;
# 建表
CREATE TABLE IF NOT EXISTS `seller`(
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`product_num` INT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 新增三条记录
insert into seller (name, product_num) values ('seller1', 1111);
insert into seller (name, product_num) values ('seller2', 2222);
insert into seller (name, product_num) values ('seller3', 3333);
# 建数据库
CREATE DATABASE second_db;
# 建表
CREATE TABLE buyer(
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
order_num int NOT NULL
);
# 新增两条记录
insert into buyer (name, order_num) values ('buyer1', 100);
insert into buyer (name, order_num) values ('buyer2', 200);
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<!-- JDBC库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>
<!-- hibernate库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>
<!-- postgresql库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<!-- mysql库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
<!-- 单元测试库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
# first-db的配置,下面五个配置项在application.properties文件中
quarkus.hibernate-orm.log.sql=true
quarkus.datasource.db-kind=mysql
quarkus.datasource.jdbc.max-size=8
quarkus.datasource.jdbc.min-size=2
quarkus.hibernate-orm.packages=com.bolingcavalry.multidb.entity.firstdb
# first-db的配置,下面三个配置项在application-test.properties文件中,即test环境下fitst-db的数据库地址、账号、密码等信息
quarkus.datasource.username=root
quarkus.datasource.password=123456
quarkus.datasource.jdbc.url=jdbc:mysql://192.168.50.43:3306/first_db
# second_db的配置,下面五个配置项在application.properties文件中
quarkus.hibernate-orm.second_db.log.sql=true
quarkus.datasource.second_db.db-kind=postgresql
quarkus.datasource.second_db.jdbc.max-size=8
quarkus.datasource.second_db.jdbc.min-size=2
quarkus.hibernate-orm.second_db.datasource=second_db
quarkus.hibernate-orm.second_db.packages=com.bolingcavalry.multidb.entity.seconddb
# second_db的配置,下面三个配置项在application-test.properties文件中,即test环境下second_db的数据库地址、账号、密码等信息
quarkus.datasource.second_db.username=quarkus
quarkus.datasource.second_db.password=123456
quarkus.datasource.second_db.jdbc.url=jdbc:postgresql://192.168.50.43:15432/second_db
package com.bolingcavalry.multidb.entity.firstdb;
import javax.persistence.*;
@Entity
@Table(name = "seller")
@NamedQuery(name = "Seller.findAll", query = "SELECT f FROM Seller f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cacheable
public class Seller {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column(name = "product_num")
private int productNum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProductNum() {
return productNum;
}
public void setProductNum(int productNum) {
this.productNum = productNum;
}
}
package com.bolingcavalry.multidb.entity.seconddb;
import javax.persistence.*;
@Entity
@Table(name = "buyer")
@NamedQuery(name = "Buyer.findAll", query = "SELECT f FROM Buyer f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cacheable
public class Buyer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column(name = "order_num")
private int orderNum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrderNum() {
return orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
}
@ApplicationScoped
public class SellerService {
@Inject
EntityManager entityManager;
public List<Seller> get() {
return entityManager.createNamedQuery("Seller.findAll", Seller.class)
.getResultList();
}
public Seller getSingle(Integer id) {
return entityManager.find(Seller.class, id);
}
@Transactional
public void create(Seller seller) {
entityManager.persist(seller);
}
@Transactional
public void update(Integer id, Seller seller) {
Seller entity = entityManager.find(Seller.class, id);
if (null!=entity) {
entity.setName(seller.getName());
}
}
@Transactional
public void delete(Integer id) {
Seller entity = entityManager.getReference(Seller.class, id);
if (null!=entity) {
entityManager.remove(entity);
}
}
}
package com.bolingcavalry.multidb.service;
import com.bolingcavalry.multidb.entity.seconddb.Buyer;
import io.quarkus.hibernate.orm.PersistenceUnit;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;
@ApplicationScoped
public class BuyerService {
@Inject
@PersistenceUnit("second_db")
EntityManager entityManager;
public List<Buyer> get() {
return entityManager.createNamedQuery("Buyer.findAll", Buyer.class)
.getResultList();
}
public Buyer getSingle(Integer id) {
return entityManager.find(Buyer.class, id);
}
@Transactional
public void create(Buyer buyer) {
entityManager.persist(buyer);
}
@Transactional
public void update(Integer id, Buyer buyer) {
Buyer entity = entityManager.find(Buyer.class, id);
if (null!=entity) {
entity.setName(buyer.getName());
}
}
@Transactional
public void delete(Integer id) {
Buyer entity = entityManager.getReference(Buyer.class, id);
if (null!=entity) {
entityManager.remove(entity);
}
}
}
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MultiDBTest {
/**
* first_db的seller表中,初始记录数
*/
private static final int FIRST_DB_EXIST_RECORDS_SIZE = 3;
/**
* second_db的buyer表中,初始记录数
*/
private static final int SECOND_DB_EXIST_RECORDS_SIZE = 2;
/**
* import.sql中,第一条记录的id
*/
private static final int EXIST_FIRST_ID = 1;
/**
* 在Fruit.java中,id字段的SequenceGenerator指定了initialValue等于10,
* 表示自增ID从10开始
*/
private static final int ID_SEQUENCE_INIT_VALUE = 10;
@Inject
SellerService sellerService;
@Inject
BuyerService buyerService;
@Test
@DisplayName("list")
@Order(1)
public void testGet() {
List<Seller> sellerList = sellerService.get();
// 判定非空
Assertions.assertNotNull(sellerList);
// seller表初始化时新增了3条记录
Assertions.assertEquals(FIRST_DB_EXIST_RECORDS_SIZE, sellerList.size());
List<Buyer> buyerList = buyerService.get();
// 判定非空
Assertions.assertNotNull(buyerList);
// buyer表初始化时新增了2条记录
Assertions.assertEquals(SECOND_DB_EXIST_RECORDS_SIZE, buyerList.size());
}
@Test
@DisplayName("getSingle")
@Order(2)
public void testGetSingle() {
// 用第二条记录吧,第一条在执行testUpdate方法时被更改了
Seller seller = sellerService.getSingle(EXIST_FIRST_ID+1);
// 判定非空
Assertions.assertNotNull(seller);
// buyer表的第一条记录
Assertions.assertEquals("seller2", seller.getName());
// 用第二条记录吧,第一条在执行testUpdate方法时被更改了
Buyer buyer = buyerService.getSingle(EXIST_FIRST_ID+1);
// 判定非空
Assertions.assertNotNull(buyer);
// buyer表的第二条记录
Assertions.assertEquals("buyer2", buyer.getName());
}
@Test
@DisplayName("update")
@Order(3)
public void testUpdate() {
// 验证first_db的操作
String newName = LocalDateTime.now().toString();
Seller seller = new Seller();
seller.setName(newName);
// 更新数据库
sellerService.update(EXIST_FIRST_ID, seller);
Seller sellerFromDB = sellerService.getSingle(EXIST_FIRST_ID);
// 从数据库取出的对象,其名称应该等于修改的名称
Assertions.assertEquals(newName, sellerFromDB.getName());
// 验证second_db的操作
Buyer buyer = new Buyer();
buyer.setName(newName);
// 更新数据库
buyerService.update(EXIST_FIRST_ID, buyer);
Buyer buyerFromDB = buyerService.getSingle(EXIST_FIRST_ID);
// 从数据库取出的对象,其名称应该等于修改的名称
Assertions.assertEquals(newName, buyerFromDB.getName());
}
@Test
@DisplayName("create")
@Order(3)
public void testCreate() {
Seller seller = new Seller();
seller.setName("seller4");
sellerService.create(seller);
// 创建成功后,记录主键肯定是大于3的
Assertions.assertTrue(seller.getId()>FIRST_DB_EXIST_RECORDS_SIZE);
// 记录总数应该等于已有记录数+1
Assertions.assertEquals(FIRST_DB_EXIST_RECORDS_SIZE+1, sellerService.get().size());
Buyer buyer = new Buyer();
buyer.setName("buyer3");
buyerService.create(buyer);
// 创建成功后,记录主键肯定是大于3的
Assertions.assertTrue(buyer.getId()>SECOND_DB_EXIST_RECORDS_SIZE);
// 记录总数应该等于已有记录数+1
Assertions.assertEquals(SECOND_DB_EXIST_RECORDS_SIZE+1, buyerService.get().size());
}
@Test
@DisplayName("delete")
@Order(5)
public void testDelete() {
List<Seller> sellers = sellerService.get();
// 先记删除前的总数
int numBeforeDelete = sellers.size();
// 删除最后一条记录
sellerService.delete(sellers.get(numBeforeDelete-1).getId());
// 记录数应该应该等于删除前的数量减一
Assertions.assertEquals(numBeforeDelete-1, sellerService.get().size());
List<Buyer> buyers = buyerService.get();
// 先记删除前的总数
numBeforeDelete = buyers.size();
// 删除最后一条记录
buyerService.delete(buyers.get(numBeforeDelete-1).getId());
// 记录数应该应该等于删除前的数量减一
Assertions.assertEquals(numBeforeDelete-1, buyerService.get().size());
}
}
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
# 建数据库
CREATE DATABASE first_db;
# 选中数据库
use first_db;
# 建表
CREATE TABLE IF NOT EXISTS `seller`(
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`product_num` INT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 新增三条记录
insert into seller (name, product_num) values ('seller1', 1111);
insert into seller (name, product_num) values ('seller2', 2222);
insert into seller (name, product_num) values ('seller3', 3333);
# 建数据库
CREATE DATABASE second_db;
# 建表
CREATE TABLE buyer(
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL,
order_num int NOT NULL
);
# 新增两条记录
insert into buyer (name, order_num) values ('buyer1', 100);
insert into buyer (name, order_num) values ('buyer2', 200);
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<!-- JDBC库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>
<!-- hibernate库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>
<!-- postgresql库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<!-- mysql库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
<!-- 单元测试库 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
# first-db的配置,下面五个配置项在application.properties文件中
quarkus.hibernate-orm.log.sql=true
quarkus.datasource.db-kind=mysql
quarkus.datasource.jdbc.max-size=8
quarkus.datasource.jdbc.min-size=2
quarkus.hibernate-orm.packages=com.bolingcavalry.multidb.entity.firstdb
# first-db的配置,下面三个配置项在application-test.properties文件中,即test环境下fitst-db的数据库地址、账号、密码等信息
quarkus.datasource.username=root
quarkus.datasource.password=123456
quarkus.datasource.jdbc.url=jdbc:mysql://192.168.50.43:3306/first_db
# second_db的配置,下面五个配置项在application.properties文件中
quarkus.hibernate-orm.second_db.log.sql=true
quarkus.datasource.second_db.db-kind=postgresql
quarkus.datasource.second_db.jdbc.max-size=8
quarkus.datasource.second_db.jdbc.min-size=2
quarkus.hibernate-orm.second_db.datasource=second_db
quarkus.hibernate-orm.second_db.packages=com.bolingcavalry.multidb.entity.seconddb
# second_db的配置,下面三个配置项在application-test.properties文件中,即test环境下second_db的数据库地址、账号、密码等信息
quarkus.datasource.second_db.username=quarkus
quarkus.datasource.second_db.password=123456
quarkus.datasource.second_db.jdbc.url=jdbc:postgresql://192.168.50.43:15432/second_db
package com.bolingcavalry.multidb.entity.firstdb;
import javax.persistence.*;
@Entity
@Table(name = "seller")
@NamedQuery(name = "Seller.findAll", query = "SELECT f FROM Seller f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cacheable
public class Seller {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column(name = "product_num")
private int productNum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProductNum() {
return productNum;
}
public void setProductNum(int productNum) {
this.productNum = productNum;
}
}
package com.bolingcavalry.multidb.entity.seconddb;
import javax.persistence.*;
@Entity
@Table(name = "buyer")
@NamedQuery(name = "Buyer.findAll", query = "SELECT f FROM Buyer f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cacheable
public class Buyer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String name;
@Column(name = "order_num")
private int orderNum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOrderNum() {
return orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
}
@ApplicationScoped
public class SellerService {
@Inject
EntityManager entityManager;
public List<Seller> get() {
return entityManager.createNamedQuery("Seller.findAll", Seller.class)
.getResultList();
}
public Seller getSingle(Integer id) {
return entityManager.find(Seller.class, id);
}
@Transactional
public void create(Seller seller) {
entityManager.persist(seller);
}
@Transactional
public void update(Integer id, Seller seller) {
Seller entity = entityManager.find(Seller.class, id);
if (null!=entity) {
entity.setName(seller.getName());
}
}
@Transactional
public void delete(Integer id) {
Seller entity = entityManager.getReference(Seller.class, id);
if (null!=entity) {
entityManager.remove(entity);
}
}
}
package com.bolingcavalry.multidb.service;
import com.bolingcavalry.multidb.entity.seconddb.Buyer;
import io.quarkus.hibernate.orm.PersistenceUnit;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;
@ApplicationScoped
public class BuyerService {
@Inject
@PersistenceUnit("second_db")
EntityManager entityManager;
public List<Buyer> get() {
return entityManager.createNamedQuery("Buyer.findAll", Buyer.class)
.getResultList();
}
public Buyer getSingle(Integer id) {
return entityManager.find(Buyer.class, id);
}
@Transactional
public void create(Buyer buyer) {
entityManager.persist(buyer);
}
@Transactional
public void update(Integer id, Buyer buyer) {
Buyer entity = entityManager.find(Buyer.class, id);
if (null!=entity) {
entity.setName(buyer.getName());
}
}
@Transactional
public void delete(Integer id) {
Buyer entity = entityManager.getReference(Buyer.class, id);
if (null!=entity) {
entityManager.remove(entity);
}
}
}
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MultiDBTest {
/**
* first_db的seller表中,初始记录数
*/
private static final int FIRST_DB_EXIST_RECORDS_SIZE = 3;
/**
* second_db的buyer表中,初始记录数
*/
private static final int SECOND_DB_EXIST_RECORDS_SIZE = 2;
/**
* import.sql中,第一条记录的id
*/
private static final int EXIST_FIRST_ID = 1;
/**
* 在Fruit.java中,id字段的SequenceGenerator指定了initialValue等于10,
* 表示自增ID从10开始
*/
private static final int ID_SEQUENCE_INIT_VALUE = 10;
@Inject
SellerService sellerService;
@Inject
BuyerService buyerService;
@Test
@DisplayName("list")
@Order(1)
public void testGet() {
List<Seller> sellerList = sellerService.get();
// 判定非空
Assertions.assertNotNull(sellerList);
// seller表初始化时新增了3条记录
Assertions.assertEquals(FIRST_DB_EXIST_RECORDS_SIZE, sellerList.size());
List<Buyer> buyerList = buyerService.get();
// 判定非空
Assertions.assertNotNull(buyerList);
// buyer表初始化时新增了2条记录
Assertions.assertEquals(SECOND_DB_EXIST_RECORDS_SIZE, buyerList.size());
}
@Test
@DisplayName("getSingle")
@Order(2)
public void testGetSingle() {
// 用第二条记录吧,第一条在执行testUpdate方法时被更改了
Seller seller = sellerService.getSingle(EXIST_FIRST_ID+1);
// 判定非空
Assertions.assertNotNull(seller);
// buyer表的第一条记录
Assertions.assertEquals("seller2", seller.getName());
// 用第二条记录吧,第一条在执行testUpdate方法时被更改了
Buyer buyer = buyerService.getSingle(EXIST_FIRST_ID+1);
// 判定非空
Assertions.assertNotNull(buyer);
// buyer表的第二条记录
Assertions.assertEquals("buyer2", buyer.getName());
}
@Test
@DisplayName("update")
@Order(3)
public void testUpdate() {
// 验证first_db的操作
String newName = LocalDateTime.now().toString();
Seller seller = new Seller();
seller.setName(newName);
// 更新数据库
sellerService.update(EXIST_FIRST_ID, seller);
Seller sellerFromDB = sellerService.getSingle(EXIST_FIRST_ID);
// 从数据库取出的对象,其名称应该等于修改的名称
Assertions.assertEquals(newName, sellerFromDB.getName());
// 验证second_db的操作
Buyer buyer = new Buyer();
buyer.setName(newName);
// 更新数据库
buyerService.update(EXIST_FIRST_ID, buyer);
Buyer buyerFromDB = buyerService.getSingle(EXIST_FIRST_ID);
// 从数据库取出的对象,其名称应该等于修改的名称
Assertions.assertEquals(newName, buyerFromDB.getName());
}
@Test
@DisplayName("create")
@Order(3)
public void testCreate() {
Seller seller = new Seller();
seller.setName("seller4");
sellerService.create(seller);
// 创建成功后,记录主键肯定是大于3的
Assertions.assertTrue(seller.getId()>FIRST_DB_EXIST_RECORDS_SIZE);
// 记录总数应该等于已有记录数+1
Assertions.assertEquals(FIRST_DB_EXIST_RECORDS_SIZE+1, sellerService.get().size());
Buyer buyer = new Buyer();
buyer.setName("buyer3");
buyerService.create(buyer);
// 创建成功后,记录主键肯定是大于3的
Assertions.assertTrue(buyer.getId()>SECOND_DB_EXIST_RECORDS_SIZE);
// 记录总数应该等于已有记录数+1
Assertions.assertEquals(SECOND_DB_EXIST_RECORDS_SIZE+1, buyerService.get().size());
}
@Test
@DisplayName("delete")
@Order(5)
public void testDelete() {
List<Seller> sellers = sellerService.get();
// 先记删除前的总数
int numBeforeDelete = sellers.size();
// 删除最后一条记录
sellerService.delete(sellers.get(numBeforeDelete-1).getId());
// 记录数应该应该等于删除前的数量减一
Assertions.assertEquals(numBeforeDelete-1, sellerService.get().size());
List<Buyer> buyers = buyerService.get();
// 先记删除前的总数
numBeforeDelete = buyers.size();
// 删除最后一条记录
buyerService.delete(buyers.get(numBeforeDelete-1).getId());
// 记录数应该应该等于删除前的数量减一
Assertions.assertEquals(numBeforeDelete-1, buyerService.get().size());
}
}
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://xinchen.blog.csdn.net/article/details/124895407
内容来源于网络,如有侵权,请联系作者删除!