Persistence4J 介绍
persistence4j 是一个小型、轻量级的 Java 对象持久层类库,实现关系数据库和 Java 对象之间的持久化。
示例代码:
//First lets create a simple pojo which you like to persist. @Entity(table="book") public class Book{ @Column(isPrimaryKey=true) private String isbn; @Column private String title; @Column private int authorid; public Book(){ } public Book(String isbn, String title, int authorid){ this.isbn = isbn; this.title = title; this.authorid = authorid; } // getters } DataProviderFactory dataProviderFactory = new DataProviderFactoryImpl(config); String databaseName = "library"; String dbmsName = "MysqL" boolean isTransactional = false; DataProvider dataProvider = dataProviderFactory.getDataProvider(databaseName, dbmsName, isTransactional); // Now lets create a object of Book class and persist it Book book = new Book("123432","TestBook",5); TransferUtil.registerClass(Book.class, database); GenericDAO<Book> genericDAO = new GenericDaoImpl<Book>(dataProvider.getDataFetcher()); //Persist Book genericDAO.createEntity(book); //Remove Book genericDAO.deleteEntity(book); //Test if Entity Exists genericDAO.isEntityExists(book); // findByPrimaryKey Object obj[] = new Object[1]; obj[0] = "123432"; genericDAO.findByPrimaryKey(Book.class, obj); //If you want to use transactions.This how to get TransactionService.Make sure //isTransactional variable should be true and underlying dbms supports ACID. TransactionService ts = dataProvider.getTransactionService(); try{ ts.beginTransaction(); genericDAO.createEntity(book); ts.commitTransaction(); }catch(Exception exp){ ts.rollbackTransaction(); }
Persistence4J 官网
http://code.google.com/p/persistence4j/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。