Hibernate环境搭建
Posted 呦,你是个老母猪呦!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate环境搭建相关的知识,希望对你有一定的参考价值。
- 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 }
- 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环境搭建的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段