jpa连接数据库

原文链接

1.XML配置




    
org.apache.openjpa.persistence.PersistenceProviderImpl 

   cn.wiztek.phm.domain.Assets
     cn.wiztek.phm.domain.Rentalreport
   cn.wiztek.phm.domain.Rentinfo
   
   



 
 
 
 







其中 cn.wiztek.phm.domain.Assets为与数据库表对应的Bean路径;

为 数据库的连接信息;

2.创建类

package cn.wiztek.phm.util;

import java.util.logging.Level;
import java.util.logging.Logger;

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

public class EntityManagerHelper {

private static final EntityManagerFactory emf; 
private static final ThreadLocal threadLocal;
private static final Logger logger;

static {
   emf = Persistence.createEntityManagerFactory("PHMSPU");    //该处名称与上面配置文件一致
   threadLocal = new ThreadLocal();
   logger = Logger.getLogger("PHMSPU");
   logger.setLevel(Level.ALL);
}
  
public static EntityManager getEntityManager() {
   EntityManager manager = threadLocal.get();  
   if (manager == null || !manager.isOpen()) {
   
      logger.info("getting EntityManager");
    manager = emf.createEntityManager();
    System.out.println(manager);
    threadLocal.set(manager);
   }
   return manager;
}

public static void closeEntityManager() {
        EntityManager em = threadLocal.get();
        threadLocal.set(null);
        if (em != null && em.isOpen()) {
          logger.info("closing EntityManager");
          em.close();
        }
    }
    
    public static void beginTransaction() {
      logger.info("begining Transaction");
    getEntityManager().getTransaction().begin();
    }
    
    public static void commit() {
      logger.info("commiting transaction");
    getEntityManager().getTransaction().commit();
    } 
    
    public static void rollback() {
      logger.info("rollback transaction");
    getEntityManager().getTransaction().rollback();
    } 
    
    public static Query createQuery(String query) {
   return getEntityManager().createQuery(query);
}

public static void log(String info, Level level, Throwable ex) {
    logger.log(level, info, ex);
    }
}

3.数据库操作

package cn.wiztek.phm.dao.impl;

import java.util.List;
import java.util.logging.Level;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import cn.wiztek.phm.dao.CommonDao;
import cn.wiztek.phm.util.EntityManagerHelper;

public class CommonDaoImpl implements CommonDao {
private EntityManager getEntityManager(){
   return EntityManagerHelper.getEntityManager();
}
/**
* 添加对象操作
* @param 
* @param entityClass
* @param t
* @return
*/
public boolean addObject(Class entityClass,T t){
   EntityManagerHelper.log("addObject "+entityClass.getSimpleName(),Level.INFO,null);
   try{
    getEntityManager().persist(t);
    getEntityManager().flush();
    EntityManagerHelper.log("addObject success",Level.INFO,null);
    return true;
   }catch(Exception e){
    EntityManagerHelper.log("addObject failed",Level.SEVERE,e);
    return false;
   }
}

/**
* 更新对象操作
* @param 
* @param entityClass
* @param t
* @return
*/
public boolean modiObject(Class entityClass,T t){
   EntityManagerHelper.log("modiObject "+entityClass.getSimpleName(),Level.INFO,null);
   try{
    getEntityManager().merge(t);
    EntityManagerHelper.log("modiObject success",Level.INFO,null);
    return true;
   }catch(Exception e){
    EntityManagerHelper.log("modiObject failed",Level.SEVERE,e);
    return false;
   }
}

/**
* 执行sql操作,主要用于更新和删除
* @param sql
* @return
*/
public boolean execSql(String sql){
   EntityManagerHelper.log("execSql sql is : "+sql,Level.INFO,null);
   int num;
   try{
    Query query = getEntityManager().createQuery(sql);
    num = query.executeUpdate();
   }catch(Exception e){
    EntityManagerHelper.log("execSql failed : ",Level.SEVERE,e);
    return false;
   }
   if(num == 0){
    EntityManagerHelper.log("execSql num is 0 ",Level.INFO,null);
   }
   return true;
}

/**
* 根据sql进行查询操作
* @param sql
* @param firse 开始行
* @param size 步长
* @return
*/
@SuppressWarnings("unchecked")
public List queryObject(final String sql,final int first,final int size ){
   EntityManagerHelper.log("queryObject sql is : "+sql,Level.INFO,null);
   try{
    List rlist;
    Query query = getEntityManager().createQuery(sql);
    int nfirst = first < 0 ? 0 : first;
    query.setFirstResult(nfirst);
    if(size >0){
     query.setMaxResults(size);
    }
    rlist = query.getResultList();
    return rlist;
   }catch(Exception e){
    EntityManagerHelper.log("queryObject failed : "+sql,Level.SEVERE,e);
    return null;
   }
}

/**
* 查询条数
* @param sql
* @return
*/
public Integer queryCount(final String sql){
   EntityManagerHelper.log("queryCount sql is : "+sql,Level.INFO,null);
   try{
    Query query = getEntityManager().createQuery(sql);
    return (Integer)query.getSingleResult();
   }catch(Exception e){
    EntityManagerHelper.log("queryCount failed : "+sql,Level.SEVERE,e);
    return 0;
   }
}
}

 

 

 

你可能感兴趣的:(Java,Oracle)