Hibernate 开发流程
Hibernate 开发流程
一、导入相应的包
1、hibernate安装文件夹中的lib->required中的包
2、导入log4j
3、导入数据库驱动
二、创建hibernate的配置文件
在src的目录下创建相应的hibernate.cfg.xml在这个文件中加入相应的数据库基本信息的配置
在hibernate.cfg.xml的配置文件中首先需要配置相应的数据库基本连接
<hibernate-configuration> <session-factory> <!-- hibernate的方言,用来确定连接的数据库 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库的连接类 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库的连接字符串和用户名密码 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itat_hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 在使用hibernate时会显示相应的SQL --> <property name="show_sql">true</property> <!-- 会自动完成类到数据表的转换 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 加入实体类的映射文件 --> </session-factory> </hibernate-configuration>
三、创建实体类
package org.test.model;
import java.util.Date; public class User { private int id; private String username; private String password; private String nickname; private Date born; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public Date getBorn() { return born; } public void setBorn(Date born) { this.born = born; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", nickname=" + nickname + ", born=" + born + "]"; } }
四、在实体类的包中创建相应的hbm文件,用来指定实体类和数据库映射关系
<?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="org.test.model"> <class name="User" table="t_user"> <id name="id"> <generator class="native"/> </id> <property name="username"/> <property name="password"/> <property name="nickname"/> <property name="born" type="timestamp"/> </class> </hibernate-mapping>
五、将配置文件添加到hibernate的cfg的配置文件中
<hibernate-configuration> <session-factory> <!-- hibernate的方言,用来确定连接的数据库 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库的连接类 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库的连接字符串和用户名密码 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itat_hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 在使用hibernate时会显示相应的SQL --> <property name="show_sql">true</property> <!-- 会自动完成类到数据表的转换 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 加入实体类的映射文件 --> <mapping resource="org/test/model/User.hbm.xml"/> </session-factory> </hibernate-configuration>
六、创建SessionFactory,SessionFactory是线程安全,所以整个SessionFactory应该基于单例的模式来创建
Configuration cfg = new Configuration().configure();
//cfg.buildSessionFactory();//在hibernate3中都是使用该种方法创建,但是在4中被禁用了
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);
七、创建session
Session session = factory.openSession();
八、通过session来进行各种操作
以下代码完成了对象的添加操作
try {
session = factory.openSession();
//开启事务
session.beginTransaction();
User u = new User();
u.setNickname("张三");
u.setPassword("123");
u.setUsername("zhangsan");
u.setBorn(new Date());
session.save(u);
//提交事务
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
if(session!=null) session.getTransaction().rollback();
} finally {
if(session!=null) session.close();
}
完整Test类
package org.test.test; import java.util.Date; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.Test; import org.test.model.User; public class TestFirst { @Test public void test01() { Configuration cfg = new Configuration().configure(); //cfg.buildSessionFactory();//在hibernate3中都是使用该种方法创建,但是在4中被禁用了 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory factory = cfg.buildSessionFactory(serviceRegistry); Session session = null; try { session = factory.openSession(); //开启事务 session.beginTransaction(); User u = new User(); u.setNickname("张三"); u.setPassword("123"); u.setUsername("zhangsan"); u.setBorn(new Date()); session.save(u); //提交事务 session.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); if(session!=null) session.getTransaction().rollback(); } finally { if(session!=null) session.close(); } } }
note:感谢董浩老师,讲解很清楚
posted on 2017-07-13 15:41 dawangandy 阅读(157) 评论(0) 收藏 举报
浙公网安备 33010602011771号