• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jpa: 封装hibernate统一操作接口

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

jpa

开源软件地址:

https://gitee.com/xnat/jpa

开源软件介绍:

介绍

jpa: 封装hibernate

安装教程

<dependency>    <groupId>cn.xnatural</groupId>    <artifactId>jpa</artifactId>    <version>1.0.9</version></dependency>

用法

创建一个Repo

//1. 根据jdbcUrl 创建Repo repo = new Repo("jdbc:mysql://localhost:3306/test?user=root&password=root").init();
//2. 自定义添加属性Repo repo = new Repo("jdbc:mysql://localhost:3306/test", "root", "root", 0, 5)        .setAttr("hibernate.hbm2ddl.auto", "update")        .entities(Db.class).init();
//3. 根据属性集创建Map<String, Object> attrs = new HashMap<>();attrs.put("jdbcUrl", "jdbc:mysql://localhost:3306/test?user=root&password=root");attrs.put("hibernate.hbm2ddl.auto", "update"); //update: 自动根据实体更新表结构, none: 不更新Repo repo = new Repo(attrs).entities(Db.class).init();

实体查询

/** * 自定义 实体 */@Entity@Table(name = "Db")public class Db implements IEntity {    @Id    public String Db;    public String Host;}
import cn.xnatural.jpa.UUIDEntity;/** * 无符号 '-' uuid实体  */@Entitypublic class Test1 extends UUIDEntity {    public String name;}
import cn.xnatural.jpa.LongIdEntity;/** * 自增长 long id 实体 */@Entitypublic class Test2 extends LongIdEntity {    public String name;}

查询一个实体

Db db = repo.find(Db.class, (root, query, cb) -> cb.equal(root.get("Db"), "sys"));

分页查询实体

Page<Db> pageData = repo.findPage(Db.class, 1, 10, (root, query, cb) -> cb.equal(root.get("Db"), "sys"));

其它实体方法

// 保存或更新实体repo.saveOrUpdate(实体对象)// 根据id查询实体repo.findById(实体Class, id值)// 删除一个实体repo.delete(实体对象)// 查询多个实体repo.findList(实体Class, 条件)// 统计实体个数repo.count(实体Class, 条件(可选))

hql查询

查询单个值

Long count = repo.hqlFirstRow("select count(1) as total from Db", Long.class);

查询一条记录

Map record = repo.hqlFirstRow("select new map(Host as host,User as user) from User where User=:user", Map.class, "root");

查询一个实体

User record = repo.hqlFirstRow("select new entity.User(Host,User) from User where User=:user", User.class, "root");

查询多个单值

List<Long> ls = repo.hqlRows("select count(1) as total from Db", Long.class);

查询多条记录

List<Map> records = repo.hqlRows("select new map(Host as host,User as user) from User where User=:user", Map.class, "root");

查询多个实体

List<User> users = repo.hqlRows("select new entity.User(Host,User) from User where User=:user", User.class, "root");

原生sql操作

查询一条数据

// 1. 无参repo.firstRow("select count(1) as total from db").get("total")// 2. 传参Map<String, Object> result = repo.firstRow("select * from db where Db=?", "sys");// 3. 包装结果Db result = repo.firstRow("select * from db where Db=?", Db.class, "sys");

查询多条数据

// 1. 默认返回List<Map>List<Map<String, Object>> results = repo.rows("select * from db limit ?", 10);// 2. 指定返回结果List<Db> results = repo.rows("select * from db where Db=?", Db.class, "sys");// 3. 分页查询Page<Db> pageData = repo.sqlPage("select * from db where Db=?", 1, 10, Db.class, "sys");// 4. 命名参数(in条件查询)List<Map<String, Object>> results = repo.rows("select * from db where Db = :db and Db in (:ids)", "sys", Arrays.asList("sys"));

更新,插入,删除

// 1. 更新repo.execute("update test set age=? where id=?", 11, "4028b881766f3e5801766f3e87ba0000")// 2. 插入repo.execute("insert into test values(?,?,?,?,?)", UUID.randomUUID().toString().replace("-", ""), new Date(), new Date(), 22, "name")// 3. 删除repo.execute("delete from test where id=?", "ad3e4ff8f3fd4171aeeb9dd2c0aa6f0c")

自定义操作

// 1. 其它自定义查询repo.trans(session -> {    // TODO    // session.createQuery(hql);// hql查询    return null;})// 2. 事务成功/失败回调repo.trans(session -> {    // TODO    // session.createQuery(hql);// hql查询    return null;}, () -> {    // TODO 成功执行}, (ex) -> {    // TODO 失败执行})

其它实用方法

// 查询实体映射的表名repo.tbName(实体Class);// 得到当前的连接jdbcUrlrepo.getJdbcUrl();// 得到当前连接的数据库名(mysql)repo.getDbName();// 得到当前数据库的版本信息repo.getDBVersion();

参与贡献

[email protected]


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap