上面是我写这个程序的项目结构图
配置文件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">
<persistence-unit name="lushuifa" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.lushuifa.po.User</class>
<properties>
<property name = "openjpa.ConnectionDriverName" value = "com.mysql.jdbc.Driver"/>
<property name = "openjpa.ConnectionURL" value = "jdbc:mysql://localhost:3306/test"/>
<property name = "openjpa.ConnectionUserName" value = "root"/>
<property name = "openjpa.ConnectionPassword" value = "lushuifa"/>
<property name = "openjpa.jdbc.SynchronizeMappings" value = "buildSchema(ForeignKeys=true)"/>
<!--显示SQL执行语句-->
<property name="openjpa.Log" value="SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
实体bean User.java
package com.lushuifa.po;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User implements Serializable
{
/**/
private static final long serialVersionUID = 1L;
private int userId;
private String userName;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getUserId()
{
return userId;
}
public void setUserId(int userId)
{
this.userId = userId;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
}
dao层,UserDao.java 数据库访问对象
package com.lushuifa.dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.Transaction;
import com.lushuifa.po.User;
public class UserDao
{
private EntityManager entityManager = getEntityManager();
// 拿到EntityManager
public EntityManager getEntityManager()
{
EntityManager em = null;
try
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("lushuifa");
em = emf.createEntityManager();
}
catch (Exception e)
{
System.out.println("获取EntityManager失败了!原因如下:" + e);
}
return em;
}
// 添加用户的方法
public void addUser(User user)
{
System.out.println(this.entityManager);
try
{
this.entityManager.getTransaction().begin();
this.entityManager.persist(user);
this.entityManager.getTransaction().commit();
System.out.println("添加用户成功!");
}
catch (Exception e)
{
System.out.println("添加用户失败,原因如下:");
e.printStackTrace();
}
}
//查询用户的方法
public User findUserById(int id){
User user = null;
try
{
user = this.entityManager.find(User.class,id);
}
catch (Exception e)
{
System.out.println("查询用户有误,原因如下:" + e);
}
return user;
}
public static void main(String[] args)
{
// 测试是否拿到entitymanager
EntityManagerFactory emf = Persistence.createEntityManagerFactory("lushuifa");
EntityManager em = emf.createEntityManager();
System.out.println(em);
}
}
单元测试类
package com.lushuifa.dao;
import org.junit.Test;
import com.lushuifa.po.User;
public class UserDaoTest
{
private UserDao userDao = new UserDao();
@Test
public void testAddUser()
{
User user = new User();
user.setUserName("刘德华");
this.userDao.addUser(user);
}
@Test
public void testFindUserById()
{
User user = this.userDao.findUserById(1);
System.out.println("查出来的名字是:"+user.getUserName());
}
}
测试效果
最后上传的我的项目