- 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执行流程
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常用配置
timestamp时间戳
//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)对应的表中生成相应的字段