DataNucleus用法之Mysql

DataNucleus提供了一些示例代码,http://sourceforge.net/projects/datanucleus/files/datanucleus-samples/

其中有 datanucleus-samples-jpa-tutorial-3.2-src.zip 和 datanucleus-samples-jdo-tutorial-3.2-src.zip

解压这两个文件,可以得到JPA与JDO访问的示例代码。

关于这两个示例的介绍在http://www.datanucleus.org/products/accessplatform_2_1/guides/jpa/tutorial.html , http://www.datanucleus.org/products/accessplatform_2_1/guides/jdo/tutorial.html


对于RDBMS, 原代码中使用的是hsqldb,这里改为mysql,具体修改如下:


JPA访问

=============================================

META-INF/persistence.xml:

<?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_1_0.xsd" version="1.0">

    <!-- Tutorial "unit" -->
    <persistence-unit name="Tutorial">
	<mapping-file>org/datanucleus/samples/jpa/tutorial/orm.xml</mapping-file>
        <class>org.datanucleus.samples.jpa.tutorial.Inventory</class>
        <class>org.datanucleus.samples.jpa.tutorial.Product</class>
        <class>org.datanucleus.samples.jpa.tutorial.Book</class>
        <properties>
            <!-- ENABLE THESE FOR RDBMS -->
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="las"/>
            <property name="javax.persistence.jdbc.password" value="las"/>

            <property name="datanucleus.autoCreateSchema" value="true"/>
        </properties>
    </persistence-unit>

</persistence>


pom.xml:

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.20</version>
</dependency>

编译运行过程:

1. Run the command: "mvn clean compile"
   This builds everything, and enhances the classes

2. Run the command: "mvn datanucleus:schema-create"
   This creates the schema for this sample.

3. Run the command: "mvn exec:java"
   This runs the tutorial

4. Run the command: "mvn datanucleus:schema-delete"
   This deletes the schema for this sample.


JDO访问

======================================================

datanucleus.properties:

javax.jdo.option.ConnectionDriverName=com.mysql.jdbc.Driver
javax.jdo.option.ConnectionURL=jdbc:mysql://127.0.0.1/jpa?useServerPrepStmts=false
javax.jdo.option.ConnectionUserName=las
javax.jdo.option.ConnectionPassword=las
javax.jdo.option.Mapping=mysql

datanucleus.metadata.validate=false
datanucleus.autoCreateSchema=true
datanucleus.validateTables=false
datanucleus.validateConstraints=false

pom.xml:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.20</version>
</dependency>

编译运行过程:

1. Run the command: "mvn clean compile"
   This builds everything, and enhances the classes

2. Run the command: "mvn datanucleus:schema-create"
   This creates the schema for this sample.

3. Run the command: "mvn exec:java"
   This runs the tutorial

4. Run the command: "mvn datanucleus:schema-delete"
   This deletes the schema for this sample.


整理后的maven工程在:http://download.csdn.net/detail/sundongsdu/4978335


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值