这里分类和汇总了欣宸的全部原创(含配套源码):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());
}
在使用本地缓存时有个问题需要注意:以city表为例,如果对city表的所有写操作都是通过当前应用完成的,那么使用本地缓存是没有问题的,如果除了basic-cache,还有另一个应用在修改city表,那么basic-cache中的缓存就不会失效(因为没人告诉它),这样从basic-cache中读取的数据因为是本地缓存,所以还是更新前的数据
至此,quarkus数据库本地缓存的现有方案,咱们已全部完成了,希望本文能给您一些参考,协助您提升应用性能
《quarkus数据库篇》系列的开篇,编码实战最基础的数据库增删改查,数据库用的是PostgreSQL,在官方demo基础上进一步精简,极速入门quarkus数据库操作
本篇咱们从零开发一个quarkus应用,支持虚拟线程响应web服务,响应式操作postgresql数据库,并且在quarkus官方还未支持的情况下,率先并将其制作成docker镜像
本篇咱们从零开发一个quarkus应用,支持虚拟线程响应web服务,响应式操作postgresql数据库,并且在quarkus官方还未支持的情况下,率先并将其制作成docker镜像
本次实验学习记录主题为“FIFO_IP核实现算术求和”,主要内容是上位机通过串口向FPGA发送一定规格的数字矩阵,FPGA对矩阵处理,按规定逻辑实现求和运算,将结果返回串口转发至上位机。