<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
implementation 'io.quarkus:quarkus-hibernate-orm-panache'
implementation 'io.quarkus:quarkus-jdbc-mysql'
#【配置数据源】数据库地址、驱动、用户名、密码
quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
quarkus.datasource.jdbc.driver=com.mysql.cj.jdbc.Driver
quarkus.datasource.username=root
quarkus.datasource.password=123456
#【字段驼峰下划线自动转换】实体类驼峰<==>数据库下划线
quarkus.hibernate-orm.physical-naming-strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy
#【不建议使用该配置】启动项目时,判断是否存在实体类对应的表,存在则删除并创建,不存在则创建
quarkus.hibernate-orm.database.generation = drop-and-create
import javax.persistence.*;
/**
* 用户基本信息 Entity
*/
@Entity
@Table(name = "t_user_base")
public class UserBaseEntity {
/**
* 主键
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
/**
* 姓名
*/
public String name;
/**
* 年龄
*/
public String age;
/**
* 创建时间
*/
public Date createTime;
@Override
public String toString() {
return "UserBaseEntity{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", createTime=" + createTime +
'}';
}
}
import com.xxx.entity.UserBaseEntity;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import javax.enterprise.context.ApplicationScoped;
/**
* 用户基本信息 Repository
*/
@ApplicationScoped
public class UserBaseRepository implements PanacheRepository<UserBaseEntity> {
}
import com.xxx.entity.UserBaseEntity;
import com.xxx.repository.UserBaseRepository;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* 用户基本信息 Resource
*/
@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserBaseResource {
@Inject
UserBaseRepository userBaseRepository;
@GET
@Path("/{id}")
public UserBaseEntity getOne(@PathParam("id") Long id) {
return userBaseRepository.findById(id);
}
@POST
@Transactional
public Boolean create(UserBaseEntity userBase) {
if (userBase.id != null) {
throw new WebApplicationException("Id was invalidly set on request.", 422);
}
userBaseRepository.persist(userBase);
return Boolean.TRUE;
}
@PUT
@Transactional
public UserBaseEntity update(UserBaseEntity userBase) {
if (userBase.id == null) {
throw new WebApplicationException("Id was invalidly set on request.", 422);
}
UserBaseEntity entity = userBaseRepository.findById(userBase.id);
if (entity == null) {
throw new WebApplicationException("UserInfo with id of " + userBaseEntity.id + " does not exist.", 404);
}
entity.name = userBase.name;
entity.age = userBase.age;
return entity;
}
@DELETE
@Path("{id}")
@Transactional
public Boolean delete(@PathParam("id") Long id) {
UserBaseEntity entity = userBaseRepository.findById(id);
if (entity == null) {
throw new WebApplicationException("UserInfo with id of " + id + " does not exist.", 404);
}
userBaseRepository.deleteById(id);
return Boolean.TRUE;
}
}
https://quarkus.io/guides/hibernate-orm-panache
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-mysql-client</artifactId>
</dependency>
implementation 'io.quarkus:quarkus-hibernate-reactive-panache'
implementation 'io.quarkus:quarkus-jdbc-mysql'
implementation 'io.quarkus:quarkus-reactive-mysql-client'
import javax.persistence.*;
/**
* 用户基本信息 Entity
*/
@Entity
@Table(name = "t_user_base")
public class UserBaseEntity {
/**
* 主键
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
/**
* 姓名
*/
public String name;
/**
* 年龄
*/
public String age;
/**
* 创建时间
*/
public Date createTime;
@Override
public String toString() {
return "UserBaseEntity{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", createTime=" + createTime +
'}';
}
}
import com.xxx.entity.UserBaseEntity;
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
import javax.enterprise.context.ApplicationScoped;
/**
* 用户基本信息 Repository
*/
@ApplicationScoped
public class UserBaseRepository implements PanacheRepository<UserBaseEntity> {
}
//CRUD
https://quarkus.io/guides/hibernate-reactive-panache