Hibernate 的基本开发步骤
1.编写配置文档hibernat.cfg.xml
例如:
<session-factory>
<!--数据库用户名-->
<property name="connection.username">root</property>
<!--数据库登录密码-->
<property name="connection.password">123456</property>
<!--jdbc驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--设置端口 字符集等 -->
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate?useUnicode=true&characterEncoding=utf-8</property>
<!--方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--是否直接打印出sql语句 -->
<property name="show_sql">true</property>
<!--sql格式 -->
<property name="format_sql">true</property>
<!--ddl生成策略-->
<property name="hbm2ddl.auto">update</property>
<!--对应的映射 -->
<mapping resource="Students.hbm.xml"/>
</session-factory>
单一主键:
1.assigned 由java应用程序负责生成(手工赋值)
2.native 由底层数据库自动生成标示符,如果是MySQL就是increment,如果是Oracle就是sequence,等等
Ps1:assigned注意:如果实体类中设置的主键id是基本类型int的话,则可以不用赋值,系统默认值为0;如是引用类型Integer话,则默认值为null,不赋值系统则报错。
Ps2:native注意:系统会自动选择该数据库对应的自动增值方式,从1开始。即使手动给他赋值,也不会起作用,但也不会报错。
2.编写实体类
需要符合JAVA BEAN 规范 即:
1、公有的类。
2、提供公有的不带参数的默认的构造方法。
3、属性私有。
4、属性setter/getter封装
3.生成对应实体类的映射文件
可在包上鼠标右键,点击new->other->Hibernate->Hibernate XML Mapping file(hbm.xml)自动创建
如:
<hibernate-mapping>
<class name="Students" table="STUDENTS">
<id name="id" type="int">
<column name="ID" />
<!--主键策略-->
<generator class="assigned" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
......................
<!--
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
-->
<property name="picture" type="java.sql.Blob">
<column name="PICTURE" />
</property>
<component name="address" class="Address">
<property name="postcode" column="POSTCODE"/>
<property name="phone" column="PHONE"/>
<property name="address" column="ADDRESS"/>
</component>
</class>
</hibernate-mapping>
常用配置如下:
4.调用Hibernate API进行测试
例如:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.Date;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//测试类
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//会话对象
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
}
@After
public void distory(){
//提交事务
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testSaveStudents(){
//生成学生对象
Students s = new Students(1,"小明","男",new Date());
Address address = new Address("312312","09876666","杭州");
s.setAddress(address);
session.save(s);//保存对象进入数据库
}
@Test
public void testWriteBlob()throws Exception{
Students s = new Students(4,"小明","男",new Date());
//获得照片文件
File f = new File("f:"+File.separator+"头像.jpg");
//获得输入流
InputStream input = new FileInputStream(f);
//创建一个Blob对象
Blob image = Hibernate.getLobCreator(session).createBlob(input, input.available());
//设置照片属性
s.setPicture(image);
//保存学生
session.save(s);
}
@Test
public void testReadBlob()throws Exception
{
Students s = (Students) session.get(Students.class, 4);
//获得blob对象
Blob image =s.getPicture();
//创建输入流
InputStream input = image.getBinaryStream();
//创建输出流
File f = new File("f:"+File.separator+"头像2.jpg");
OutputStream output = new FileOutputStream(f);
//缓冲区
byte[] buff = new byte[input.available()];
input.read(buff);
output.write(buff);
input.close();
output.close();
}
Hibernate其他一些基础知识
transaction:事务
Hibernate对数据的操作都是封装在事务当中,并且默认是非自动提交的方式。所以用session保存对象时,如果不开启事务,并且手工提交事务,对象并不会真正保存在数据库中 如上例中的:
//开启事务
transaction = session.beginTransaction();
//提交事务
transaction.commit();
session
1.getCurrentSession在事务提交或者回滚之后会自动关闭,而openSesssion需要你手动关闭。
如果使用openSession而没有手动关闭,多次之后会导致连接池溢出!
2.openSession每次创建新的session对象,
getCurrentSession使用现有的session对象(现有session没commit之前)
基本类型
对象类型
注:本文图片等部分来源于慕课网教程=.=