D 语言的 ORM 框架 Entity

Apache
D语言 查看源码»
跨平台
2018-05-14
冰力

Entity 是 D 语言(dlang)数据库操作 ORM 框架,设计参照 javax JPA 实现,具有很好的扩展性和规范性,数据库驱动依赖 Huntlabs 开发的 database for dlang

特性

  1. 实现了完整的 CriteriaQuery 支持

  2. Repository 也有部分封装

  3. 完全对象化查询,不用考虑底层数据库驱动

支持的数据库

  • PostgreSQL 9.0+

  • MySQL 5.1+

  • SQLite 3.7.11+

使用示例

import entity;

@Table("user")
class User : Entity
{
    @PrimaryKey
    @AutoIncrement
    int id;

    string name;
    double money;
    string email;
    bool status;
}

void main()
{
    DatabaseOption options = new DatabaseOption("mysql://root:123456@localhost:3306/huntblog?charset=utf-8");
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default", options);
    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 = "brian@huntlabs.cn";
    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);
加载中

评论(1)

晴朗320
晴朗320
现在搞D, 用什么编辑器呢?

暂无资讯

暂无问答

How to update BOL entity property value via ABAP code

Suppose I have one product with ID I042416 which could be found in CRM WebClient UI: I would like to change its description from "i042416" to for example "Jerry test". Here be...

昨天 14:26
0
0
JPA 2.1: Entity Graph

# JPA 2.1: Entity Graph Entity Graph is a means to specify the structure of a graph of entities, it defines the return path and boundaries when the entity is loaded. ## Define E...

2013/12/03 20:06
143
0
Entity Framework资料搜集

personal blog of Rowan Miller The program manager for the Entity Framework team, is a must-read for any developer working on an Entity Framework-based application. Managing DbCo...

2016/05/31 07:19
51
0
EntitySystem

http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/ http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/ entity system o...

2012/03/09 14:09
32
0
Entity Component System

http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/ http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/ entity system o...

2014/05/28 11:29
172
0
mybatis基于Provider的通用CRUD

mybatis基于Provider的通用CRUD,参考《 MyBatis3 通用CRUD的研究[一]》

2016/02/22 18:05
272
1
Auditing with Hibernate Envers

#Auditing with Hibernate Envers The jpa lifecyle hook method and Spring Data auditing only store the creation and last modification info of an Entity, but all the modification h...

2013/11/24 23:47
156
0
Tao of Spring Roo

#Tao of Spring Roo # As you see, Spring Roo is not only a simple scollad tool for code generation, it also try to introduce a new programming model. ##AspectJ and AOP programmin...

2013/05/27 18:59
50
0
magento eav model study

eav_entity_type: customer , customer_address, catalog_category, catalog_product, sales, invoice, creditmemo, shipment. 每一个产品都是“catalog_product_entity”中的一行。Magento...

08/07 14:24
6
0
Using an entity resolver

Most XML and HTML developers are familiar with entity references, the odd little XML constructs you often see that begin with an ampersand (&) and end with a semicolon (;). Prob...

09/13 14:37
4
0

没有更多内容

加载失败,请刷新页面

返回顶部
顶部