hibernate使用类直接建表的固定代码(以及封装的方法和xml的配置)

---恢复内容开始---

                                                               

2016-04-27

1.封装的方法:

1 import org.hibernate.Session;
 2 import org.hibernate.SessionFactory;
 3 import org.hibernate.cfg.Configuration;
 4 import org.hibernate.service.ServiceRegistry;
 5 import org.hibernate.service.ServiceRegistryBuilder;
 6 public class MySession {
 7     private MySession() {
 8     }
 9     private static Configuration cfg = null;
10     private static SessionFactory sf = null;
11     static {
12         cfg = new Configuration().configure("/com/gao/cc/util/hibernate.cfg.xml");
13         ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(
14                 cfg.getProperties()).buildServiceRegistry();
15         sf = cfg.buildSessionFactory(registry);
16     }
17     public static Configuration getConfiguration() {
18         return cfg;
19     }
20     public static Session getSession() {
21         return sf.openSession();
22     }
23     public static void close(Session session) {
24         if (session != null) {
25             session.close();
26         }
27         if (sf != null) {
28             sf.close();
29         }
30     }
31 }

2:相关xml的配置:

<?xml version='1.0' encoding='utf-8'?>
<!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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
      <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databasename=mydb</property>
      <property name="hibernate.connection.username">sa</property>
      <property name="hibernate.connection.password">1234</property>
      <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.format_sql">true</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- <mapping class="com.sunzone.model.EntityVo" /> <mapping class="com.sunzone.model.Husband"  //双引号添加类路径
         /> <mapping class="com.sunzone.model.Wife" /> -->
   </session-factory>
</hibernate-configuration>

  3:生成代码的固定方法代码

public class Gao05 {
	public static void main(String[] args) {
		Configuration cfg = MySession.getConfiguration();
		SchemaExport export = new SchemaExport(cfg);
		export.create(true, true);
	}
}

  

 4:测试方法(略有固定,可参考)

public class Gao06 {
	public static void main(String[] args) {
		Configuration cfg = MySession.getConfiguration();
		// 1、加注解类Class
		cfg.addAnnotatedClass(Person.class);
		// .....
		Session session = MySession.getSession();
		// 2、创建类实例;
		Person p1 = new Person("001", "张老三", 53);
		Person p2 = new Person();
		p2.setId("011");
		p2.setName("jack");
		p2.setAge(32);
		// 3、生成一个事务
		Transaction tx = session.beginTransaction();
		session.save(p1);
		session.save(p2);
		tx.commit();
		// .........
		MySession.close(session);
	}
}

  

---恢复内容结束---

原文地址:https://www.cnblogs.com/yongyao/p/5439993.html