首页  

quarkus hibernate 例子     所属分类 quarkus 浏览量 473
创建postgreSQL 数据库
不需要建表 ,应用启动后自动创建 
自动加载初始化数据 
src/main/resources/import.sql 

连接池 quarkus-agroal


Fruit 实体类 import javax.persistence.Cacheable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.NamedQuery; import javax.persistence.QueryHint; import javax.persistence.SequenceGenerator; import javax.persistence.Table; @Entity @Table(name = "known_fruits") @NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true")) @Cacheable public class Fruit { @Id @SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10) @GeneratedValue(generator = "fruitsSequence") private Integer id; @Column(length = 40, unique = true) private String name;
rest api 实现类 FruitResource import java.util.List; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.transaction.Transactional; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; import org.jboss.logging.Logger; import org.jboss.resteasy.annotations.jaxrs.PathParam; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; @Path("fruits") @ApplicationScoped @Produces("application/json") @Consumes("application/json") public class FruitResource { private static final Logger LOGGER = Logger.getLogger(FruitResource.class.getName()); @Inject EntityManager entityManager; @GET public List<Fruit> get() { return entityManager.createNamedQuery("Fruits.findAll", Fruit.class) .getResultList(); } @GET @Path("{id}") public Fruit getSingle(@PathParam Integer id) { Fruit entity = entityManager.find(Fruit.class, id); if (entity == null) { throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404); } return entity; } @POST @Transactional public Response create(Fruit fruit) { if (fruit.getId() != null) { throw new WebApplicationException("Id was invalidly set on request.", 422); } entityManager.persist(fruit); return Response.ok(fruit).status(201).build(); } @PUT @Path("{id}") @Transactional public Fruit update(@PathParam Integer id, Fruit fruit) { if (fruit.getName() == null) { throw new WebApplicationException("Fruit Name was not set on request.", 422); } Fruit entity = entityManager.find(Fruit.class, id); if (entity == null) { throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404); } entity.setName(fruit.getName()); return entity; } @DELETE @Path("{id}") @Transactional public Response delete(@PathParam Integer id) { Fruit entity = entityManager.getReference(Fruit.class, id); if (entity == null) { throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404); } entityManager.remove(entity); return Response.status(204).build(); } @Provider public static class ErrorMapper implements ExceptionMapper<Exception> { @Inject ObjectMapper objectMapper; @Override public Response toResponse(Exception exception) { LOGGER.error("Failed to handle request", exception); int code = 500; if (exception instanceof WebApplicationException) { code = ((WebApplicationException) exception).getResponse().getStatus(); } ObjectNode exceptionJson = objectMapper.createObjectNode(); exceptionJson.put("exceptionType", exception.getClass().getName()); exceptionJson.put("code", code); if (exception.getMessage() != null) { exceptionJson.put("error", exception.getMessage()); } return Response.status(code) .entity(exceptionJson) .build(); } } }
rest api 测试 列表 http://127.0.0.1:8080/fruits 获取单个 http://127.0.0.1:8080/fruits/3 添加 curl -XPOST -H 'Content-Type:application/json' http://127.0.0.1:8080/fruits -d '{"id":4,"name":"tiger"}' {"exceptionType":"javax.ws.rs.WebApplicationException","code":422,"error":"Id was invalidly set on request."} curl -XPOST -H 'Content-Type:application/json' http://127.0.0.1:8080/fruits -d '{"name":"tiger"}' 注意设置 Content-Type ,且不需要id 更新 curl -XPUT -H 'Content-Type:application/json' http://127.0.0.1:8080/fruits/10 -d '{"name":"tiger_NEW"}' 删除 curl -XDELETE -H 'Content-Type:application/json' http://127.0.0.1:8080/fruits/1
https://github.com/quarkusio/quarkus-quickstarts/tree/main/hibernate-orm-quickstart https://gitee.com/dyyx/hellocode/tree/master/demo/quarkus/hibernate-orm-quickstart
curl实用技巧

上一篇     下一篇
go test 实例

quarkus qute 例子

Java 异步编程

quarkus 打包类型 quarkus.package.type

quarkus 配置优先级

JavaScript 模板引擎