这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
名称 | 链接 | 备注 |
---|---|---|
项目主页 | 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协议 |
<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>
<!-- 单元测试库 -->
<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>
quarkus.datasource.db-kind=postgresql
quarkus.hibernate-orm.log.sql=true
quarkus.datasource.jdbc.max-size=8
quarkus.datasource.jdbc.min-size=2
quarkus.datasource.username=quarkus
quarkus.datasource.password=123456
quarkus.datasource.jdbc.url=jdbc:postgresql://192.168.50.43:15432/quarkus_test
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.sql-load-script=import.sql
INSERT INTO city(id, name) VALUES (1, 'BeiJing');
INSERT INTO city(id, name) VALUES (2, 'ShangHai');
INSERT INTO city(id, name) VALUES (3, 'GuangZhou');
INSERT INTO country(id, name) VALUES (1, 'China');
INSERT INTO country_city(country_id, cities_id) VALUES (1, 1);
INSERT INTO country_city(country_id, cities_id) VALUES (1, 2);
INSERT INTO country_city(country_id, cities_id) VALUES (1, 3);
package com.bolingcavalry.db.entity;
import javax.persistence.*;
@Entity
@Table(name = "city")
@NamedQuery(name = "City.findAll", query = "SELECT c FROM City c ORDER BY c.name")
public class City {
@Id
@SequenceGenerator(name = "citySequence", sequenceName = "city_id_seq", allocationSize = 1, initialValue = 10)
@GeneratedValue(generator = "citySequence")
private Integer id;
@Column(length = 40, unique = true)
private String name;
public City() {
}
public City(String name) {
this.name = name;
}
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;
}
}
package com.bolingcavalry.db.entity;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "country")
public class Country {
@Id
@SequenceGenerator(name = "countrySequence", sequenceName = "country_id_seq", allocationSize = 1, initialValue = 10)
@GeneratedValue(generator = "countrySequence")
private Integer id;
@Column(length = 40, unique = true)
private String name;
@OneToMany
List<City> cities;
public Country() {
}
public Country(String name) {
this.name = name;
}
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 List<City> getCities() {
return cities;
}
public void setCities(List<City> cities) {
this.cities = cities;
}
}
@ApplicationScoped
public class CityService {
@Inject
EntityManager entityManager;
public City getSingle(Integer id) {
return entityManager.find(City.class, id);
}
public List<City> get() {
return entityManager.createNamedQuery("City.findAll", City.class)
.getResultList();
}
@Transactional
public void create(City fruit) {
entityManager.persist(fruit);
}
@Transactional
public void update(Integer id, City fruit) {
City entity = entityManager.find(City.class, id);
if (null!=entity) {
entity.setName(fruit.getName());
}
}
@Transactional
public void delete(Integer id) {
City entity = entityManager.getReference(City.class, id);
if (null!=entity) {
entityManager.remove(entity);
}
}
}
@ApplicationScoped
public class CountyService {
@Inject
EntityManager entityManager;
public Country getSingle(Integer id) {
return entityManager.find(Country.class, id);
}
}
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class CacheTest {
/**
* import.sql中导入的记录数量,这些是应用启动是导入的
*/
private static final int EXIST_CITY_RECORDS_SIZE = 3;
private static final int EXIST_COUNTRY_RECORDS_SIZE = 1;
/**
* 在City.java中,id字段的SequenceGenerator指定了initialValue等于10,
* 表示自增ID从10开始
*/
private static final int ID_SEQUENCE_INIT_VALUE = 10;
/**
* import.sql中,第一条记录的id
*/
private static final int EXIST_FIRST_ID = 1;
@Inject
CityService cityService;
@Inject
CountyService countyService;
@Test
@DisplayName("list")
@Order(1)
public void testGet() {
List<City> list = cityService.get();
// 判定非空
Assertions.assertNotNull(list);
// import.sql中新增3条记录
Assertions.assertEquals(EXIST_CITY_RECORDS_SIZE, list.size());
}
@Test
@DisplayName("getSingle")
@Order(2)
public void testGetSingle() {
City city = cityService.getSingle(EXIST_FIRST_ID);
// 判定非空
Assertions.assertNotNull(city);
// import.sql中的第一条记录
Assertions.assertEquals("BeiJing", city.getName());
}
@Test
@DisplayName("update")
@Order(3)
public void testUpdate() {
String newName = LocalDateTime.now().toString();
cityService.update(EXIST_FIRST_ID, new City(newName));
// 从数据库取出的对象,其名称应该等于修改的名称
Assertions.assertEquals(newName, cityService.getSingle(EXIST_FIRST_ID).getName());
}
@Test
@DisplayName("create")
@Order(4)
public void testCreate() {
int numBeforeDelete = cityService.get().size();
City city = new City("ShenZhen");
cityService.create(city);
// 由于是第一次新增,所以ID应该等于自增ID的起始值
Assertions.assertEquals(ID_SEQUENCE_INIT_VALUE, city.getId());
// 记录总数应该等于已有记录数+1
Assertions.assertEquals(numBeforeDelete + 1, cityService.get().size());
}
@Test
@DisplayName("delete")
@Order(5)
public void testDelete() {
// 先记删除前的总数
int numBeforeDelete = cityService.get().size();
// 删除testCreate方法中新增的记录,此记录的是第一次使用自增主键,所以id等于自增主键的起始id
cityService.delete(ID_SEQUENCE_INIT_VALUE);
// 记录数应该应该等于删除前的数量减一
Assertions.assertEquals(numBeforeDelete-1, cityService.get().size());
}
}
@DisplayName("cacheEntity")
@Order(6)
@RepeatedTest(10000)
public void testCacheEntity() {
City city = cityService.getSingle(EXIST_FIRST_ID);
// 判定非空
Assertions.assertNotNull(city);
}
@DisplayName("cacheSQL")
@Order(7)
@RepeatedTest(10000)
public void testCacheSQL() {
List<City> cities = cityService.get();
// 判定非空
Assertions.assertNotNull(cities);
// import.sql中新增3条city记录
Assertions.assertEquals(EXIST_CITY_RECORDS_SIZE, cities.size());
}
Query query = ...
query.setHint("org.hibernate.cacheable", Boolean.TRUE);
@DisplayName("cacheOne2Many")
@Order(8)
@RepeatedTest(10000)
public void testCacheOne2Many() {
Country country = countyService.getSingle(EXIST_FIRST_ID);
// 判定非空
Assertions.assertNotNull(country);
// import.sql中新增3条city记录
Assertions.assertEquals(EXIST_CITY_RECORDS_SIZE, country.getCities().size());
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://xinchen.blog.csdn.net/article/details/124958726
内容来源于网络,如有侵权,请联系作者删除!