乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      hibernate入門(mén)---uuid.hex生成方式【依據(jù)機(jī)器標(biāo)識(shí)等自生】【第二天】

       印度阿三17 2019-05-30

      在進(jìn)行這個(gè)實(shí)例之前,先說(shuō)之前出現(xiàn)的問(wèn)題:

      1、類(lèi)前面一定要避免有空格,在這之前,從未想過(guò)類(lèi)名前面有空格還能創(chuàng)建文件;在配置的時(shí)候,會(huì)出現(xiàn)如果沒(méi)有空格,就無(wú)法找到映射類(lèi)。當(dāng)然,加了空格就可以,但請(qǐng)避免使用。

      2、在配置過(guò)程中,請(qǐng)一定要根據(jù)需要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù)表,并且這個(gè)表的主鍵是否考慮整型,默認(rèn)值,是否自增等。

      3、運(yùn)行過(guò)程中報(bào)錯(cuò),優(yōu)先看控制臺(tái),判斷出錯(cuò)問(wèn)題,快速定位;如無(wú)法找到,再運(yùn)用其他技巧。

      下面:先將之前幾個(gè)實(shí)例用到的數(shù)據(jù)庫(kù)表及本次使用的表貼出來(lái):

      表1:

      CREATE TABLE `customers` (
        `customerID` varchar(8) NOT NULL,
        `name` varchar(15) DEFAULT NULL,
        `phone` varchar(16) DEFAULT NULL,
        PRIMARY KEY (`customerID`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

      表2:

      CREATE TABLE `customers2` (
        `customerID` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(15) DEFAULT NULL,
        `phone` varchar(16) DEFAULT NULL,
        PRIMARY KEY (`customerID`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1015 DEFAULT CHARSET=utf8;

      ?

      表3:

      CREATE TABLE `customers3` (
        `customerID` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(15) DEFAULT NULL,
        `phone` varchar(16) DEFAULT NULL,
        PRIMARY KEY (`customerID`)
      ) ENGINE=InnoDB AUTO_INCREMENT=1015 DEFAULT CHARSET=utf8;

      ?

      表4:(略,Oracle數(shù)據(jù)庫(kù)測(cè)試)

      表5:

      CREATE TABLE `customers5` (
        `customerID` varchar(50) NOT NULL,
        `name` varchar(15) DEFAULT NULL,
        `phone` varchar(16) DEFAULT NULL,
        PRIMARY KEY (`customerID`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

      表5示例 uuid.hex生成方式【依據(jù)機(jī)器標(biāo)識(shí)等自生】

      一、Customer.hbm.xml

      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE hibernate-mapping PUBLIC 
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www./dtd/hibernate-mapping-3.0.dtd">
      
      
      <hibernate-mapping package="bean" auto-import="false">
         <!-- POJO類(lèi)映射表及某表的關(guān)系-->
         <class name="bean.Customer5" table="customers5" catalog="test">
             <id name="customerID" type="java.lang.String">
                 <column name="customerID"/>
                 <generator class="uuid.hex"></generator>
             </id>
             <!-- 映射表中name字段 -->
             <property name="name" type="java.lang.String">
                <column name="name" length="40"/>
             </property>
             <!-- 映射表中phone字段 -->
             <property name="phone" type="java.lang.String">
                <column name="phone" length="16"/>
             </property>  
         </class>
      </hibernate-mapping>

      二、hibernate.cfg.xml

      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www./dtd/hibernate-configuration-3.0.dtd">
      <hibernate-configuration>
      <!-- 配置文件標(biāo)簽順序property*,mapping*,(class-cache|collection-cache),event,listener* -->
          <session-factory>
            <!-- 設(shè)置訪問(wèn)mysql數(shù)據(jù)庫(kù)的驅(qū)動(dòng)描述 -->
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <!-- 設(shè)置數(shù)據(jù)庫(kù)的url -->
            <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
            <!-- 指定登錄數(shù)據(jù)庫(kù)用戶賬戶 -->
            <property name="connection.username">root</property>
            <!-- 指定登錄數(shù)據(jù)庫(kù)用戶密碼 -->
            <property name="connection.password">123456</property>
            
            <!-- 設(shè)置訪問(wèn)數(shù)據(jù)庫(kù)的方言,提高數(shù)據(jù)庫(kù)訪問(wèn)性能 -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <!-- 設(shè)置ddl -->
            <!-- <property name="hbm2ddl.auto">auto</property> -->
             <!-- 配置控制臺(tái)視圖,顯示查詢(xún)內(nèi)容 -->
             <property name="show_sql">true</property>
             <!-- 下面是多表映射 -->
            <!-- 指出映射文件 -->
            <mapping resource="resource/Customer.hbm.xml"/>
            <!-- 映射文件 -->
            <mapping resource="resource/Customer2.hbm.xml"/>
            <!-- 映射文件 -->
            <mapping resource="resource/Customer3.hbm.xml"/>
            <!-- 映射文件 -->
            <mapping resource="resource/Customer5.hbm.xml"/>
          </session-factory>
      </hibernate-configuration>

      三、Customer5.java

      package bean;
      
      //驗(yàn)證uuid.hex生成主鍵方式的映射類(lèi),數(shù)據(jù)庫(kù)對(duì)應(yīng)表customers2
      public class Customer5 {
          private String customerID;
          private String name,phone;
          
          public String  getCustomerID() {
              return customerID;
          }
      
          public void setCustomerID(String  customerID) {
              this.customerID = customerID;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public String getPhone() {
              return phone;
          }
      
          public void setPhone(String phone) {
              this.phone = phone;
          }
      }

      四、HibernateSessionFactory.java

      package hibernate.factory;
      
      
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.cfg.Configuration;
      
      
      public class HibernateSessionFactory {
          private static String configfile = "resource/hibernate.cfg.xml";
          /**ThreadLocal是一個(gè)本地線程**/
          private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
          private static Configuration config;
          private static SessionFactory sessionFactory;
          /**讀取配置文件,創(chuàng)建一個(gè)工廠會(huì)話,這段代碼為靜態(tài)塊,編譯后已經(jīng)運(yùn)行**/
          static{
              try {
                  config = new Configuration().configure(configfile);
                  sessionFactory = config.buildSessionFactory();
              } catch (Exception e) {
                  // TODO: handle exception
                  e.printStackTrace();
              }
          }
          /**通過(guò)會(huì)話工廠打開(kāi)會(huì)話,就可以訪問(wèn)數(shù)據(jù)庫(kù)了**/
          public static Session getSession(){
              Session session = (Session)threadLocal.get();
              if (session==null||!session.isOpen()) {
                  if (sessionFactory==null) {
                      rebuildSessionFactory();
                  }
                  session = (sessionFactory!=null)?sessionFactory.openSession():null;
              }
              return session;
          }
          /**重新創(chuàng)建一個(gè)會(huì)話工廠**/
          public static void rebuildSessionFactory() {
              try {
                  config.configure(configfile);
                  sessionFactory = config.buildSessionFactory();
              } catch (Exception e) {
                  // TODO: handle exception
                  e.printStackTrace();
              }
          }
          /**關(guān)閉與數(shù)據(jù)庫(kù)的會(huì)話**/
          public static void closeSession() {
              Session session = (Session)threadLocal.get();
              threadLocal.set(null);
              if (session!=null) {
                  session.close();
              }
          }
      }

      五、Customer5Demo.java

      package bean;
      
      import java.util.List;
      
      
      
      
      import hibernate.factory.HibernateSessionFactory;
      
      import org.hibernate.Session;
      import org.hibernate.Transaction;
      import org.hibernate.query.Query;
      
      
      //用于測(cè)試increment主鍵生成方式,增加記錄,不建議使用,他是實(shí)例自增,多實(shí)例訪問(wèn)時(shí),會(huì)重復(fù)主鍵,出問(wèn)題
      //由于是增加數(shù)據(jù),所以不需要寫(xiě)Query,只需要new表,增加數(shù)據(jù)即可
      public class Customer5Demo {
          Session session = HibernateSessionFactory.getSession();
          Transaction tran = session.beginTransaction();
          public static void main(String[] args) {
              
              //測(cè)試increment主鍵生成
              Customer5Demo demo = new Customer5Demo();
              Customer5 customer5 = new Customer5();
              demo.saveCustomer5IDByIdentity(customer5, "華山", "580");
              //測(cè)試查詢(xún)結(jié)果
              List list = demo.queryAllCustomer5();
              for (int i = 0; i < list.size(); i  ) {
                  Customer5 customer  = (Customer5)list.get(i);
                  System.out.println(customer.getCustomerID() customer.getName() customer.getPhone());
              }
              HibernateSessionFactory.closeSession();
              
          }
          
          //下面是封裝保存
          public void saveCustomer5IDByIdentity(Customer5 customer5,String name,String phone) {
              customer5.setName(name);
              customer5.setPhone(phone);
              session.save(customer5);
              //一定一定要記得提交事務(wù)
              tran.commit();
          }
          //下面是封裝查詢(xún)
          @SuppressWarnings("rawtypes")
          public List queryAllCustomer5(){
              /**由會(huì)話工廠類(lèi)創(chuàng)建一個(gè)會(huì)話Session對(duì)象**/
              Session session = HibernateSessionFactory.getSession();
              /**由會(huì)話session對(duì)象創(chuàng)建一個(gè)查詢(xún)對(duì)象**/
              Query query = session.createQuery("from bean.Customer5");
              List list = query.list();
              HibernateSessionFactory.closeSession();
              return list;
          }
      }

      ?

      來(lái)源:http://www./content-4-218801.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類(lèi)似文章 更多