Hibernate環境搭建
阿新 • • 發佈:2018-06-02
5.2.1 mapping 方言 下載 service sta close 1.0 dtd
- 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 16User類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環境搭建