Openjpa开发

一、Openjpa下载

       http://openjpa.apache.org/

 
二、开发jar包:
      openjpa-all-2.3.0.jar(我的版本是2.3.0)
三、Openjpa开发需要配置一个persistence.xml文件
       我使用的mysql,所以配置文件配置的数据库连接是mysql的连接
      
 <?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"
    version="1.0">
    <persistence-unit name="hellojpa" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.apache.openjpa.persistence.PersistenceProviderImpl
        </provider>
        <!--实体bean--> 
        <class>entity.User</class>
        <properties>
            <property name = "openjpa.ConnectionDriverName" value = "com.mysql.jdbc.Driver"/>
   	    <property name = "openjpa.ConnectionURL" value = "jdbc:mysql://localhost:3306/beifengwenku?useUnicode=true&amp;characterEncoding=utf8"/>
            <property name = "openjpa.ConnectionUserName" value = "root"/>
            <property name = "openjpa.ConnectionPassword" value = "ecology"/>
            <property name = "openjpa.jdbc.SynchronizeMappings" value = "buildSchema(ForeignKeys=true)"/>
            <!--显示SQL执行语句-->
            <property name="openjpa.Log" value="SQL=TRACE"/>
            <property name="openjpa.RuntimeUnenhancedClasses" value="supported"></property>
        </properties>
    </persistence-unit>
</persistence>
 
四、构建实体bean
 
 
 
package entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @Entity注解申明实体bean
 * @Table(name="usertable") 关联数据库表
 * @Id 申明字段对应的数据库主键
 */
@Entity
@Table(name="usertable")
public class User implements Serializable{
	
	private static final long serialVersionUID = 1L;
	@Id
	private String userId;
	private String username;
	private String password;
	private String email;
	private Date registtime;
	private int experience;
	private int wealth;
	private int isvalidation;
	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getRegisttime() {
		return registtime;
	}
	public void setRegisttime(Date registtime) {
		this.registtime = registtime;
	}
	public int getExperience() {
		return experience;
	}
	public void setExperience(int experience) {
		this.experience = experience;
	}
	public int getWealth() {
		return wealth;
	}
	public void setWealth(int wealth) {
		this.wealth = wealth;
	}
	public int getIsvalidation() {
		return isvalidation;
	}
	public void setIsvalidation(int isvalidation) {
		this.isvalidation = isvalidation;
	}
	
}

5、编写dao层
package dao;

import java.util.List;

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

import entity.User;

public class UserDAO {

	private EntityManager entityManager = getEntityManager();
	
	// 拿到EntityManager
	public EntityManager getEntityManager()
	{
		EntityManager em = null;
		try
		{
			EntityManagerFactory emf = Persistence.createEntityManagerFactory("hellojpa");
			em = emf.createEntityManager();
		}
		catch (Exception e)
		{
			System.out.println("获取EntityManager失败了!原因如下:" + e);
		}
		return em;
	}
	
	public void addUser(User user){
		//根据名字找到配置项,从而初始化一个实体管理工厂的对象
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("hellojpa", System.getProperties());
		//通过实体管理工厂返回一个实体管理器
		EntityManager em = factory.createEntityManager();
		//打开事物
		em.getTransaction().begin();
		//添加数据
		em.persist(user);
		//提交事物
		em.getTransaction().commit();
		
	}
	/**
         *Openjpa删除数据首先必须获取实体user才能进行删除
         *不能通过id进行删除
         */
 
	public void deleteUser(User user){
		this.entityManager.getTransaction().begin();
		this.entityManager.remove(user);
		this.entityManager.getTransaction().commit();
	}
	
	public void updateUser(User user){
		this.entityManager.getTransaction().begin();
		this.entityManager.merge(user);
		this.entityManager.getTransaction().commit();
	}
	
	public User queryUserById(String userId){
		return this.entityManager.find(User.class, userId);
	}
	
	/**
	*  sql查询语句的语法为:select u from User u where u.userId='xxxx'
	*  针对实体bean查询 而不是数据表查询
	*/
	public List<User> queryListBySQL(String sql){
		Query query=this.entityManager.createQuery(sql);
		return query.getResultList();
	}
}
 

 

你可能感兴趣的:(openjpa)