hibernate版本,Hibernate_01

 2023-11-18 阅读 25 评论 0

摘要:Hibernate hibernate是一個持久層orm框架,目的:簡化項目開發,提高開發效率。 快速入門 下載hibernate,然后導入hibernate->lib->required下面的所有jar包,還有就是數據庫驅動的jar包。創建表cst_customer CREATE TABLE `cst_cu

Hibernate

hibernate是一個持久層orm框架,目的:簡化項目開發,提高開發效率。

快速入門

  • 下載hibernate,然后導入hibernate->lib->required下面的所有jar包,還有就是數據庫驅動的jar包。
  • 創建表cst_customer
    CREATE TABLE `cst_customer` (
    `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)',
    `cust_name` varchar(32) NOT NULL COMMENT '客戶名稱(公司名稱)',
    `cust_source` varchar(32) DEFAULT NULL COMMENT '客戶信息來源',
    `cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業',
    `cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別',
    `cust_address` varchar(128) DEFAULT NULL COMMENT '客戶聯系地址',
    `cust_phone` varchar(64) DEFAULT NULL COMMENT '客戶聯系電話',
    PRIMARY KEY (`cust_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
    
  • 創建相應的實體類 Customer.java

      public class Customer implements Serializable {private static final long serialVersionUID = 1L;private Long id;// 主鍵private String custName;// 客戶姓名private String custSource; // 客戶來源private String custIndustry; // 所屬行業private String custLevel; // 客戶級別private String custAddress; // 地址private String custPhone; // 電話public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getCustName() {return custName;}public void setCustName(String custName) {this.custName = custName;}public String getCustSource() {return custSource;}public void setCustSource(String custSource) {this.custSource = custSource;}public String getCustIndustry() {return custIndustry;}public void setCustIndustry(String custIndustry) {this.custIndustry = custIndustry;}public String getCustLevel() {return custLevel;}public void setCustLevel(String custLevel) {this.custLevel = custLevel;}public String getCustAddress() {return custAddress;}public void setCustAddress(String custAddress) {this.custAddress = custAddress;}public String getCustPhone() {return custPhone;}public void setCustPhone(String custPhone) {this.custPhone = custPhone;}@Overridepublic String toString() {return "Customer [id=" + id + ", custName=" + custName + ", custSource=" + custSource + ", custIndustry="+ custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress + ", custPhone="+ custPhone + "]";}
    }
    
  • 創建對應的配置文件 Customer.hbm.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!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 : 實體類的全限定名table : 對應數據庫的表名(如果類名和數據庫表的名字一樣,可以省略table配置)--><class name="com.xiaoshitou.domain.Customer" table="cst_customer"><!-- id標簽:主鍵name : 實體類中的idcolumn : 數據庫中主鍵的名字--><id name="id" column="cust_id"><!-- gennerator class:主鍵的生成策略;native表示,主鍵由數據庫自動維護 --><generator class="native"></generator></id><!-- property:主鍵以外的普通字段name=>實體類中的成員變量,column=>表中的字段名  --><property name="custName" column="cust_name"></property><property name="custSource" column="cust_source"></property><property name="custIndustry" column="cust_industry"></property><property name="custLevel" column="cust_level"></property><property name="custAddress" column="cust_address"></property><property name="custPhone" column="cust_phone"></property></class>
    </hibernate-mapping>
    
  • 編寫hiberna核心配置文件:一定要放在src目錄下,hibernate.cfg.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.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><!-- 配置數據庫方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 顯示sql --><property name="hibernate.show_sql">true</property><!-- 格式化sql --><property name="hibernate.format">true</property><!-- create-drop:啟動時會創建表,sessionFactory關閉時刪除表,create: 每次都會重新創建表,update: 如果實體類和數據庫的表字段不一樣時,會修改數據庫中的字段,保證實體類和數據庫表是同步的,validate: 如果實體類和數據庫表字段不一致時,會報錯  --><property name="hibernate.hbm2ddl.auto">update</property><!-- 把實體類的配置文件加載到核心配置文件中 --><mapping resource="com/xiaoshitou/domain/Customer.hbm.xml"/></session-factory>
    </hibernate-configuration>
    
  • hibernate版本。編寫測試類,使用hibernate來添加一條數據

    /*** 快速入門:利用hibernate添加一條數據*/@Testpublic void test01() {// 創造一個customer對象Customer customer = new Customer();customer.setCustName("東方科技");customer.setCustLevel("VIP");customer.setCustSource("朋友推薦");customer.setCustIndustry("無人機");customer.setCustAddress("東方1號路");customer.setCustPhone("8221365");// 利用hibernate完成保存Configuration configuration = new Configuration();configuration.configure();// 創建sessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();// 獲取sessionSession session = sessionFactory.openSession();// 開啟事務Transaction tx = session.beginTransaction();// 保存session.save(customer);// 提交事務,關閉資源tx.commit();session.close();sessionFactory.close();}
    

    hibernate 使用c3p0連接池

  • 導入jar包:hibernate-release-5.0.7.Final\lib\optional\c3p0
  • 在hibernate.cfg.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><!-- 使用c3p0連接池 --><property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property><!-- 在數據庫連接池中,最大連接數 --><property name="hibernate.c3p0.max_size">20</property><!-- 在數據庫連接池中,最小連接數 --><property name="hibernate.c3p0.min_size">5</property><!-- 如果連接池中,某個連接空閑時間超過設置的時間,將會被從連接池沖清除 --><property name="hibernate.c3p0.timeout">5000</property><!-- 每個3000秒檢查,連接池中的空閑連接,單位秒 --><property name="hibernate.c3p0.idle_test_period">3000</property><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><!-- 配置數據庫方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 顯示sql --><property name="hibernate.show_sql">true</property><!-- 格式化sql --><property name="hibernate.format_sql">true</property><!-- create-drop:啟動時會創建表,sessionFactory關閉時刪除表,create: 每次都會重新創建表,update: 如果實體類和數據庫的表字段不一樣時,會修改數據庫中的字段,保證實體類和數據庫表是同步的,validate: 如果實體類和數據庫表字段不一致時,會報錯  --><property name="hibernate.hbm2ddl.auto">update</property><!-- 把實體類的配置文件加載到核心配置文件中 --><mapping resource="com/xiaoshitou/domain/Customer.hbm.xml"/></session-factory>
    </hibernate-configuration>
    

    session中的saveOrUpdate方法:

  • 如果對象中沒有設置id,就會新增一條記錄
  • 如果對象中設置了id,并且數據庫中,有對應id的記錄,那么就是修改記錄
  • 如果對象中設置了id,在數據庫中沒有對應id的記錄,那么將會執行失敗,拋出異常。

    session中get和load方法的區別

  • get是立即加載(立即執行sql語句),load是延遲加載(在真正使用對象的屬性時,才會發送sql語句)
  • get返回的對象是要查詢的實體對象,load返回的代理對象

?

轉載于:https://www.cnblogs.com/xiaoshitoutest/p/7359908.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/174191.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息