Hibernate逍遥游记-第2章-使用hibernate.properties

时间:2021-03-13 14:03:33

1.

 package mypack;

 import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService{
public static SessionFactory sessionFactory; /** 初始化Hibernate,创建SessionFactory实例 */
static{
try{
// 根据默认位置的Hibernate配置文件的配置信息,创建一个Configuration实例
Configuration config = new Configuration();
//加载Monkey类的对象-关系映射文件
config.addClass(Monkey.class);
// 创建SessionFactory实例
sessionFactory = config.buildSessionFactory();
SessionFactory sessionFactory2 = config.buildSessionFactory();
System.out.println("---------->"+(sessionFactory2==sessionFactory)); }catch(RuntimeException e){e.printStackTrace();throw e;}
} /** 查询所有的Monkey对象,然后打印Monkey对象信息 */
public void findAllMonkeys(){
Session session = sessionFactory.openSession(); //创建一个会话
Transaction tx = null;
try {
tx = session.beginTransaction(); //开始一个事务
Query query=session.createQuery("from Monkey as m order by m.name asc");
List monkeys=query.list();
for (Iterator it = monkeys.iterator(); it.hasNext();) {
Monkey monkey=(Monkey) it.next();
System.out.println("ID="+monkey.getId()
+",姓名="+monkey.getName()
+",年龄="+monkey.getAge()
+",性别="+(monkey.getGender()=='M'?"公猴":"母猴"));
} tx.commit(); //提交事务 }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} /** 持久化一个Monkey对象 */
public void saveMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} /** 按照OID加载一个Monkey对象,然后修改它的属性 */
public void loadAndUpdateMonkey(Long monkey_id,int age){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction(); Monkey m=(Monkey)session.get(Monkey.class,monkey_id);
m.setAge(age);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} /**删除Monkey对象 */
public void deleteMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void test(){
Monkey monkey=new Monkey();
monkey.setName("智多星");
monkey.setAge(1);
monkey.setGender('M'); saveMonkey(monkey); findAllMonkeys();
loadAndUpdateMonkey(monkey.getId(),2);
findAllMonkeys();
deleteMonkey(monkey);
findAllMonkeys();
} public static void main(String args[]){
new BusinessService().test();
sessionFactory.close();
}
}

2.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="mypack.Monkey" table="MONKEYS"> <id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="string" not-null="true" />
<property name="age" column="AGE" type="int" />
<property name="gender" column="GENDER" type="character"/>
</class> </hibernate-mapping>

3.

 hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB?useUnicode=true&characterEncoding=GBK
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.show_sql=true

4.