hibernate dao 封装
整体结构:
com.it.bean
Student.java中字段
private String stu_id;
private String stu_pwd;
private String stu_name;
private String stu_sex;
private String stu_age;
private String stu_addr;
student.hbm.xml
com.it.dao
BaseDAO.java
package com.it.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.it.bean.Student;
public class BaseDAO<E> {
private Session session;
public BaseDAO(Session session) {
this.session=session;
}
/**
* 添加
* @param e
*/
public void add(E e){
session.save(e);
}
/**
* 删除
* @param e
*/
public void del(E e){
session.delete(e);
}
/**
* 修改
* @param e
*/
public void upd(E e){
session.update(e);
}
/**
* 查全部
* @param hql
* @return
*/
public List<E> findAll(String hql){
Query query=session.createQuery(hql);
return query.list();
}
/**
* 分页查
* @param hql
* @param currentPage
* @param currentsize
* @return
*/
public List<E> findFenYe(String hql,int currentPage,int currentsize){
Query query=session.createQuery(hql);
int startrec=(currentPage-1)*currentsize;
query.setFetchSize(startrec);
query.setMaxResults(currentsize);
return query.list();
}
/**
* 模糊查
* @param hql
* @param stu
* @param params
* @return
*/
public List<E> findMoHu(String hql,Student stu,String...params){
Query query=session.createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setString(i, params[i]);
}
return query.list();
}
/**
* 模糊分页查
* @param hql
* @param currentPage
* @param currentSize
* @param stu
* @param params
* @return
*/
public List<E> findMoFen(String hql,int currentPage,int currentSize,Student stu,String...params){
Query query=session.createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setString(i, params[i]);
}
int startRec=(currentPage-1)*currentSize;
query.setFetchSize(startRec);
query.setMaxResults(currentSize);
return query.list();
}
}
StudentDAO.java
package com.it.dao;
import java.util.List;
import org.hibernate.Session;
import com.it.bean.Student;
public class StudentDAO extends BaseDAO<Student>{//添加
public void add(Student stu){
super.add(stu);
}
//删除
public void del(Student stu){
super.del(stu);
}
//修改
public void upd(Student stu){
super.upd(stu);
}
//查全部
public List<Student> findAll(){
String hql="select new List(stu_id,stu_pwd,stu_name,stu_sex,stu_age,stu_addr)from Student stu";
return super.findAll(hql);
}
//分页查
public List<Student> findFenYe(int currentPage,int currentSize){
String hql="select new List(stu_id,stu_pwd,stu_name,stu_sex,stu_age,stu_addr)from Student stu";
return super.findFenYe(hql, currentPage, currentSize);
}
//模糊查
public List<Student> findMoHu(Student stu){
String hql="from Student stu where stu_id like ? and stu_pwd like ? and stu_name like ? and stu_sex like ? and stu_age like ? and stu_addr like ? ";
String []params={"%"+stu.getStu_id()+"%","%"+stu.getStu_pwd()+"%","%"+stu.getStu_name()+"%","%"+stu.getStu_sex()+"%","%"+stu.getStu_age()+"%","%"+stu.getStu_addr()+"%"};
return super.findMoHu(hql, stu, params);
}
//模糊分页查
public List<Student> findMoFen(int currentPage,int currentSize,Student stu){
String hql="from Student stu where stu_id like ? and stu_pwd like ? and stu_name like ? and stu_sex like ? and stu_age like ? and stu_addr like ? ";
String []params={"%"+stu.getStu_id()+"%","%"+stu.getStu_pwd()+"%","%"+stu.getStu_name()+"%","%"+stu.getStu_sex()+"%","%"+stu.getStu_age()+"%","%"+stu.getStu_addr()+"%"};
return super.findMoFen(hql, currentPage, currentSize, stu, params);
}
public StudentDAO(Session session) {
super(session);
// TODO Auto-generated constructor stub
}
}
com.it.service
StudentService.java
package com.it.service;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.it.bean.Student;
import com.it.dao.StudentDAO;
import com.it.util.CreateSessionFactoryUtil;
public class StudentService {
private SessionFactory sessionFactory=null;
private Session session=null;
private Transaction tx=null;
private List<Student> list=null;
/**
* 增加
* @param stu
*/
public void add(Student stu){
try {
sessionFactory=CreateSessionFactoryUtil.CreateSessionFactory();
session=sessionFactory.getCurrentSession();
tx=session.beginTransaction();
new StudentDAO(session).add(stu);
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
System.out.println("==回滚成功==");
} catch (Exception e2) {
System.out.println("==回滚失败==");
}
}
}
/**
* 删除
* @param stu
*/
public void del(Student stu){
try {
sessionFactory=CreateSessionFactoryUtil.CreateSessionFactory();
session=sessionFactory.getCurrentSession();
tx=session.beginTransaction();
new StudentDAO(session).del(stu);
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
System.out.println("==回滚成功==");
} catch (Exception e2) {
System.out.println("==回滚失败==");
}
}
}
/**
* 修改
* @param stu
*/
public void upd(Student stu){
try {
sessionFactory=CreateSessionFactoryUtil.CreateSessionFactory();
session=sessionFactory.getCurrentSession();
tx=session.beginTransaction();
new StudentDAO(session).upd(stu);
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
System.out.println("==回滚成功==");
} catch (Exception e2) {
System.out.println("==回滚失败==");
}
}
}
/**
* 查全部
* @return
*/
public List<Student> findAll(){
try {
sessionFactory=CreateSessionFactoryUtil.CreateSessionFactory();
session=sessionFactory.getCurrentSession();
tx=session.beginTransaction();
list=new StudentDAO(session).findAll();
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
System.out.println("==回滚成功==");
} catch (Exception e2) {
System.out.println("==回滚失败==");
}
}
return list;
}
/**
* 分页查
* @param currentPage
* @param currentsize
* @return
*/
public List<Student> findFenYe(int currentPage,int currentSize){
try {
sessionFactory=CreateSessionFactoryUtil.CreateSessionFactory();
session=sessionFactory.getCurrentSession();
tx=session.beginTransaction();
list=new StudentDAO(session).findFenYe(currentPage, currentSize);
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
System.out.println("==回滚成功==");
} catch (Exception e2) {
System.out.println("==回滚失败==");
}
}
return list;
}
/**
* 模糊查
* @param stu
* @return
*/
public List<Student> findMoHu(Student stu){
try {
sessionFactory=CreateSessionFactoryUtil.CreateSessionFactory();
session=sessionFactory.getCurrentSession();
tx=session.beginTransaction();
list=new StudentDAO(session).findMoHu(stu);
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
System.out.println("==回滚成功==");
} catch (Exception e2) {
System.out.println("==回滚失败==");
}
}
return list;
}
/**
* 模糊分页查
* @param currentPage
* @param currentSize
* @param stu
* @return
*/
public List<Student> findMoFen(int currentPage,int currentSize,Student stu){
try {
sessionFactory=CreateSessionFactoryUtil.CreateSessionFactory();
session=sessionFactory.getCurrentSession();
tx=session.beginTransaction();
list=new StudentDAO(session).findMoFen(currentPage, currentSize, stu);
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
System.out.println("==回滚成功==");
} catch (Exception e2) {
System.out.println("==回滚失败==");
}
}
return list;
}
}
com.it.util
CreateSessionFactoryUtil.java
sessionFactory属于大对象使用单例设计模式中的饿汉设计模式
session属于会话--一次会话包含 多个操作
SessionFactory:cfg.xml--->大对象的创建
多个session依赖一个SessionFactory--单例
package com.it.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class CreateSessionFactoryUtil {
private static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
public CreateSessionFactoryUtil() {
}
public static SessionFactory CreateSessionFactory(){
return sessionFactory;
}
}
src下
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">cq</property>
<property name="connection.password">123456</property>
<!--SQL dialect 方言-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!--Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!--Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="com/it/bean/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>