JPA JPA(Java Persistence API)是Sun官方提出的Java持久化规范。它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据。
gazelle 是一款类似于Spring data jpa的组件,但是它提供了比Spring data jpa更多的功能。它是基于hibernate提供的JPA2.1规范的。
gazelle提供了事务管理功能,一种是Jpa原生的TransactionManager,一种是植入的springTransactionManager
事务测试
@Transactional(rollbackFor = Exception.class)
public void testTX () {
shopRepository.delete("014A7A16-2297-474D-B3C4-D8F9B9E976A3");
shopRepository.delete("1111111111111");//这里会报错,将回滚
GazelleQuery.update(Shop.class)
.setter().set("shopName", "XXX").build()
.where().eq("id", "35847C13-40DE-4885-8FBE-2C1DD39F7860").build()
.execute();
}
- JDK1.7或更高版本
- 依赖管理工具: Maven3.x版本/Gradle3.1以上版本
<dependency>
<groupId>io.github.finefuture</groupId>
<artifactId>gazelle</artifactId>
<version>2.1</version>
</dependency>
@Configuration
@EnableGazelleRepository(basePackages = "org.gra4j.gazelleExample.crud.dao.jpa")
public class GazelleConfiguration {
@PersistenceContext
EntityManager entityManager;
@Autowired
PlatformTransactionManager tx;
@Bean
@PostConstruct
public Jpa jpa () {
Jpa jpa = new Jpa(entityManager);
JpaContext.setEntityManager(entityManager);
JpaContext.setTransactionType(TransactionalType.spring);//TransactionalType.jpa
JpaContext.setSpringTransactionManager(tx);
return jpa;
}
}
public interface ShopRepository extends GazelleRepository<Shop, String> {
@Query(@Where(and = @And({@Expression(ops = ExpressionOps.eq,key = "del"),
@Expression(ops = ExpressionOps.eq,key = "shopName")}),
// or = @Or({@Expression(ops = ExpressionOps.like,key = "id",value = "%BC85"),
// @Expression(ops = ExpressionOps.in)}),
order = @Order(asc = "createTime"),
check = {CheckOps.checkNullValue, CheckOps.checkEmptyValue}))
List<Shop> find (@ExpParam Object del, @ExpParam Object shopName,
@PageParam(PageType.first) int first);
@SqlQuery(value = "select * from shop where del=:del order by create_time limit 10", isNative = true, result = Shop.class)
List<Shop> find (@ExpParam("del") Integer del);
@Update(set = {"shopName"},where = @Where(and = @And(@Expression(ops = ExpressionOps.eq, key="shopName"))))
int update(@ExpParam Object shopName, @ModifyParam String name);
}
public List<Shop> find () {
Shop one = (Shop) GazelleQuery.basic(Shop.class).findOne("5BD0E7D5-CE2A-4A8A-9261-363BFD928FBD");
System.out.println(one);
return GazelleQuery.query().nativeQuery("select * from Shop limit 10", Shop.class);
// return GazelleQuery.basic(Shop.class).findAll();
// Shop save = (Shop) GazelleQuery.basic(Shop.class).save(new Shop());
// return GazelleQuery.select(Shop.class).list();
}
- qq交流群: 634217275
- 邮件交流: 1120170646@qq.com