一、jar包
最基础的hibernatejar包,以及数据库驱动的jar包
二、数据库
1 t_user表 2 id int 主键 自动增长 3 name varchar(20)
三、配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 配置数据库信息 --> 8 <property name="hibernate.connection.username">root</property> 9 <property name="hibernate.connection.password">mysql123</property> 10 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 11 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property> 12 <property name="hibernate.connection.autocommit">true</property> 13 14 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 15 <property name="hibernate.hbm2ddl.auto">update</property> 16 <!-- 显示生成的sql语句 --> 17 <property name="hibernate.show_sql">true</property> 18 19 <mapping resource="cn/itcast/domain/User.hbm.xml"/> 20 </session-factory> 21 </hibernate-configuration>
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.itcast.domain"> 6 <class name="User" table="t_user"> 7 <id name="id" type="int"> 8 <generator class="native" /> 9 </id> 10 <property name="name" type="string"/> 11 </class> 12 </hibernate-mapping>
四、程序
1、主要是负责session的开启,属于模板代码
1 package cn.itcast.domain.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 7 public class HibernateUtils { 8 9 private static SessionFactory sessionFactory; 10 static{ 11 sessionFactory=new Configuration() 12 .configure() 13 .buildSessionFactory(); 14 } 15 public static SessionFactory getSessionFactory(){ 16 return sessionFactory; 17 } 18 public static Session openSession(){ 19 return sessionFactory.openSession(); 20 } 21 }
2、定义一个接口,声明增删改查的方法
1 package cn.itcast.domain.dao; 2 3 import java.util.List; 4 import cn.itcast.domain.User; 5 6 public interface UserDao { 7 8 public void save(User user); 9 public void update(User user); 10 public void delete(int id); 11 public User getById(int id); 12 public List<User> findAll(); 13 public QueryResult findAll(int firstNumber, int maxNumber); 14 }
3、对接口创建实现类
1 package cn.itcast.domain.dao; 2 3 import java.util.List; 4 5 import org.hibernate.Session; 6 import org.hibernate.Transaction; 7 import cn.itcast.domain.User; 8 9 public class UserDaoImpl implements UserDao { 10 11 @Override 12 public void save(User user) { 13 // TODO Auto-generated method stub 14 Session session = HibernateUtils.openSession(); 15 Transaction tx= null; 16 try{ 17 tx=session.beginTransaction(); 18 session.save(user); 19 tx.commit(); 20 }catch(RuntimeException exception){ 21 tx.rollback(); 22 throw exception; 23 }finally{ 24 session.close(); 25 } 26 } 27 28 @Override 29 public void update(User user) { 30 // TODO Auto-generated method stub 31 Session session = HibernateUtils.openSession(); 32 Transaction tx=null; 33 try{ 34 tx=session.beginTransaction(); 35 session.update(user); 36 tx.commit(); 37 }catch(RuntimeException exception){ 38 tx.rollback(); 39 throw exception; 40 }finally{ 41 session.close(); 42 } 43 } 44 45 @Override 46 public void delete(int id) { 47 // TODO Auto-generated method stub 48 Session session = HibernateUtils.openSession(); 49 Transaction tx= null; 50 try{ 51 tx=session.beginTransaction(); 52 Object user = session.get(User.class, id); 53 session.delete(user); 54 tx.commit(); 55 }catch(RuntimeException exception){ 56 tx.rollback(); 57 throw exception; 58 }finally{ 59 session.close(); 60 } 61 } 62 63 @Override 64 public User getById(int id) { 65 // TODO Auto-generated method stub 66 Session session = HibernateUtils.openSession(); 67 Transaction tx= null; 68 try{ 69 tx=session.beginTransaction(); 70 User user =(User) session.get(User.class, id); 71 tx.commit(); 72 return user; 73 }catch(RuntimeException exception){ 74 tx.rollback(); 75 throw exception; 76 }finally{ 77 session.close(); 78 } 79 } 80 81 @Override 82 public List<User> findAll() { 83 // TODO Auto-generated method stub 84 Session session = HibernateUtils.openSession(); 85 Transaction tx= null; 86 try{ 87 tx=session.beginTransaction(); 88 List<User> user = session.createQuery(" FROM User").list(); 89 tx.commit(); 90 return user; 91 }catch(RuntimeException exception){ 92 tx.rollback(); 93 throw exception; 94 }finally{ 95 session.close(); 96 } 97 } 98 99 /** 100 * 分页查询 101 */ 102 @Override 103 public QueryResult findAll(int firstNumber, int maxNumber) { 104 // TODO Auto-generated method stub 105 Session session = HibernateUtils.openSession(); 106 Transaction tx= null; 107 try{ 108 tx=session.beginTransaction(); 109 List<User> list = session.createQuery(" FROM User") 110 .setFirstResult(firstNumber) 111 .setMaxResults(maxNumber) 112 .list(); 113 114 Long count = (Long) session.createQuery("SELECT COUNT(*) FROM User").uniqueResult(); 115 tx.commit(); 116 return new QueryResult(count.intValue(),list); 117 }catch(RuntimeException exception){ 118 tx.rollback(); 119 throw exception; 120 }finally{ 121 session.close(); 122 } 123 } 124 125 }
4、做一个测试类
1 package cn.itcast.domain.dao; 2 3 import java.util.List; 4 5 import org.junit.Test; 6 7 import cn.itcast.domain.User; 8 9 public class TestUserDao { 10 11 private UserDao userDao = new UserDaoImpl(); 12 13 @Test 14 public void testSave_1() { 15 User user = new User(); 16 user.setName("张三"); 17 18 // 保存 19 userDao.save(user); 20 } 21 22 @Test 23 public void testGetById() { 24 User user = userDao.getById(1); 25 System.out.println(user); 26 } 27 28 @Test 29 public void testUpdate() { 30 // 从数据库获取一条存在的数据 31 User user = userDao.getById(1); 32 user.setName("李四"); 33 34 // 更新 35 userDao.update(user); 36 } 37 38 @Test 39 public void testDelete() { 40 userDao.delete(1); 41 } 42 43 // ------------ 44 45 @Test 46 public void testSave_25() { 47 for (int i = 1; i <= 25; i++) { 48 User user = new User(); 49 user.setName("test_" + i); 50 userDao.save(user); // 保存 51 } 52 } 53 54 @Test 55 public void testFindAll() { 56 List<User> list = userDao.findAll(); 57 for (User user : list) { 58 System.out.println(user); 59 } 60 } 61 62 @Test 63 public void testFindAllIntInt() { 64 // 查询 65 // QueryResult qr = userDao.findAll(0, 10); // 第1页,每页10条 66 // QueryResult qr = userDao.findAll(10, 10); // 第2页,每页10条 67 QueryResult qr = userDao.findAll(20, 10); // 第3页,每页10条 68 69 // 显示结果 70 System.out.println("总记录数:" + qr.getCount()); 71 for (User user : (List<User>) qr.getList()) { 72 System.out.println(user); 73 } 74 } 75 76 }
当神已无能为力,那便是魔渡众生