从零打造在线网盘系统之Hibernate框架起步
歡迎瀏覽Java工程師SSH教程從零打造在線網(wǎng)盤系統(tǒng)系列教程,本系列教程將會(huì)使用SSH(Struts2+Spring+Hibernate)打造一個(gè)在線網(wǎng)盤系統(tǒng),本系列教程是從零開始,所以會(huì)詳細(xì)以及著重地闡述SSH三個(gè)框架的基礎(chǔ)知識(shí),第四部分將會(huì)進(jìn)入項(xiàng)目實(shí)戰(zhàn),如果您已經(jīng)對(duì)SSH框架有所掌握,那么可以直接瀏覽第四章,源碼均提供在GitHub/ssh-network-hard-disk上供大家參閱
本章的學(xué)習(xí)目標(biāo)
- 明白什么是Hibernate
- 掌握XML配置Hibernate
- 掌握注解配置Hibernate
- Hibernate的基本增刪改查的使用
Hibernate概述
Hibernate是一個(gè)對(duì)象關(guān)系映射框架,可以通過XML配置文件或者注解將數(shù)據(jù)庫與實(shí)體Bean進(jìn)行映射
Hibernate常規(guī)配置步驟
- 配置Hibernate
- 建立實(shí)體Bean及其映射文件
- 建立會(huì)話(Session)工廠
- 通過會(huì)話(Sessioon)工廠操作會(huì)話
配置Hibernate
對(duì)于配置Hibernate我們有很多種方法,例如XML配置,屬性文件配置,編程方式配置,注解配置等.
XML配置 小節(jié)完整示例代碼下載
編寫名為Hibernate.cfg.xml的xml文件放入資源目錄,根據(jù)下面形式對(duì)Hibernate進(jìn)行配置
<?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 元素用于配置Hibernate中的屬性鍵:值--><!-- hibernate.connection.driver_class : 連接數(shù)據(jù)庫的驅(qū)動(dòng) --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!-- hibernate.connection.username : 連接數(shù)據(jù)庫的用戶名 --><property name="hibernate.connection.username">root</property><!-- hibernate.connection.password : 連接數(shù)據(jù)庫的密碼 --><property name="hibernate.connection.password">jimisun</property><!-- hibernate.connection.url : 連接數(shù)據(jù)庫的地址,路徑 --><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property><!-- show_sql: 操作數(shù)據(jù)庫時(shí),會(huì) 向控制臺(tái)打印sql語句 --><property name="show_sql">true</property><!-- format_sql: 打印sql語句前,會(huì)將sql語句先格式化 --><property name="format_sql">true</property><!-- hbm2ddl.auto: 生成表結(jié)構(gòu)的策略配置update(最常用的取值): 如果當(dāng)前數(shù)據(jù)庫中不存在表結(jié)構(gòu),那么自動(dòng)創(chuàng)建表結(jié)構(gòu).如果存在表結(jié)構(gòu),并且表結(jié)構(gòu)與實(shí)體一致,那么不做修改如果存在表結(jié)構(gòu),并且表結(jié)構(gòu)與實(shí)體不一致,那么會(huì)修改表結(jié)構(gòu).會(huì)保留原有列.create(很少):無論是否存在表結(jié)構(gòu).每次啟動(dòng)Hibernate都會(huì)重新創(chuàng)建表結(jié)構(gòu).(數(shù)據(jù)會(huì)丟失)create-drop(極少): 無論是否存在表結(jié)構(gòu).每次啟動(dòng)Hibernate都會(huì)重新創(chuàng)建表結(jié)構(gòu).每次Hibernate運(yùn)行結(jié)束時(shí),刪除表結(jié)構(gòu).validate(很少):不會(huì)自動(dòng)創(chuàng)建表結(jié)構(gòu).也不會(huì)自動(dòng)維護(hù)表結(jié)構(gòu).Hibernate只校驗(yàn)表結(jié)構(gòu). 如果表結(jié)構(gòu)不一致將會(huì)拋出異常.--><property name="hbm2ddl.auto">update</property><!-- 數(shù)據(jù)庫方言配置org.hibernate.dialect.MySQLDialect (選擇最短的)--><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- hibernate.connection.autocommit: 事務(wù)自動(dòng)提交 --><property name="hibernate.connection.autocommit">true</property><!-- 將Session與線程綁定=> 只有配置了該配置,才能使用getCurrentSession --><property name="hibernate.current_session_context_class">thread</property><!-- 引入ORM 映射文件填寫src之后的路徑--><mapping resource="com/jimisun/domain/User.hbm.xml"/></session-factory>
</hibernate-configuration> 屬性文件配置 小節(jié)完整示例代碼下載
編寫hibernate.properties的properties配置文件放入資源目錄,配置形式如下
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update 編程配置
編程配置hibernate在實(shí)際開發(fā)中使用較少,了解即可
Configuration configuration = new Configuration();configuration.addResource("mapping.xml").setProperty("connection.username", "root").setProperty("connection.password", "jimisun").setProperty("dialect", "org.hibernate.dialect.MySWLDialect").setProperty("connection.url", "jdbc:mysql://localhost:3306/test").setProperty("connection.driver_class", "com.mysql.jdbc.Driver"); 注解配置 小節(jié)完整示例代碼下載
注解配置嚴(yán)格上來說并不是一種配置方式,仍然需要使用XML或者properties將Hibernate進(jìn)行配置,在Bean實(shí)體和表之間的映射關(guān)系我們就可以使用注解進(jìn)行配置,就不需要編寫B(tài)ean對(duì)應(yīng)的映射XML文件
首先使用XML或者properties配置hibernate @Entity
@Table(name = "user")
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;private String username;private String password;} Session增刪改查示例 小節(jié)完整示例代碼下載
/*** 保存&更新User*/static void addUser() {Transaction transaction = session.beginTransaction();User user = new User();user.setUsername("jimisunl");user.setPassword("jimisun");session.saveOrUpdate(user);transaction.commit();} /*** 查找User** @param theClass* @param id* @return*/static User getUser(Class theClass, Serializable id) {return (User) session.find(theClass, id);}
/*** 刪除User* @param object*/static void deleteUser(Object object) {Transaction transaction = session.beginTransaction();session.delete(object);transaction.commit();} 本篇總結(jié)
掌握Hibernate的配置方式,能使用的HibernateSessionFactory的Session進(jìn)行增刪改查操作
轉(zhuǎn)載于:https://www.cnblogs.com/jimisun/p/9938384.html
總結(jié)
以上是生活随笔為你收集整理的从零打造在线网盘系统之Hibernate框架起步的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的世界小麦种子怎么获得?
- 下一篇: 2018.11.12