下面介绍一下编写Hibernate的配置文件,使用Hibernate操作数据库。
开始部署:下载需要的jar包
下载Hibernate
Hibernate 的官方主页是www.hibernate.org
推荐下载hibernate-distribution-3.3.2.GA-dist.zip
Hibernate包目录结构
部署jar包
hibernate3.jar
required 目录下的jar 包
Oracle 数据库驱动jar包
第一步:创建实体类和实体映射文件
public class User { private int id; private String username; private String password; } 省略get和set方法
配置映射文件(*.hbm.xml)
<?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 package="com.msit.hibernate.entity"> <!--User实体,t_user数据库表面名--> <class name="User" table="t_user"> <!--自增id--> <id name="id"> <generator class="increment"/> </id> <property name="username" /> <property name="password" /> </class> </hibernate-mapping>
第二步:向hibernate.cfg.xml文件中配置映射文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="foo"> <!-- 数据库方言 --> <property name="dialect"> org.hibernate.dialect.OracleDialect </property> <!-- 连接数据库Url --> <property name="hibernate.connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <!-- 连接驱动 --> <property name="hibernate.connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <!-- 用户名 --> <property name="hibernate.connection.username">epet</property> <!-- 密码 --> <property name="hibernate.connection.password">123456</property> <!-- 自动创建数据库表格 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置映射信息 --> <mapping resource="com/msit/hibernate/entity/User.hbm.xml" /> </session-factory> </hibernate-configuration> 注: <session-factory> <!--省略其他配置--> <!--注意配置文件名必须包含其相对于classpath 的全路径--> <mapping resource="cn/jbit/houserent/entity/User.hbm.xml" /> </session-factory>
第三步:抽出HibernateUtil接口
package com.msit.hibernate.HibernateUtil; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private HibernateUtil(){ }; public static SessionFactory SessionFactory = null; static{ //hibernate初始化 Configuration cf = new Configuration(); cf.configure(); SessionFactory = cf.buildSessionFactory();//DriverManager.getconnection() //Session session = SessionFactory.openSession();//相当于得到Connection对象 } public static Session getSession(){ return SessionFactory.openSession(); } public static void closeSession(Session session){ if(session!=null){ session.clear(); } } }
第四步:编写测试类:
package com.msit.hibernate.test; import java.sql.DriverManager; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.msit.hibernate.HibernateUtil.HibernateUtil; import com.msit.hibernate.entity.User; public class HibernateTest { /** * @param args */ public static void main(String[] args) { //创建用户 User user = new User(); user.setId(1); user.setUsername("master123456"); user.setPassword("123"); Session session = HibernateUtil.getSession(); //进行事务处理 Transaction Transaction = session.beginTransaction(); try { //对数据做保存至数据库 session.update(user); //提交事务 Transaction.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); //如果出现异常则进行事务回滚 Transaction.rollback(); } } }
时间: 2024-10-27 13:56:47