hibernate.cfg.xml
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name="hibernate.connection.password">tiger</property>
- <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:TEST</property>
- <property name="hibernate.connection.username">scott</property>
- <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
- <!-- 在后台显示隐藏执行的 HQL 语句 -->
- <property name="hibernate.show_sql">true</property>
- <!-- 添加映射资源 -->
- <mapping resource="bzc/terry/pojo/Student.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
Student.java
- package bzc.terry.pojo;
- /**
- *
- * @author Terry
- *
- */
- public class Student {
- //
- private int id;
- private String name;
- private String password;
- private String e_mail;
- /**
- *
- * @return
- */
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- /**
- *
- * @return
- */
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- /**
- *
- * @return
- */
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- /**
- *
- * @return
- */
- public String getE_mail() {
- return e_mail;
- }
- public void setE_mail(String e_mail) {
- this.e_mail = e_mail;
- }
- }
Student.hbm.xml
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="bzc.terry.pojo.Student" table="student">
- <!-- 设置主键 -->
- <id name="id" column="stu_id" type="integer">
- <generator class="increment" />
- </id>
- <!-- 设置其它字段 -->
- <property name="name" column="stu_name" type="string" />
- <property name="password" column="stu_pwd" type="string" />
- <property name="e_mail" column="stu_mail" type="string" />
- </class>
- </hibernate-mapping>
StudentService.java
- package bzc.terry.service;
- import java.util.Collection;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import bzc.terry.pojo.Student;
- /**
- * 业务逻辑类。
- *
- * 此类用来完成对数据库的增、删、改、查操作。
- *
- * @author Terry
- *
- */
- public class StudentService {
- private static SessionFactory factory = null; //
- static {//用静态初始化块来解析 xml 配置文件
- try {
- //
- Configuration config = new Configuration();
- //
- factory = config.configure().buildSessionFactory();
- } catch(Exception ex) {
- ex.printStackTrace();
- // 抛出静态初始化块异常
- throw new ExceptionInInitializerError();
- }
- }
- /**
- * 向数据库中添加数据
- */
- public void add(Student stu) {
- Session session = null; //
- Transaction trans = null; //
- try {
- // 打开一个会话
- session = factory.openSession();
- // 开始事务
- trans = session.beginTransaction();
- //
- session.save(stu);
- // 提交事务
- trans.commit();
- System.out.println("Transaction has been commited!");
- } catch(RuntimeException ex) {
- // 回滚事务
- trans.rollback();
- // 打印所有异常信息
- ex.printStackTrace();
- // 抛出异常
- throw ex;
- } finally {// 关闭打开的事务
- System.out.println("Session has been closed!");
- session.close();
- }
- }
- /**
- * 查询数据库中的所有数据
- */
- public Collection findAll() {
- Collection students = null; // 用来存放查询到的所有记录
- Session session = null; // 用于取得数据库的连接 == Connection
- Transaction trans = null; //
- String sql = "SELECT student FROM Student student";
- try {
- // 打开一个会话
- session = factory.openSession();
- // 开始事务
- trans = session.beginTransaction();
- // 通过 sql 语句对数据库进行查询操作
- Query query = session.createQuery(sql);
- System.out.println("Excute Query!");
- // 将查询的结果按顺序放到集合中
- students = query.list();
- System.out.println("after Query!");
- // 提交事务
- trans.commit();
- System.out.println("Transaction has been commited!");
- } catch(RuntimeException ex) {
- // 回滚事务
- trans.rollback();
- // 打印所有异常信息
- ex.printStackTrace();
- // 抛出异常
- throw ex;
- } finally {// 关闭打开的事务
- System.out.println("Session has been closed!");
- session.close();
- }
- // 返回查询到的所有数据
- return students;
- }
- }
Test.java
- package test;
- import java.util.Collection;
- import java.util.Iterator;
- import bzc.terry.pojo.Student;
- import bzc.terry.service.StudentService;
- /**
- * 测试类
- *
- * @author Terry
- *
- */
- public class Test {
- // 测试方法
- public static void main(String[] args) {
- // 创建实体类的对象
- Student stu = new Student();
- // 给对象的属性赋值
- stu.setName("小乐");
- stu.setPassword("201314");
- stu.setE_mail("lele@21.cn");
- // 创建业类务对象
- StudentService stuserv = new StudentService();
- // 向数据库中添加一条记录
- // stuserv.add(stu);
- // 查询数据库中的所有记录
- Collection students = stuserv.findAll();
- //
- Iterator iter = students.iterator();
- //
- while(iter.hasNext()) {
- // 对查询到的一条数据进行强制转型
- Student onestu = (Student)iter.next();
- // 依次打印当前学生的所有信息(当前 Student 对象的所有属性)
- System.out.print("~ " + onestu.getId());
- System.out.print(" " + onestu.getName());
- System.out.print(" " + onestu.getPassword());
- System.out.println(" " + onestu.getE_mail());
- }
- }
- }