SSH框架搭建过程---之Hibernate框架的使用(1)
2014-07-22 17:11
501 查看
Hibernate框架的使用
0.导包1. hibernate.cfg.xml
<span style="font-size:12px;color:#339999;"><strong><?xml version="1.0"encoding="UTF-8"?> <!DOCTYPE hibernate-configurationPUBLIC "-//Hibernate/HibernateConfiguration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Databaseconnection settings --> <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/graduate</property> <propertyname="connection.username">root</property> <propertyname="connection.password">root</property> <!-- SQL dialect--> <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Echo allexecuted SQL to stdout --> <propertyname="show_sql">true</property> <propertyname="format_sql">true</property> <mappingresource="cn/edu/bucea/domain/Demo.hbm.xml" /> </session-factory> </hibernate-configuration></strong></span>
2. Demo.java
<span style="font-size:12px;">/** * */ package cn.edu.bucea.domain; import java.io.Serializable; /** * @author JJG * */ public class Demo implementsSerializable { private static final longserialVersionUID = -2072913502409092000L; private String id; private String name; private String password; public Demo() {} /** * @param id * @param name * @param password */ public Demo(String id, Stringname, String password) { super(); this.id = id; this.name = name; this.password =password; } public String getId() { return id; } public void setId(String id){ this.id = id; } public String getName() { return name; } public void setName(Stringname) { this.name = name; } public String getPassword() { return password; } public voidsetPassword(String password) { this.password =password; } @Override public String toString() { return "Demo[id=" + id + ", name=" + name + ", password=" +password +"]"; } }</span>
Demo.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.edu.bucea.domain.Demo"table="DEMO"> <id name="id" type="int"> <column name="ID"/> <generator class="identity"/> </id> <property name="name" type="string"> <column name="NAME"/> </property> <property name="password" type="string"> <column name="PASSWORD"></column> </property> </class> </hibernate-mapping>
把新的映射加入到Hibernate的配置中;
例:<mapping resource=" cn/edu/bucea/domain/Demo.hbm.xml"/>
3.SessionfactoryUtil.java
importorg.hibernate.HibernateException; importorg.hibernate.SessionFactory; importorg.hibernate.cfg.Configuration; public classSessionFactoryUtil { private static SessionFactory sessionFactory; private static String CONFIG_FILE_LOCATION = "/cn/edu/bucea/config/hibernate.cfg.xml"; static{ try{ sessionFactory = new Configuration().configure(CONFIG_FILE_LOCATION). buildSessionFactory(); }catch(HibernateExceptione){ System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } public static SessionFactorygetSessionFactory() { return sessionFactory; } public static void main(String[]args) { System.out.println(getSessionFactory()); } }
DemoException.java
/** * @author JJG * 用户自定义异常类 */ public class DemoException extends Exception { private static final long serialVersionUID = 704805066683813880L; publicDemoException(Exception e){ super(e); } publicDemoException(String msg){ super(msg); } }
4. DemoDAO.java
importorg.hibernate.HibernateException; importorg.hibernate.Session; importorg.hibernate.Transaction; import cn.edu.bucea.domain.Demo; importcn.edu.bucea.exception.DemoException; importcn.edu.bucea.util.SessionFactoryUtil; public class DemoDAO { public DemoDAO() { } public void saveDemo(Demodemo) throws DemoException{ Session session= null; Transactiontransaction = null;//注意Session和Transaction的包 try{ session =SessionFactoryUtil.getSessionFactory().openSession(); transaction= session.beginTransaction(); session.save(demo); transaction.commit(); } catch(HibernateExceptione){ transaction.rollback(); throw newDemoException(e.getMessage()); } finally{ if (session != null){ session.close(); } } //... } }
5.DeomTest.java
import cn.edu.bucea.DAO.DemoDAO; import cn.edu.bucea.domain.Demo; import cn.edu.bucea.exception.DemoException; /** * @author JJG * DemoDAO测试类 */ public class DemoTest { public static void testSaveDemo() throws DemoException{ Demo demo = new Demo(); demo.setId(1); demo.setName("JJG"); demo.setPassword("123"); DemoDAO demoDAO = new DemoDAO(); demoDAO.saveDemo(demo); } //… public static void main(String[] args) throws DemoException { testSaveDemo(); //… } }
6…
注:以上Hibernate框架的单独应用
===========================================================================
下一篇:SSH框架搭建过程---之Spring框架的使用
相关文章推荐
- SSH框架搭建过程---之Struts框架的使用(3)
- 不使用Maven的情况下,搭建Spring+Struts+Hibernate框架 - 项目搭建
- SSM框架——Spring+SpringMVC+Mybatis的搭建教程 一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛。 Spring作为一个轻量级
- hibernate框架初次搭建并使用
- 五分钟阅读阿里巴巴架构师如何使用微服务框架搭建电商平台全过程
- SSH框架搭建过程---之Spring-Hibernate整合(5)
- 使用Myeclipse10.0自动生成搭建SSH框架(数据库表自动反向转换成Hibernate实体)实现用户登陆
- IDEA安装与破解/IDEA汉化版安装/IDEA + Maven + SpringMVC + Hibernate集成环境搭建/框架配置以及IDEA快捷使用
- 使用spring、springmvc和hibernate开发博客(一)——搭建框架
- hibernate框架基本搭建及使用
- [教程]使用MyEclipse实现集成Struts+hibernate框架的项目开发过程
- 在Eclipse下手动搭建SSH框架,并使用Hibernate插件反向生成实体类和关系映射文件。
- 搭建ssm框架的详细过程 一 (使用mybatis的接口)
- 使用Myeclipse10.0自动生成搭建SSH框架(数据库表自动反向转换成Hibernate实体)实现用户登陆
- Hibernate 框架的详细搭建与简单使用(一)
- hibernate框架环境搭建与使用
- 使用RESTful Jersey框架搭建WebService,Hibernate框架访问数据库,MySQL存储数据
- 五分钟阅读阿里巴巴架构师如何使用微服务框架搭建电商平台全过程
- 搭建ssm框架的详细过程 二 (使用mybatis的接口实现类)
- Hibernate学习笔记一 使用idea开发工具搭建框架