Hibatis ,轻量级、高并发、分布式数据库框架

网友投稿 773 2022-10-28

Hibatis ,轻量级、高并发、分布式数据库框架

Hibatis ,轻量级、高并发、分布式数据库框架

Hibatis

Hibatis ,轻量级、高并发、分布式数据库框架 ,是 一套融合Ibatis和JPA优点的ORM框架,完美兼容JPA和SQLMapper两种ORM书写方式。对比Hibernate优势1、完美兼容JPA和SQLMapper两种ORM书写方式2、不使用session缓存,直接采用全局缓存方式,避免session级缓存带来的额外内存开销,因为实际应用时很少重复去get/load对象。3、为简捷开发,不完全遵循JPA标准,结合JPA和Ibatis特性重新制定JPA;开发效率高,易理解。4、避免了Hibernate-JPA常规性异常,如对象关联、懒加载等。5、直接瞬时状态实体执行Update,框架会自动把瞬时状态实体和持久对象进行属性复制合并,避免get/load操作带来的烦恼。6、灵活简易的全局缓存机制,支持Memory、Redis、Memcached等分布式缓存。7、支持分布式对象锁,避免数据for update 带来的数据库额外消耗和高并发问题。

maven依赖

com.yaoakeji hibatis 1.4.26

在spring中配置

如何使用

定义实体

@Entity(cacheable = false)@Table(name = "act_activity")public class Activity { @Id private long id; private String name ; }

数据库操作

public class HibatisBaseDao { @Autowired private SqlMapperTemplate mapper; /////////////////////// 实体 操作 //////////////////////// public T findById(Serializable id) { return mapper.findById(entityType, id); } public T findById(Serializable id , LockMode lockMode) { return mapper.findById(entityType, id , lockMode); } public void refresh(T entity) { mapper.refresh(entity, LockMode.NONE); } public void refresh(T entity , LockMode lockMode) { mapper.refresh(entity, lockMode); } public long count(Criterion criterion) { if(criterion == null){ criterion = CriterionBuilder.create(entityType).build(); } return mapper.count(criterion); } public R aggregate(Aggregate aggregate , Class resultType) { return mapper.aggregate(aggregate, resultType); } public List aggregateList(Aggregate aggregate , Class resultType) { return mapper.aggregateList(aggregate, resultType); } public QueryResult query(QueryForm form) { QueryResult result = null; QueryFormCriterionBuilder builder = new QueryFormCriterionBuilder(entityType, form); Criterion criterion = builder.getCriterion(); if (form instanceof PaginationForm) { Page page = mapper.paging(criterion); String orderBy = form.getOrderBy(); List list = page.getContent(); long total = page.getTotal(); int pageSize = page.getPageSize(); int currentPage = page.getCurrentPage(); result = new PaginationResult<>(list, orderBy, pageSize, currentPage, total); } else { List list = mapper.find(criterion); String orderBy = form.getOrderBy(); result = new SimpleQueryResult<>(list, orderBy); } return result; } public PaginationResult paging(Criterion criterion) { String orderBy = ""; Page page = mapper.paging(criterion); List list = page.getContent(); long total = page.getTotal(); int pageSize = page.getPageSize(); int currentPage = page.getCurrentPage(); return new PaginationResult<>(list, orderBy, pageSize, currentPage, total); } public List query(Criterion criterion) { return mapper.find(criterion); } public T findOne(Criterion criterion) { criterion.setFirstResult(0); criterion.setMaxResults(1); List list = mapper.find(criterion); if(list.size() == 0){ return null; }else{ return list.get(0); } } public List find(Criterion criterion) { return this.query(criterion); } public List findAll() { Criterion criterion = CriterionBuilder.create(entityType).build(); return this.query(criterion); } public T insert(T entity) { return mapper.insert(entity); } public int update(T entity) { return mapper.update(entity); } public T save(T entity) { return mapper.save(entity); } public int delete(T entity) { return mapper.delete(entity); } public int delete(Criterion criterion){ return mapper.delete(criterion); } public int deleteById(Object... id) { return mapper.deletebyId(entityType, id); } /////////////////////// SQLMapper 操作 //////////////////////// public R selectOne(String statement) { return this.selectOne(statement, null); } public R selectOne(String statement, Object parameter) { return mapper.selectOne(entityType.getName() + "." + statement, parameter); } public List selectList(String statement) { return mapper.selectList(entityType.getName() + "." + statement, null); } public List selectList(String statement, Object parameter) { return mapper.selectList(entityType.getName() + "." + statement, parameter); } public int insert(String statement, T entity) { return mapper.insert(entityType, entityType.getName() + "." + statement, entity); } public int update(String statement, Object parameter) { return mapper.update(entityType,entityType.getName() + "." + statement,parameter); } public int delete(Class entityClass, String statement, T parameter) { return mapper.delete(entityClass, entityClass.getName() + "." + statement, parameter); } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot集成文件 - 集成POI之Excel导入导出
下一篇:获取年数,季度数.星期数,天数,小时…
相关文章

 发表评论

暂时没有评论,来抢沙发吧~