- 版本:hibernate-release-5.0.1.Final
- 首先,导入Jar包
- 其次,配置hibernate.cfg.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4 <hibernate-configuration> 5 <session-factory> 6 <property name="hibernate.connection.password">zhangpn</property> 7 <property name="hibernate.connection.username">root</property> 8 <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> 9 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT&useSSL=false</property> 10 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 11 <property name="hibernate.show_sql">true</property> 12 <property name="hibernate.format_sql">true</property> 13 <property name="hibernate.hbm2ddl.auto">update</property> 14 <mapping resource="easyStart/Entity/User.hbm.xml" /> 15 </session-factory> 16 </hibernate-configuration>
- 然后,创建POJO类
1 package easyStart.Entity; 2 3 public class User { 4 private int id; 5 private String name; 6 private String address; 7 8 public User(String name, String address) { 9 super(); 10 this.name = name; 11 this.address = address; 12 } 13 14 public User() { 15 super(); 16 } 17 18 public int getId() { 19 return id; 20 } 21 22 public void setId(int id) { 23 this.id = id; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public String getAddress() { 35 return address; 36 } 37 38 public void setAddress(String address) { 39 this.address = address; 40 } 41 42 @Override 43 public String toString() { 44 return "User [id=" + id + ", name=" + name + ", address=" + address + "]"; 45 } 46 47 }
- 然后,配置映射文件[POJO.hbm.xml]
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2018-6-9 22:05:31 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6 <class name="easyStart.Entity.User" table="USER"> 7 <id name="id" type="int"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="name" type="java.lang.String"> 12 <column name="NAME" /> 13 </property> 14 <property name="address" type="java.lang.String"> 15 <column name="ADDRESS" /> 16 </property> 17 </class> 18 </hibernate-mapping>
- 最后,写代码测试。
1 package easyStart.Run; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.boot.MetadataSources; 6 import org.hibernate.boot.registry.StandardServiceRegistry; 7 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 8 9 import easyStart.Entity.User; 10 11 public class QuickStart { 12 13 public static void main(String[] args) { 14 15 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build(); 16 SessionFactory sessionFactory = null; 17 try { 18 sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); 19 } catch (Exception e) { 20 StandardServiceRegistryBuilder.destroy(registry); 21 } 22 23 Session session = sessionFactory.openSession(); 24 session.beginTransaction(); 25 // 处理事务 Begin 26 User user = new User("zhangpn", "JangSu-XuZhou"); 27 session.save(user); 28 // 处理事务 End 29 session.getTransaction().commit(); 30 session.close(); 31 sessionFactory.close(); 32 } 33 34 }
执行结果:
1 六月 11, 2018 3:07:33 下午 org.hibernate.Version logVersion 2 INFO: HHH000412: Hibernate Core {5.0.1.Final} 3 六月 11, 2018 3:07:33 下午 org.hibernate.cfg.Environment <clinit> 4 INFO: HHH000206: hibernate.properties not found 5 六月 11, 2018 3:07:33 下午 org.hibernate.cfg.Environment buildBytecodeProvider 6 INFO: HHH000021: Bytecode provider name : javassist 7 六月 11, 2018 3:07:33 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 8 INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final} 9 六月 11, 2018 3:07:33 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity 10 WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time. 11 六月 11, 2018 3:07:34 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 12 WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) 13 六月 11, 2018 3:07:34 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 14 INFO: HHH000401: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT&useSSL=false] 15 六月 11, 2018 3:07:34 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 16 INFO: HHH000046: Connection properties: {user=root, password=****} 17 六月 11, 2018 3:07:34 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 18 INFO: HHH000006: Autocommit mode: false 19 六月 11, 2018 3:07:34 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 20 INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 21 六月 11, 2018 3:07:34 下午 org.hibernate.dialect.Dialect <init> 22 INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 23 六月 11, 2018 3:07:34 下午 org.hibernate.boot.model.source.internal.hbm.ModelBinder bindProperty 24 WARN: HHH90000003: Use of DOM4J entity-mode is considered deprecated 25 六月 11, 2018 3:07:34 下午 org.hibernate.boot.model.source.internal.hbm.ModelBinder bindProperty 26 WARN: HHH90000003: Use of DOM4J entity-mode is considered deprecated 27 六月 11, 2018 3:07:34 下午 org.hibernate.boot.model.source.internal.hbm.ModelBinder bindProperty 28 WARN: HHH90000003: Use of DOM4J entity-mode is considered deprecated 29 六月 11, 2018 3:07:34 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute 30 INFO: HHH000228: Running hbm2ddl schema update 31 Hibernate: 32 insert 33 into 34 USER 35 (NAME, ADDRESS) 36 values 37 (?, ?) 38 六月 11, 2018 3:07:35 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop 39 INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT&useSSL=false]
自动创建实体表,而且成功被插入一条数据;
尝试进行查询操作:
1 package easyStart.Run; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.boot.MetadataSources; 6 import org.hibernate.boot.registry.StandardServiceRegistry; 7 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 8 9 import easyStart.Entity.User; 10 11 public class QuickStart { 12 13 public static void main(String[] args) { 14 15 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build(); 16 SessionFactory sessionFactory = null; 17 try { 18 sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); 19 } catch (Exception e) { 20 StandardServiceRegistryBuilder.destroy(registry); 21 } 22 23 Session session = sessionFactory.openSession(); 24 session.beginTransaction(); 25 // 处理事务 Begin 26 // User user = new User("zhangpn", "JangSu-XuZhou"); 27 // session.save(user); 28 29 System.out.println(session.get(User.class, 1)); 30 31 // 处理事务 End 32 session.getTransaction().commit(); 33 session.close(); 34 sessionFactory.close(); 35 } 36 37 }
1 六月 11, 2018 3:11:11 下午 org.hibernate.Version logVersion 2 INFO: HHH000412: Hibernate Core {5.0.1.Final} 3 六月 11, 2018 3:11:11 下午 org.hibernate.cfg.Environment <clinit> 4 INFO: HHH000206: hibernate.properties not found 5 六月 11, 2018 3:11:11 下午 org.hibernate.cfg.Environment buildBytecodeProvider 6 INFO: HHH000021: Bytecode provider name : javassist 7 六月 11, 2018 3:11:11 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 8 INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final} 9 六月 11, 2018 3:11:11 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity 10 WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time. 11 六月 11, 2018 3:11:12 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 12 WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) 13 六月 11, 2018 3:11:12 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 14 INFO: HHH000401: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT&useSSL=false] 15 六月 11, 2018 3:11:12 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 16 INFO: HHH000046: Connection properties: {user=root, password=****} 17 六月 11, 2018 3:11:12 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 18 INFO: HHH000006: Autocommit mode: false 19 六月 11, 2018 3:11:12 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 20 INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 21 六月 11, 2018 3:11:12 下午 org.hibernate.dialect.Dialect <init> 22 INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 23 六月 11, 2018 3:11:12 下午 org.hibernate.boot.model.source.internal.hbm.ModelBinder bindProperty 24 WARN: HHH90000003: Use of DOM4J entity-mode is considered deprecated 25 六月 11, 2018 3:11:12 下午 org.hibernate.boot.model.source.internal.hbm.ModelBinder bindProperty 26 WARN: HHH90000003: Use of DOM4J entity-mode is considered deprecated 27 六月 11, 2018 3:11:12 下午 org.hibernate.boot.model.source.internal.hbm.ModelBinder bindProperty 28 WARN: HHH90000003: Use of DOM4J entity-mode is considered deprecated 29 六月 11, 2018 3:11:13 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute 30 INFO: HHH000228: Running hbm2ddl schema update 31 六月 11, 2018 3:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop 32 INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT&useSSL=false] 33 Hibernate: 34 select 35 user0_.ID as ID1_0_0_, 36 user0_.NAME as NAME2_0_0_, 37 user0_.ADDRESS as ADDRESS3_0_0_ 38 from 39 USER user0_ 40 where 41 user0_.ID=? 42 User [id=1, name=zhangpn, address=JangSu-XuZhou]
查询成功!