hibernate介绍:
1.1 什么是hibernate?
hibernate是一个ORM思想的持久层框架
1.2 什么是ORM
Object Relational Mapping,对象关系映射;将实体类中的属性名称跟数据库表中的字段名对应起来,从而达到操作实体类就能操作数据库表的效果
1.3 org.hibernate.Session的API
增
save(Object o);
删
delete(Object o); //推荐先查再删
改
update(Object o); //推荐先查再改
查
get(Class<Object> clazz,Object id); //立即加载
load(Class<Object> clazz,Object id); //延迟加载
1.4 get和load方法的区别:
get方法:
立即加载,查询的时候立刻发送SQL;查询的结果返回的是真实的实体类对象;如果不存在这个对象就返回null
load方法:
延迟加载,只有当使用到非主键的实体类属性时才会发送SQL;查询结果返回的是基于子类的动态代理对象,如果使用final关键字来修饰实体类,将不会获得延迟加载的效果;如果不存在这个对象就报Object Not Found Exception
1.5在maven项目中,编译器默认不会对src/main/java下的xml文件进行编译,因此会导致hibernate.cfg.xml加载实体类映射配置文件失效;
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
入门测试详解
1.Configuration:加载核心配置文件
1.1加载hibernate.properties
Configuration configuration = new Configuration();
configuration.addResource("com/baidu/hibernate/domain/Customer.hbm.xml");
1.2加载hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
2.SessionFactory:负责管理Session,管理连接池,管理Hibernate二级缓存。采用了工厂模式,不是一个轻量级的对象。通常情况下一个项目只对应一个SessionFactory就够了。是线程安全的对象。
3.Session:相当于Connection,Session是Hibernate程序与数据库之间的桥梁。完成CRUD的操作。Session是一个单线程的对象,内部维护了Hibernate的一级缓存。
4.Transaction:事务对象
Hibernate3的时候事务自动提交的参数是false(不自动提交)。Hibernate5可以自动提交,但是默认每个操作都是在一个事务中。一般需要手动开启事务。
Hibernate入门测试
1.添加依赖
下载开发包https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/
2.创建实体类
3.在类路径下创建hibernate核心配置文件,通常叫做hibernate.cfg.xml
4.配置实体类的映射配置文件,通常命名规则:实体类名.hbm.xml
注意:mybatis中的映射配置文件是dao层接口的映射配置文件,而hibernate是实体类的映射配置文件
5.Junit单元测试
@Test
public void testSave(){
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
customer customer = new Customer();
customer.setName("张三");
session.save(customer);
transaction.commit();
session.close();
}
简单CRUD
public class HibernateTest {
private Session session;
private Transaction transaction;
@Before
public void init() {
session = HibernateUtil.openSession();
transaction = session.beginTransaction();
}
@After
public void destroy() {
if (session != null)
session.close();
}
@Test
public void testSave() {
transaction.begin();
Customer customer = new Customer();
customer.setCust_name("张三");
session.save(customer);
transaction.commit();
}
@Test
public void testDelete() {
transaction.begin();
Customer customer = session.get(Customer.class, 1L);
session.delete(customer);
transaction.commit();
}
@Test
public void testUpdate(){
transaction.begin();
Customer customer = session.get(Customer.class, 1L);
customer.setCust_name("李四");
session.update(customer);
transaction.commit();
}
@Test
public void testFind(){
Customer customer = session.get(Customer.class,1L);
}
}
核心配置文件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:///hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">120</property>
<property name="c3p0.idle_test_period">3000</property>
<mapping resource="com/baidu/hibernate/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
实体类映射配置文件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="com.baidu.hibernate.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name" length="32" type="string"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>