基于maven的openjpa简单例子

一:创建maven项目jpa

整体项目构造如下:

基于maven的openjpa简单例子_第1张图片


二:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.lala</groupId>
	<artifactId>jpa</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<name>jpa</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.openjpa</groupId>
			<artifactId>openjpa</artifactId>
			<version>2.4.0</version>
		</dependency>
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>9.3-1103-jdbc41</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
	<persistence-unit name="OpenJpaPP" transaction-type="RESOURCE_LOCAL">
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<class>com.lala.bean.User</class>
		<properties>
			<property name="openjpa.ConnectionURL" value="jdbc:postgresql://127.0.0.1/db_cloud" />
			<property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver" />
			<property name="openjpa.ConnectionUserName" value="admin" />
			<property name="openjpa.ConnectionPassword" value="root" />
			
			<!-- 自动生成表 -->
			<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
		</properties>
	</persistence-unit>
</persistence>


User.java

package com.lala.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "pp_users")
public class User 
{
	@Id
	@Column(name = "user_id")  
	@GeneratedValue(strategy = GenerationType.TABLE)
	private Long userId;
	
	@Column(name = "user_name")  
	private String userName;
	
	private Integer status;
	
	private String account;
	
	private String email;
	
	@Column(name = "phone_num")
	private String phoneNum ;

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}
}

App.java

package com.lala.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.lala.bean.User;

public class App 
{
    public static void main( String[] args )
    {
    	EntityManagerFactory emf = Persistence.createEntityManagerFactory("OpenJpaPP");
    	
    	EntityManager em = emf.createEntityManager();
    	
    	User user = new User();
    	user.setAccount("admin");
    	user.setEmail("[email protected]");
    	user.setPhoneNum("15619301011");
    	user.setStatus(1);
    	user.setUserName("管理员");
    	
    	em.getTransaction().begin();
    	
    	em.persist(user);
    	
    	em.getTransaction().commit();
    	
    	em.close();
    	emf.close();
    }
}


你可能感兴趣的:(maven,openjpa)