Hibernate是数据持久层框架,用于将数据持久化到数据库
核心组件:
1.Configuration类:用于读取配置文件,生成SessionFactory
2.SessionFactory接口:用于生成Session实例的工厂
3.Session接口:用于操作PO,封装了增删改查等方法
4.Transaction接口:用于事物管理的接口,通过Session获得
一、jar包支持
|
二、下面来看一个简单的例子:
1.PO类:(为了查看结果方便,重写toString方法)
public class User { private int id; private Stringaccount; private Stringpassword; //无参构造 public User() {} //有参构造 public User(String account, String password) { super(); this.account = account; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public StringtoString() { return "User [id=" +id + ", account=" +account + ", password=" + password +"]"; } }
|
2.User.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="com.zrgk.pojo"> <class name="User" table="user"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="account" column="account"></property> <property name="password" column="password"></property> </class> </hibernate-mapping> |
3.数据库结构:
|
4.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://localhost:3306/struts</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 配置方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 显示和格式化sql语句 ,以便于在控制台打印--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 配置映射文件 --> <mapping resource="com/zrgk/pojo/User.hbm.xml"/> </session-factory> </hibernate-configuration> |
5.测试类:
//hibernate测试 public class TestHibernate { @Test public void test01(){ //创建对象 User user = new User(); user.setAccount("hibernate"); user.setPassword("123"); //获得配置对象 Configuration configure = new Configuration().configure(); //获得会话工厂 SessionFactory sessionFactory = configure.buildSessionFactory(); //获取会话 Session session = sessionFactory.openSession(); //开始事物 Transaction transaction = session.beginTransaction(); //执行操作.返回该对象在数据库中生成的id int id= (Integer) session.save(user); System.out.println(id); transaction.commit(); //关闭资源 session.close(); sessionFactory.close(); } } |
|
三、简单介绍下上面的小例子:
1.实体类必须提供无参的构造函数
因为Hibernate是通过反射来建立实体类的对象,是调用的实体类的无参构造,再通过该类的set方法将返回的数据封装进对象。如果不提供无参构造,而只提供有参构造,则无法建立对象,封装数据。
2.User.hbm.xml配置文件
通过该配置文件建立起数据实体与数据库表之间的映射关系,hibernate也通过这个配置来对数据进行封装和获取
该配置文件hibernate映射约束的获取:
在该路径下的
如图所示的dtd文件中
如下图:
|
3.hibernate.cfg.xml配置文件
该配置文件主要是配置数据库连接以及添加映射文件等作用
配置文件约束在上图所示的hibernate-configuration-3.0.dtd文件中