1.
1 package mypack; 2 3 import org.hibernate.*; 4 import org.hibernate.cfg.Configuration; 5 import java.util.*; 6 7 public class BusinessService{ 8 public static SessionFactory sessionFactory; 9 10 /** 初始化Hibernate,创建SessionFactory实例 */ 11 static{ 12 try{ 13 // 根据默认位置的Hibernate配置文件的配置信息,创建一个Configuration实例 14 Configuration config = new Configuration(); 15 //加载Monkey类的对象-关系映射文件 16 config.addClass(Monkey.class); 17 // 创建SessionFactory实例 18 sessionFactory = config.buildSessionFactory(); 19 SessionFactory sessionFactory2 = config.buildSessionFactory(); 20 System.out.println("---------->"+(sessionFactory2==sessionFactory)); 21 22 }catch(RuntimeException e){e.printStackTrace();throw e;} 23 } 24 25 /** 查询所有的Monkey对象,然后打印Monkey对象信息 */ 26 public void findAllMonkeys(){ 27 Session session = sessionFactory.openSession(); //创建一个会话 28 Transaction tx = null; 29 try { 30 tx = session.beginTransaction(); //开始一个事务 31 Query query=session.createQuery("from Monkey as m order by m.name asc"); 32 List monkeys=query.list(); 33 for (Iterator it = monkeys.iterator(); it.hasNext();) { 34 Monkey monkey=(Monkey) it.next(); 35 System.out.println("ID="+monkey.getId() 36 +",姓名="+monkey.getName() 37 +",年龄="+monkey.getAge() 38 +",性别="+(monkey.getGender()=='M'?"公猴":"母猴")); 39 } 40 41 tx.commit(); //提交事务 42 43 }catch (RuntimeException e) { 44 if (tx != null) { 45 tx.rollback(); 46 } 47 throw e; 48 } finally { 49 session.close(); 50 } 51 } 52 53 /** 持久化一个Monkey对象 */ 54 public void saveMonkey(Monkey monkey){ 55 Session session = sessionFactory.openSession(); 56 Transaction tx = null; 57 try { 58 tx = session.beginTransaction(); 59 session.save(monkey); 60 tx.commit(); 61 62 }catch (RuntimeException e) { 63 if (tx != null) { 64 tx.rollback(); 65 } 66 throw e; 67 } finally { 68 session.close(); 69 } 70 } 71 72 /** 按照OID加载一个Monkey对象,然后修改它的属性 */ 73 public void loadAndUpdateMonkey(Long monkey_id,int age){ 74 Session session = sessionFactory.openSession(); 75 Transaction tx = null; 76 try { 77 tx = session.beginTransaction(); 78 79 Monkey m=(Monkey)session.get(Monkey.class,monkey_id); 80 m.setAge(age); 81 tx.commit(); 82 83 }catch (RuntimeException e) { 84 if (tx != null) { 85 tx.rollback(); 86 } 87 throw e; 88 } finally { 89 session.close(); 90 } 91 } 92 93 /**删除Monkey对象 */ 94 public void deleteMonkey(Monkey monkey){ 95 Session session = sessionFactory.openSession(); 96 Transaction tx = null; 97 try { 98 tx = session.beginTransaction(); 99 session.delete(monkey); 100 tx.commit(); 101 102 }catch (RuntimeException e) { 103 if (tx != null) { 104 tx.rollback(); 105 } 106 throw e; 107 } finally { 108 session.close(); 109 } 110 } 111 112 public void test(){ 113 Monkey monkey=new Monkey(); 114 monkey.setName("智多星"); 115 monkey.setAge(1); 116 monkey.setGender('M'); 117 118 saveMonkey(monkey); 119 120 findAllMonkeys(); 121 loadAndUpdateMonkey(monkey.getId(),2); 122 findAllMonkeys(); 123 deleteMonkey(monkey); 124 findAllMonkeys(); 125 } 126 127 public static void main(String args[]){ 128 new BusinessService().test(); 129 sessionFactory.close(); 130 } 131 }
2.
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 <class name="mypack.Monkey" table="MONKEYS"> 8 9 10 <id name="id" column="ID" type="long"> 11 <generator class="increment"/> 12 </id> 13 <property name="name" column="NAME" type="string" not-null="true" /> 14 <property name="age" column="AGE" type="int" /> 15 <property name="gender" column="GENDER" type="character"/> 16 </class> 17 18 </hibernate-mapping>
3.
1 hibernate.dialect=org.hibernate.dialect.MySQLDialect 2 hibernate.connection.driver_class=com.mysql.jdbc.Driver 3 hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB?useUnicode=true&characterEncoding=GBK 4 hibernate.connection.username=root 5 hibernate.connection.password=1234 6 hibernate.show_sql=true
4.