从hibernate5.x.x开始对获取SessionFactory的方式做了修改,按照原来的代码获取不到SessionFactory了,hibernate5.x.x获取SessionFactory的代码如下:
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
??.configure() // configures settings from hibernate.cfg.xml
??.build();
????sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
??? Session session =sessionFactory.openSession();
hibernate最新版本(目前最新版本为hibernate5.1.0)的下载地址为:http://hibernate.org/
一、hibernate环境的搭建:hibernate环境搭建还算比较简单的,其主要的jar包为hibernate-core-5.1.0.Final.jar,当然,我这里强烈建议大家把下载好的hibernate文件中lib目录下required里面的所有jar文件都拷贝到自己项目WEB-INF/lib目录下面,否则在调试的的时候总会遇到各种找不到类的错误问题。
二、新建hibernate.cfg.xml文件,其作用为配置数据库基本连接信息,配置好以后我们就不需要再程序中写连接数据库的代码了,其配置如下:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
?<property name="connection.driver_class">
??com.mysql.jdbc.Driver
?</property>
?<property name="connection.username">你的mysql数据库登陆名</property>
?<property name="connection.password">你的mysql数据库登陆密码</property>
?<property name="connection.url">
??jdbc:mysql:///TestHibernate?createDatabaseIfNotExist=true<!--如果数据库不存在,则自动创建数据库
-->?</property>
?<property name="hbm2ddl.auto">update</property>
?<property name="dialect">
??org.hibernate.dialect.MySQLDialect
?</property>
?<mapping resource="cn/test/Bean/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
三、创建User.java类,代码如下:
package cn.test.Bean;
public class User {
?private long id;
?private String name;
?private String age;
?private String position;
?public String getName() {
??return name;
?}
?public void setName(String name) {
??this.name = name;
?}
?public String getAge() {
??return age;
?}
?public void setAge(String age) {
??this.age = age;
?}
?public String getPosition() {
??return position;
?}
?public void setPosition(String position) {
??this.position = position;
?}
?public long getId() {
??return id;
?}
?public void setId(long id) {
??this.id = id;
?}
}
四、创建User.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>
<class name="cn.test.Bean.User" table="User">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age"></property>
<property name="position"></property>
</class>
</hibernate-mapping>
好了,到这里hibernate的需要基本配置就ok了。
五,创建一个类,保存一条数据至数据库。
package cn.test.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Test;
import cn.test.Bean.User;
public class TestSave {
?private SessionFactory sessionFactory;
?@Test
?public void SaveUser()
?{
??final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
??.configure() // configures settings from hibernate.cfg.xml
??.build();
???? sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
???? Session session =sessionFactory.openSession();
? User user =new User();
? user.setAge("19");
? user.setName("李四");
? user.setPosition("学生");
? session.beginTransaction();//开启事务
? session.save(user);
? session.getTransaction().commit();//提交事务
? session.close();
? System.out.println("保存成功!");
?}
}
大家主要要注意的就是在?final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
??.configure() // configures settings from hibernate.cfg.xml
??.build();中configure()默认读取的hibernate.cfg.xml路径为项目的src目录下的文件,如果hibernate.cfg.xml
没有直接放在项目src目录下,则需在configure() 中进行配置。否则,会找不到hibernate.cfg.xml
,另外,要使得类中的方法可以单独执行需要添加单元测试@Test。
hibernate开发1--使用hibernate5.1.0保存数据到数据库
原文:http://673390302.iteye.com/blog/2294697