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

hunt-entity: An object-relational mapping (ORM) framework for dlang's databa ...

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

开源软件名称:

hunt-entity

开源软件地址:

https://gitee.com/huntlabs/hunt-entity

开源软件介绍:

Build Status

hunt-entity

Hunt-entity is an object-relational mapping tool for the D programming language. Referring to the design idea of JPA.

Support databases

  • PostgreSQL 9.0+
  • MySQL 5.1+
  • SQLite 3.7.11+

Depends

Simple code

import hunt.entity;@Table("user")class User{    mixin MakeModel;    @PrimaryKey    @AutoIncrement    int id;    string name;    double money;    string email;    bool status;}void main(){    auto option = new EntityOption;    option.database.driver = "mysql";    option.database.host = "localhost";    option.database.port = 3306;    option.database.database = "test";    option.database.username = "root";    option.database.password = "123456";    option.database.charset = "utf8mb4";    option.database.prefix = "hunt_";    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default", option);    EntityManager em = entityManagerFactory.createEntityManager();    // begin transaction    em.getTransaction().begin();    // define your database existing row id in here    int id = 10;    auto user = em.find!User(id);    log("User name is: ", user.name);    // commit transaction    em.getTransaction().commit();    em.close();    entityManagerFactory.close();}

Insert row

    auto user = new User();    user.name = "Brian";    user.email = "[email protected]";    user.money = 99.9;        // insert user    em.persist(user);    log("User id is: ", user.id);

Delete row

    int n = em.remove!User(id);    log("The number of users deleted is: ", n);

Update row

    auto user = em.find!User(id);    log("User name is: ", user.name);    user.name = "zoujiaqing";    em.merge!User(user);    log("The number of users updated is: ", n);

Use CriteriaQuery to find

    // create CriteriaBuilder object from em    CriteriaBuilder builder = em.getCriteriaBuilder();    CriteriaQuery!User criteriaQuery = builder.createQuery!User;    Root!User root = criteriaQuery.from();    Predicate p1 = builder.equal(root.User.id, id);    TypedQuery!User typedQuery = em.createQuery(criteriaQuery.select(root).where(p1));    auto user = typedQuery.getSingleResult();    log("User name is: ", user.name);

Use CriteriaQuery to Multi-condition find

    // create CriteriaBuilder object from em    CriteriaBuilder builder = em.getCriteriaBuilder();    CriteriaQuery!User criteriaQuery = builder.createQuery!User;    Root!User root = criteriaQuery.from();    Predicate p1 = builder.lt(root.User.id, 1000);  // User id is less than 1000.    Predicate p2 = builder.gt(root.User.money, 0);  // User money is greater than 0.    Predicate p3 = builder.like(root.User.name, "z%");  // User name prefix is z.    TypedQuery!User typedQuery = em.createQuery(criteriaQuery.select(root).where(builder.and(p1, p2), p3));    User[] users = typedQuery.getResultList();    log("The number of users found is: ", users.length);

Avaliable Versions

IdentifierDescription
HUNT_SQL_DEBUGUsed to log debugging messages about SQL handling
HUNT_SQL_DEBUG_MOREUsed to log more debugging messages about SQL handling

鲜花

握手

雷人

路过

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

请发表评论

全部评论

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

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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