- intellij新建Maven項目
- 在src/resource下新建
hibernate.cfg.xml
文件,一定要在src/resource目錄下,否則會報無法定位資源文件的錯誤。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--數據庫url,以及防止中文亂碼-->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate-final?useUnicode=true&characterEncoding=UTF-8</property>
<!--Mysql驅動-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--Mysql用戶名-->
<property name="connection.username">root</property>
<!--Mysql密碼-->
<property name="connection.password">rootPwd</property>
<!--方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--調試時顯示sql語句-->
<property name="show_sql">true</property>
<!--格式化調試時輸出的sql語句-->
<property name="format_sql">true</property>
<!-- DB schema will be updated if needed -->
<!--create表結構每次發生改變都會刪除原來的表,創建新的表。update是在原有的表上進行更新,保留數據-->
<property name="hbm2ddl.auto">update</property>
<!--<property name="hbm2ddl.auto">create</property>-->
<!--映射文件-->
<mapping resource="StudentEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 不要使用intellij的Add Framework Support,可能是intellij的hibernate版本太低了,使用這種方式生成數據庫表的時候會報錯,"type"在mysql高版本中已經被刪除。
- 在項目的pom文件中添加hibernate依賴
<!--hibernate依賴-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<!--單元測試依賴-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<!--mysqlJDBC依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
- 新建持久化類
package Entity;
/**
* Created by futao on 2017/9/24.
*/
public class Student {
//JavaBean(持久化類)編寫規范
//1.公有的類
//2.私有屬性
//3.提供公有的不帶參數的默認構造方法
//4.私有屬性的公有getter和setter
//5.為方便實例化對象,一般生成一個帶參的構造方法
//設置public static final String的意義在于:比如 .add(Restrictions.eq())的時候,可以直接通過實體的類型點出來,而不用手動輸入,這樣避免了手動輸入發生錯誤的情況
private int sid;
public static final String _sid = "sid";
private String name;
public static final String _name = "name";
private String gender;
public static final String _gender = "gender";
private Timestamp birthday;
public static final String _birthday = "birthday";
private String address;
public static final String _address = "address";
private String tel;
public static final String _tel = "tel";
public Student(){ }
public Student(int sid, String name, String gender, String birthday, String address, String tel) {
this.sid = sid;
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.address = address;
this.tel = tel;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
- 在src/resource文件夾下創建實體映射文件
StudentEntity.hbm.xml
,maven項目的配置文件應該都應該放在這個文件夾下,否則都會報錯。
<?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="Entity.Student" table="t_student">
<id name="sid">
<column name="sid" sql-type="int(11)"/>
<generator class="native"/>
</id>
<property name="name">
<column name="name" sql-type="varchar(255)" not-null="true"/>
</property>
<property name="birthday">
<column name="birthday" sql-type="varchar(255)" not-null="true"/>
</property>
<property name="address">
<column name="address" sql-type="varchar(255)" not-null="false"/>
</property>
</class>
</hibernate-mapping>
- 在hibernate主配置文件中添加實體映射文件
<!--映射文件-->
<mapping resource="StudentEntity.hbm.xml"/>
- 測試
package Entity
import org.hibernate.cfg.Configuration
import org.junit.Test
@Suppress("DEPRECATION")
/**
* Created by futao on 2017/9/24.
*/
class t{
@Test
fun testInit(){
//加載配置文件
val configuration=Configuration().configure()
//創建會話工廠
val sessionFactory=configuration.buildSessionFactory()
//創建會話
val session=sessionFactory.openSession()
//開啟事務
val transaction=session.beginTransaction()
//crud
session.save(Student(1,"熊小二","男", "2017-9-24","上海市小二房","1887978252"))
//提交事務
transaction.commit()
//關閉會話
session.close()
//關閉會話工廠
sessionFactory.close()
}
}
基礎版完成 ↑↑↑↑↑↑↑
Hibernate執行流程
Hibernate執行流程
session簡單地理解就是一個操作數據庫的對象
hibernate的操作必須包裝在事務當中,否則操作不會同步到數據庫(或者使用doWord()的方式)
創建session對象的兩種方式
- val session=sessionFactory.openSession()
- val session=sessionFactory.currentSession
使用第二種方式獲得session需要在hibernate主配置文檔中進行配置
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
hbm常用配置
mapping
class
id
主鍵生成策略
基本類型
timestamp時間戳
image.png
長文本
//Entity
private Blob picture;//照片,長文本
public static final String _picture = "picture";
//hbm.xml
<property name="picture">
<column name="picture" sql-type="longblob" not-null="false"/>
</property>
//Test 存
val f= File("d:"+File.separator+"banner.png")
val input=FileInputStream(f)
val blob=Hibernate.getLobCreator(session).createBlob(input, input.available().toLong())
session.save(Student(5,"熊","男", Timestamp(DateTime.now().millis) ,"上海市小二房","1887978252",blob))
// 取
val entity = session.get(Student::class.java, 4) as Student
//獲得Blob對象
val pic= entity.picture
//獲得照片的輸入流
val inPut=pic.getBinaryStream()
//創建輸出流
val file = File("d:"+File.separator+"bannerNew.png")
//獲得輸出流
val outPut=FileOutputStream(file)
//創建緩沖區
var buff = ByteArray(inPut.available())
inPut.read(buff)
outPut.write(buff)
inPut.close()
outPut.close()
組件屬性
組件屬性
<?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="Entity.Student" table="t_student">
<id name="sid">
<column name="sid" sql-type="int(11)"/>
<generator class="native"/>
</id>
<property name="name">
<column name="name" sql-type="varchar(255)" not-null="true"/>
</property>
<property name="birthday">
<column name="birthday" sql-type="timestamp" not-null="true"/>
</property>
<!--<property name="address">-->
<!--<column name="address" sql-type="varchar(255)" not-null="false"/>-->
<!--</property>-->
<property name="picture">
<column name="picture" sql-type="longblob" not-null="false"/>
</property>
<!--組件屬性-->
<component name="addressEntity" class="Entity.Address">
<property name="postCode">
<column name="postCode"/>
</property>
<property name="phone">
<column name="phone"/>
</property>
<property name="address">
<column name="address"/>
</property>
</component>
</class>
</hibernate-mapping>
組件屬性的所有property會在實體(Student)對應的表中生成相應的字段
table
get和load
1
2
項目組成介紹