hibernate 小程序
来源:互联网 发布:海珠 租房 知乎 编辑:程序博客网 时间:2024/05/29 18:27
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());
- }
- }
- }
- hibernate 小程序
- Hibernate小程序关键代码
- Eclipse Hibernate 入门小程序
- Hibernate简单小程序--浅入hibernate
- Hibernate环境搭建第一个小程序
- struts+Hibernate小程序的坑我
- Hibernate学习笔记_01_第一个Hibernate小程序
- eclipse 下配置hibernate以及第一个hibernate小程序
- eclipse 下配置hibernate以及第一个hibernate小程序
- MyEclipse10下开发第一个Hibernate小程序
- MyEclipse10下开发第一个Hibernate小程序
- Hibernate入门到开发(一)之入门小程序
- 小程序
- 小程序
- 小程序
- 小程序
- 小程序
- 小程序
- subversion源代码安装
- HP大中华区总裁孙振耀退休十五天后九大感言
- 引擎技术研究之水的渲染
- 像大师们一样思考——从“UML何时死掉”谈起
- typdef的理解
- hibernate 小程序
- Hwnd和Handle的区别
- 穷人的感悟
- Charpter 4&5 ARP&RARP
- 17 TCP
- 合并字符串数组的实现
- Groovy Tip 28 Gpath
- 哪位有华谊MS8261和8264万用表电路图
- SQL Server 2005 的新增功能