码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate环境搭建

时间:2018-06-02 20:03:54      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:builds   导入   closed   mys   lease   dial   zha   nat   tab   

  • 1.去官网下载Jar包:以hibernate-release-5.2.16.Final为例

解压后将hibernate-release-5.2.16.Final\lib\required目录中的所有jar包导入

  • 2.配置文件

将hibernate-release-5.2.16.Final\project\etc中hibernate.cfg.xml文件复制到src下

并且修改配置如下:

 

<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=GMT</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">zhangpn</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/zhangpn/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
  • 3.为测试而创建一个pojo类
技术分享图片
 1 package com.zhangpn.pojo;
 2 
 3 public class User {
 4     private int id;
 5     private String name;
 6     private String password;
 7 
 8     public int getId() {
 9         return id;
10     }
11 
12     public void setId(int id) {
13         this.id = id;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public String getPassword() {
25         return password;
26     }
27 
28     public void setPassword(String password) {
29         this.password = password;
30     }
31 
32 }
User类
  • 4.配置*.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.zhangpn.pojo.User" table="user">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="password"/>
    </class>
</hibernate-mapping>
  • 5.将User.hbm.xml加入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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">zhangpn</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<mapping resource="com/zhangpn/pojo/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
  • 6.测试

 

package com.zhangpn.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.zhangpn.pojo.User;

public class Test {

	public static void main(String[] args) {
		/**
		 * hibernate 4 版本
		 */
//		Configuration cfg = new Configuration().configure();
//		ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
//		SessionFactory sf = cfg.buildSessionFactory(registry);
//		Session session = sf.openSession();
		/**
		 * hibernate 5 版本
		 */
		StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
		Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();
		SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
		
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		User user = new User();
		user.setName("zhangpn");
		user.setPassword("888888");
		session.save(user);
		tx.commit();
		session.close();
	}

}
  • 测试结果

技术分享图片


  • 问题与解决:

如果遇到错误:Error executing DDL via JDBC Statement

建议:修改方言为:<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

Hibernate环境搭建

标签:builds   导入   closed   mys   lease   dial   zha   nat   tab   

原文地址:https://www.cnblogs.com/batj/p/9126348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!