参考博文:http://blog.csdn.net/hanhan313/article/details/8125924
1、整体目录
(1)添加一个HibernateUtil类(获取会话)
(2)添加一个BaseDAO类(基本操作)
(3)添加一个测试类
2、HibernateUtil
1 package com.demo.util; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 import org.hibernate.service.ServiceRegistry; 7 import org.hibernate.service.ServiceRegistryBuilder; 8 9 public class HibernateUtil {10 private static SessionFactory sessionFactory;11 12 /**13 * @return 获取会话工厂14 */15 public static SessionFactory getSessionFactory()16 {17 //第一步:读取Hibernate的配置文件 hibernamte.cfg.xml文件18 Configuration con=new Configuration().configure();19 //第二步:创建服务注册构建器对象,通过配置对象中加载所有的配置信息20 ServiceRegistryBuilder regbulider=new ServiceRegistryBuilder().applySettings(con.getProperties());21 //创建注册服务22 ServiceRegistry reg=regbulider.buildServiceRegistry();23 //第三步:创建会话工厂24 SessionFactory sessionFactory=con.buildSessionFactory(reg);25 return sessionFactory;26 }27 28 /**29 * @return 获取会话对象30 */31 public static Session getSession()32 {33 return getSessionFactory().openSession();34 }35 }
3、BaseDAO类
1 package com.demo.dao; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 import org.hibernate.Transaction; 9 10 import com.demo.util.HibernateUtil; 11 12 public class BaseDAO { 13 /** 14 * @param obj 添加数据 15 * @return 16 */ 17 public static boolean add(Object obj) 18 { 19 Session session=null; 20 Transaction tran=null; 21 boolean result=false; 22 try 23 { 24 session=HibernateUtil.getSession(); 25 tran=session.beginTransaction(); 26 session.save(obj); 27 tran.commit(); 28 result=true; 29 } 30 catch (Exception e) 31 { 32 if(tran!=null) 33 { 34 //事物回滚 35 tran.rollback(); 36 } 37 } 38 finally 39 { 40 if(session!=null) 41 { 42 //关闭session 43 session.close(); 44 } 45 } 46 return result; 47 } 48 49 /** 50 * @return 更新数据 51 * 参数为修改的主键id对象 52 */ 53 public static boolean update(Object object) 54 { 55 Session session=null; 56 Transaction tran=null; 57 boolean result=false; 58 try 59 { 60 session=HibernateUtil.getSession(); 61 tran=session.beginTransaction(); 62 session.update(object); 63 tran.commit(); 64 result=true; 65 } 66 catch (Exception e) 67 { 68 if(tran!=null) 69 { 70 //事物回滚 71 tran.rollback(); 72 } 73 } 74 finally 75 { 76 if(session!=null) 77 { 78 //关闭session 79 session.close(); 80 } 81 } 82 return result; 83 } 84 85 /** 86 * @param c 87 * @param id 查询一条数据根据主键的id号 88 * @return 89 */ 90 public static Object get(Class c,int id) 91 { 92 Session session=null; 93 Object object=null; 94 try 95 { 96 session=HibernateUtil.getSession(); 97 object=session.get(c,id); 98 } 99 catch (Exception e)100 {101 }102 finally103 {104 if(session!=null)105 {106 //关闭session107 session.close();108 }109 }110 return object;111 }112 113 /**114 * @param obj115 * @return 删除数据116 */117 public static boolean delete(Object obj)118 {119 Session session=null;120 Transaction tran=null;121 boolean result=false;122 try123 {124 session=HibernateUtil.getSession();125 tran=session.beginTransaction();126 session.delete(obj);127 tran.commit();128 result=true;129 }130 catch (Exception e)131 {132 if(tran!=null)133 {134 //事物回滚135 tran.rollback();136 }137 }138 finally139 {140 if(session!=null)141 {142 //关闭session143 session.close();144 }145 }146 return result;147 }148 149 150 /**151 * @param查询多条记录152 * @param sql sql语句153 * @param param 参数数组154 * @return155 */156 @SuppressWarnings("unchecked")157 public static List query(String sql,String[] param)158 {159 160 List list=new ArrayList ();161 Session session=null;162 try163 {164 session=HibernateUtil.getSession();165 Query query=session.createQuery(sql);166 if(param!=null)167 {168 for(int i=0;i 223 * @param sql224 * @param param225 * @param page226 * @param size227 * @return 实现分页查询228 */229 @SuppressWarnings("unchecked")230 public static List queryByPage(String sql,String[] param,int page,int size)231 {232 List list=new ArrayList ();233 Session session=null;234 try235 {236 session=HibernateUtil.getSession();237 Query query=session.createQuery(sql);238 if(param!=null)239 {240 for(int i=0;i
4、TestHibernateUtil类
1 package com.demo.test; 2 3 import java.util.Iterator; 4 import java.util.List; 5 6 import com.demo.dao.BaseDAO; 7 import com.demo.model.User; 8 9 public class TestHibernateUtil {10 public static void main(String[] args) {11 12 BaseDAO userDao = new BaseDAO();13 //add14 User user1 = new User();15 user1.setUserName("asdf");16 user1.setUserPwd("1234");17 userDao.add(user1);18 System.out.println("添加了一条数据:asdf-->1234");19 //get20 User user2 = (User)userDao.get(User.class, 6);21 System.out.println("用户ID为6的用户数据:"+user2.getUserName()+"-->"+user2.getUserPwd());22 //update23 user2.setUserName("test");24 user2.setUserPwd("789");25 userDao.update(user2);26 System.out.println("用户ID为6的用户数据:"+user2.getUserName()+"-->"+user2.getUserPwd());27 //query all28 String[] para ={"3"};29 Listlist = userDao.query("from User u where u.id=?", para);30 Iterator iter = list.iterator();31 User user3 = null;32 while(iter.hasNext()){33 user3 = (User)iter.next();34 System.out.println(user3.getUserName()+"-->"+user3.getUserPwd());35 }36 37 38 //del by id39 /* User user4 = new User();40 user4.setId(5);41 userDao.delete(user4);42 System.out.println("删除了ID为5的用户!");43 */ }44 }