获取 EntityManager的工具类 及JPA+Struts2 整合中关闭 entityManager
public class JPAUtil {
private static final String persistenceName = "ConsolePU";
private static EntityManagerFactory entityManagerFactory;
static {
try {
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceName);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("entityManagerFactory init exception...");
}
}
public static EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}
}
public class JPAUtilAndThread {
private static final String persistenceName = "ConsolePU";
private static EntityManagerFactory entityManagerFactory;
private static ThreadLocal
static {
try {
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceName);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("entityManagerFactory init exception...");
}
}
// threadLocal 中取值
public static EntityManager getEntityManager() {
EntityManager entityManager = threadLocal.get();
if(entityManager == null || !entityManager.isOpen()) {
entityManager = entityManagerFactory.createEntityManager();
threadLocal.set(entityManager);
}
return entityManager;
}
// 释放资源
public static void close() {
EntityManager entityManager = threadLocal.get();
if(entityManager != null && entityManager.isOpen()) {
entityManager.close();
threadLocal.remove();
}
}
}
一、BaseDao
public interface IBaseDao
void save(T t);
void update(T t);
void delete(Long id);
T getById(Long id);
List
}
public class BaseDaoImpl
//构造一个Class的字节码对象,需要从子类构造方法中传入
private Class
public BaseDaoImpl(Class
super();
this.entityClass = entityClass;
}
@Override
public void save(T t) {
EntityManager entityManager = JPAUtil.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(t);
transaction.commit();
entityManager.close();
}
@Override
public void update(T t) {
EntityManager entityManager = JPAUtil.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.merge(t);
transaction.commit();
entityManager.close();
}
@Override
public void delete(Long id) {
EntityManager entityManager = JPAUtil.getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
T t = entityManager.find(entityClass, id);
if(t != null) {
entityManager.remove(t);
}
transaction.commit();
entityManager.close();
}
@Override
public T getById(Long id) {
EntityManager entityManager = JPAUtil.getEntityManager();
T t = entityManager.find(entityClass, id);
entityManager.close();
return t;
}
@Override
public List
EntityManager entityManager = JPAUtil.getEntityManager();
String jpql = "select o from "+entityClass.getName()+" o";
Query query = entityManager.createQuery(jpql);
List
entityManager.close();
return list;
}
}
二、实现类
public class StudentDao extends BaseDaoImpl
public StudentDao() {
super(Student.class);
}
}
三、测试类
public class MyTest02 {
StudentDao stuDao = new StudentDao();
@Test
public void test() {
//查
List
System.out.println(list.get(0).toString());
//级联添加
Grade grade = new Grade();
grade.setGname("国际班商务班");
Student stu = new Student();
stu.setSname("强哥");
stu.setSex("old boy");
stu.setGrade(grade);
stuDao.save(stu);
}
}