要使用DataNucleus,必须要添加其jar包,因为其jar包比较多还涉及jar包依赖关系,所以最好是使用maven进行构建。下面是项目依赖:
<!-- JDO API --> <dependency> <groupId>javax.jdo</groupId> <artifactId>jdo-api</artifactId> <version>3.1-rc1</version> </dependency> <!-- mysql数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.4</version> <scope>test</scope> </dependency> <!-- 下面为DataNucleus所需jar包 --> <dependency> <groupId>org.datanucleus</groupId> <artifactId>datanucleus-core</artifactId> <version>3.0.0-m4</version> </dependency> <dependency> <groupId>org.datanucleus</groupId> <artifactId>datanucleus-enhancer</artifactId> <version>3.0.0-m4</version> </dependency> <dependency> <groupId>org.datanucleus</groupId> <artifactId>datanucleus-api-jdo</artifactId> <version>3.0.0-m4</version> </dependency> <dependency> <groupId>org.datanucleus</groupId> <artifactId>datanucleus-jdo-query</artifactId> <version>3.0.0-m2</version> </dependency> <dependency> <groupId>org.datanucleus</groupId> <artifactId>datanucleus-cache</artifactId> <version>3.0.0-m2</version> </dependency> <dependency> <groupId>org.datanucleus</groupId> <artifactId>datanucleus-rdbms</artifactId> <version>3.0.0-m4</version> </dependency> <dependency> <groupId>org.datanucleus</groupId> <artifactId>datanucleus-management</artifactId> <version>1.0.2</version> </dependency>
<?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <!-- 一个配置文件中可以有多个持久化单元(persistence-unit) RESOURCE_LOCAL表示本地事务 --> <persistence-unit name="xtayfjpk" transaction-type="RESOURCE_LOCAL"> <-- 包含一个Person实体 --> <class>com.xtayfjpk.jdo.entity.Person</class> <exclude-unlisted-classes/> <properties> <property name="datanucleus.ConnectionDriverName" value="com.mysql.jdbc.Driver"/> <property name="datanucleus.ConnectionURL" value="jdbc:mysql://127.0.0.1:3306/test"/> <property name="datanucleus.ConnectionUserName" value="root"/> <property name="datanucleus.ConnectionPassword" value="******"/> <property name="datanucleus.autoCreateSchema" value="true"/> </properties> </persistence-unit> </persistence>
<property name="javax.jdo.option.ConnectionDriverName" value="com.mysql.jdbc.Driver"/> <property name="javax.jdo.option.ConnectionURL" value="jdbc:mysql://127.0.0.1:3306/test"/> <property name="javax.jdo.option.ConnectionUserName" value="root"/> <property name="javax.jdo.option.ConnectionPassword" value="******"/>
package com.xtayfjpk.jdo.entity; import javax.jdo.annotations.Column; import javax.jdo.annotations.PersistenceCapable; import javax.jdo.annotations.PrimaryKey; @PersistenceCapable(table="JDO_PERSON") public class Person { @PrimaryKey @Column(name="P_ID") //如果ID要自增长请加上@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) private Integer id; @Column(name="P_NAME") private String name; @Column(name="P_AGE") private int age; @Column(name="P_EMAIL") private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + ", email=" + email + "]"; } }
package com.xtayfjpk.jdo; import javax.jdo.JDOHelper; import javax.jdo.PersistenceManager; import javax.jdo.PersistenceManagerFactory; import javax.jdo.Transaction; import org.junit.Before; import org.junit.Test; import com.xtayfjpk.jdo.entity.Person; /** * Unit test for JDO. */ public class JDOTest { private static PersistenceManagerFactory pmf = null; @Before public void before() { /*Properties properties = new Properties(); properties.setProperty("javax.jdo.option.ConnectionDriverName","com.mysql.jdbc.Driver"); properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:mysql://127.0.0.1:3306/test"); properties.setProperty("javax.jdo.option.ConnectionUserName","root"); properties.setProperty("javax.jdo.option.ConnectionPassword","xtayfjpk"); properties.setProperty("javax.jdo.option.Optimistic","true"); properties.setProperty("datanucleus.autoCreateSchema","true");*/ //getPersistenceManagerFactory有很多重载的方法,具体请参看API pmf = JDOHelper.getPersistenceManagerFactory("xtayfjpk"); } @Test public void testPersistenceManageFactory() { System.out.println(pmf); } @Test public void testSave() { PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); Person person = new Person(); person.setId(1); person.setName("王五"); person.setAge(40); person.setEmail("wangwu@qq.com"); Person p = pm.makePersistent(person); tx.commit(); } catch (Exception e) { e.printStackTrace(); if (tx.isActive()) { tx.rollback(); } } finally { pm.close(); pmf.close(); } } }
然后添加DataNucleus支持(add DataNucleus support),再选择enabel Auto-Ehancement启用自动增强。
DataNucleus之JDO操作例子,布布扣,bubuko.com
原文地址:http://blog.csdn.net/xtayfjpk/article/details/25320499