hibernate 小程序

来源:互联网 发布:海珠 租房 知乎 编辑:程序博客网 时间:2024/05/29 18:27

hibernate.cfg.xml

  1. <?xml version="1.0" encoding="GBK"?>
  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.         <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  8.         <property name="hibernate.connection.password">tiger</property>
  9.         <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:TEST</property>
  10.         <property name="hibernate.connection.username">scott</property>
  11.         <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
  12.         <!-- 在后台显示隐藏执行的 HQL 语句 -->
  13.         <property name="hibernate.show_sql">true</property>
  14.         
  15.         <!-- 添加映射资源 -->
  16.         <mapping resource="bzc/terry/pojo/Student.hbm.xml"/>
  17.     </session-factory>
  18. </hibernate-configuration>

 

 

Student.java

 

  1. package bzc.terry.pojo;
  2. /**
  3.  * 
  4.  * @author Terry
  5.  *
  6.  */
  7. public class Student {
  8.     //
  9.     private int id;
  10.     private String name;
  11.     private String password;
  12.     private String e_mail;
  13.     
  14.     /**
  15.      * 
  16.      * @return
  17.      */
  18.     public int getId() {
  19.         return id;
  20.     }
  21.     public void setId(int id) {
  22.         this.id = id;
  23.     }
  24.     /**
  25.      * 
  26.      * @return
  27.      */
  28.     public String getName() {
  29.         return name;
  30.     }
  31.     public void setName(String name) {
  32.         this.name = name;
  33.     }
  34.     
  35.     /**
  36.      * 
  37.      * @return
  38.      */
  39.     public String getPassword() {
  40.         return password;
  41.     }
  42.     public void setPassword(String password) {
  43.         this.password = password;
  44.     }
  45.     
  46.     /**
  47.      * 
  48.      * @return
  49.      */
  50.     public String getE_mail() {
  51.         return e_mail;
  52.     }
  53.     public void setE_mail(String e_mail) {
  54.         this.e_mail = e_mail;
  55.     }
  56. }

 

Student.hbm.xml

 

 

  1. <?xml version="1.0" encoding="GBK"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6.   <class name="bzc.terry.pojo.Student" table="student">
  7.     <!-- 设置主键 -->
  8.     <id name="id" column="stu_id" type="integer">
  9.         <generator class="increment" />
  10.     </id>
  11.     <!-- 设置其它字段 -->
  12.     <property name="name" column="stu_name" type="string" />
  13.     <property name="password" column="stu_pwd" type="string" />
  14.     <property name="e_mail" column="stu_mail" type="string" />
  15.   </class>
  16. </hibernate-mapping>

 

 

StudentService.java

 

  1. package bzc.terry.service;
  2. import java.util.Collection;
  3. import org.hibernate.Query;
  4. import org.hibernate.Session;
  5. import org.hibernate.SessionFactory;
  6. import org.hibernate.Transaction;
  7. import org.hibernate.cfg.Configuration;
  8. import bzc.terry.pojo.Student;
  9. /**
  10.  * 业务逻辑类。
  11.  * 
  12.  * 此类用来完成对数据库的增、删、改、查操作。
  13.  * 
  14.  * @author Terry
  15.  *
  16.  */
  17. public class StudentService {
  18.     
  19.     
  20.     private static SessionFactory factory = null;   //
  21.     
  22.     static {//用静态初始化块来解析 xml 配置文件
  23.         try {
  24.             //
  25.             Configuration config = new Configuration();
  26.             //
  27.             factory = config.configure().buildSessionFactory();
  28.         } catch(Exception ex) {
  29.             ex.printStackTrace();
  30.             // 抛出静态初始化块异常
  31.             throw new ExceptionInInitializerError();
  32.         }
  33.     }
  34.     
  35.     /**
  36.      * 向数据库中添加数据
  37.      */
  38.     public void add(Student stu) {
  39.         Session session = null;    //
  40.         Transaction trans = null;  //
  41.         
  42.         try {
  43.             // 打开一个会话
  44.             session = factory.openSession();
  45.             // 开始事务
  46.             trans = session.beginTransaction();
  47.             //
  48.             session.save(stu);
  49.             // 提交事务
  50.             trans.commit();
  51.             System.out.println("Transaction has been commited!");   
  52.         } catch(RuntimeException ex) {
  53.             // 回滚事务
  54.             trans.rollback();
  55.             // 打印所有异常信息
  56.             ex.printStackTrace();
  57.             // 抛出异常
  58.             throw ex; 
  59.         } finally {// 关闭打开的事务
  60.             System.out.println("Session has been closed!");
  61.             session.close();
  62.         }
  63.     }
  64.     
  65.     /**
  66.      * 查询数据库中的所有数据 
  67.      */
  68.     public Collection findAll() {
  69.         Collection students = null// 用来存放查询到的所有记录
  70.         Session session = null;     // 用于取得数据库的连接 == Connection
  71.         Transaction trans = null;   // 
  72.         String sql = "SELECT student FROM Student student";
  73.         
  74.         try {
  75.             // 打开一个会话
  76.             session = factory.openSession();
  77.             // 开始事务
  78.             trans = session.beginTransaction();
  79.             // 通过 sql 语句对数据库进行查询操作
  80.             Query query = session.createQuery(sql);
  81. System.out.println("Excute Query!");
  82.             // 将查询的结果按顺序放到集合中
  83.             students = query.list();
  84. System.out.println("after Query!");
  85.             // 提交事务
  86.             trans.commit();
  87.             System.out.println("Transaction has been commited!");
  88.         } catch(RuntimeException ex) {
  89.             // 回滚事务
  90.             trans.rollback();
  91.             // 打印所有异常信息
  92.             ex.printStackTrace();
  93.             // 抛出异常
  94.             throw ex; 
  95.         } finally {// 关闭打开的事务
  96.             System.out.println("Session has been closed!");
  97.             session.close();
  98.         }
  99.         // 返回查询到的所有数据
  100.         return students;
  101.     }
  102. }

 

Test.java

 

  1. package test;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import bzc.terry.pojo.Student;
  5. import bzc.terry.service.StudentService;
  6. /**
  7.  * 测试类
  8.  * 
  9.  * @author Terry
  10.  *
  11.  */
  12. public class Test {
  13.     
  14.     // 测试方法
  15.     public static void main(String[] args) {
  16.         // 创建实体类的对象
  17.         Student stu = new Student();
  18.         // 给对象的属性赋值
  19.         stu.setName("小乐");
  20.         stu.setPassword("201314");
  21.         stu.setE_mail("lele@21.cn");
  22.         
  23.         // 创建业类务对象
  24.         StudentService stuserv = new StudentService();
  25.         // 向数据库中添加一条记录
  26. //      stuserv.add(stu);
  27.         
  28.         // 查询数据库中的所有记录
  29.         Collection students = stuserv.findAll();
  30.         //
  31.         Iterator iter = students.iterator();
  32.         //
  33.         while(iter.hasNext()) {
  34.             // 对查询到的一条数据进行强制转型
  35.             Student onestu = (Student)iter.next();
  36.             // 依次打印当前学生的所有信息(当前 Student 对象的所有属性)
  37.             System.out.print("~ " + onestu.getId());
  38.             System.out.print("   " + onestu.getName());
  39.             System.out.print("   " + onestu.getPassword());
  40.             System.out.println("    " + onestu.getE_mail());
  41.         }
  42.     }
  43. }