zoukankan      html  css  js  c++  java
  • 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);
    	}
    }
    

      

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

  • 相关阅读:
    自由职业者:如何找到你的第一个客户 【转载】
    MPI教程地址
    多线程程序设计.....
    PE格式的深入理解(一)
    用Hook解决在VC++与C++Builder方面界面设计的一些问题
    ANSI,MBCS,Unicode与使用swprintf的陷阱
    关于单片机程序
    C#操作SharePoint列表
    CAML中比较日期时间类型[转]
    使用C#创建webservice及三种调用方式
  • 原文地址:https://www.cnblogs.com/yongyao/p/5439993.html
Copyright © 2011-2022 走看看