1.写映射文件 实体类名.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 package="doamin">
<class name="AccountEntity" table="account" schema="spring1">
<id name="id" column="id">
<!--generator指定主键的生成方式。native是使用本地数据库的自增长能力-->
<generator class="native"></generator>
</id>
<property name="name" column="name"/>
<property name="money" column="money"/>
</class>
</hibernate-mapping>
2.写核心配置文件 hibernate.cfg.xml
<?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>
<!--1.连接数据库的信息-->
<property name="connection.url">jdbc:mysql://localhost:3306/spring1</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!--2.hibernate可选配置-->
<!--检测实体类的映射配置和数据库的表结构是否一致,如果不一致,更新表结构-->
<property name="hbm2ddl.auto">update</property>
<!--数据库方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--是否显示hibernate生成的sql语句-->
<property name="hibernate.show_sql">true</property>
<!--是否使用格式化输出sql语句-->
<property name="hibernate.format_sql">true</property>
<!--3.设置hibernate的连接池提供商-->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<!--4.映射文件的位置-->
<mapping class="doamin.AccountEntity"/>
<mapping resource="doamin/AccountEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.1一般简单用法:使用Hibernate操作数据库,将实体对象存入数据库
/**
* @Author: David.Xiao
* @Date: 2018/10/11 19:33
* @Description: 使用Hibernate操作数据库的方法
* Configuration 加载配置文件
* SessionFactory 线程安全的,一个应用应该只有一个SessionFactory,在应用加载时创建,应用卸载时销毁
* Session 负责操作数据库,应用程序与数据库之间交互操作的一个单线程对象(一个线程只能有一个Session)
* Transaction 负责提交和回滚事务
*/
public class Test1 {
public static void main(String[] args)
{
AccountEntity account = new AccountEntity();
account.setName("kobe");
account.setMoney(9000.0);
// 1.加载主配置文件
org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
// 默认到类的根路径下(maven中是resource)加载cfg.xml配置文件
cfg.configure();
// 2.根据配置文件创建SessionFactory
SessionFactory sessionFactory = cfg.buildSessionFactory();
// 3.创建Session
Session session = sessionFactory.openSession();
// 4.开启事务
Transaction transaction = session.beginTransaction();
// 5.执行操作,增
session.save(account);
// 6.提交事务
transaction.commit();
// 7.释放资源
session.close();
sessionFactory.close();
}
}
操作示例:
增:
AccountEntity account = new AccountEntity();
account.setName("kobe");
account.setMoney(9000.0);
session.save(account);
查:id为5的客户
// 查询id为5的客户
AccountEntity accountEntity = session.get(AccountEntity.class,5);
改:id为5的客户的Money
AccountEntity accountEntity = session.get(AccountEntity.class,5);
accountEntity.setMoney(7777.0);
session.update(accountEntity);
删:id为4的客户
AccountEntity accountEntity = session.get(AccountEntity.class,4);
session.delete(accountEntity);
3.2.实际用法
3.2.1 创建Hibernate的操作工具类,将SessionFactory对象放在静态代码中创建(即只在初始化类时创建一次),以后每次只需要调用其opensession方法,创建session即可,避免了每执行一次操作都创建一次SessionFactory对象的资源开销
package dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* @Author: David.Xiao
* @Date: 2018/10/11 21:17
* @Description:抽取hibernate的工具类
*/
public class HibernateUtil {
private static org.hibernate.SessionFactory factory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
factory = configuration.buildSessionFactory();
} catch (HibernateException e) {
System.out.println("初始化SessionFactory失败");
e.printStackTrace();
}
}
public static Session openSession()
{
return factory.openSession();
}
}
3.2.2 操作数据库,增删改查
package dao;
import doamin.AccountEntity;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
/**
* @Author: David.Xiao
* @Date: 2018/10/11 21:28
* @Description: Hibernate增删改查
*/
public class HibernateUtilTest {
@Test
public void testSave()
{
AccountEntity accountEntity = new AccountEntity();
accountEntity.setName("MacGrady");
accountEntity.setMoney(8000.0);
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
session.save(accountEntity);
tx.commit();
session.close();
}
@Test
public void testFindOne()
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
// 查询id为5的客户
AccountEntity accountEntity = session.get(AccountEntity.class,5);
System.out.println(accountEntity.toString());
tx.commit();
session.close();
}
@Test
public void testUpdate()
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
// 修改id为5的客户的Money
AccountEntity accountEntity = session.get(AccountEntity.class,5);
accountEntity.setMoney(7777.0);
session.update(accountEntity);
tx.commit();
session.close();
}
@Test
public void testDelete()
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
// 修改id为4的客户
AccountEntity accountEntity = session.get(AccountEntity.class,4);
session.delete(accountEntity);
tx.commit();
session.close();
}
@Test
public void openSession() {
}
}
4.get方法和load方法对比
package Test;
import dao.HibernateUtil;
import doamin.AccountEntity;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* @Author: David.Xiao
* @Date: 2018/10/12 10:33
* @Description: Hibernate中查询的方法 get/load(都是根据id获取实体) 区别
* 1.查询的时机:get每次调用get方法时,马上发起查询,即立即加载
* load默认每次真正使用的时候,发起查询,即延迟加载(可通过配置改为立即加载)
* 2.返回的结果:get返回实体对象
* load返回动态代理对象
*/
public class Test3 {
public static void main(String[] args) {
/**
* get()方法
*/
Session session = HibernateUtil.openSession();
Transaction transaction = session.beginTransaction();
AccountEntity accountEntity = session.get(AccountEntity.class,2);
System.out.println("get : "+accountEntity);
/**
* load()方法
*/
AccountEntity accountEntity2 = session.load(AccountEntity.class,2);
System.out.println("load : "+accountEntity);
}
}
5.在Hibernate中使用JDBC API
package Test;
import dao.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @Author: David.Xiao
* @Date: 2018/10/12 10:21
* @Description: 在Hibernate中使用原始JDBC API
* JDBC API:
* Connection
* Statement
* PreparedStatement
* ResultSet
*/
public class Test2 {
public static void main(String[] args) {
Session session = HibernateUtil.openSession();
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.out.println(connection.getClass().getName());
}
});
}
}