openJpa的应用,感觉还可以


 


 上面是我写这个程序的项目结构图

 

配置文件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());
 }
 
}

 

测试效果



 


最后上传的我的项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值