(一) maven依赖
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.3.12.Final</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency>
|
(二) 概念
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
(三) 使用
3.1 构造领域模型
User 模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@Data public class User { private int id; private Date birthday; private Date createTime; private String name; private int status; private Date updateTime; private int countryId; }
|
3.2 在类路径/mapping下面配置映射文件
针对 User 类的配置文件 user.hbm.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?xml version="1.0"?> <!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.xiaobu.learn_hibernate.entity"> <class name="User" lazy="true" table="user"> <id name="id" column="id"> <generator class="native"/> </id>
<property name="birthday" column="birthday"/> <property name="countryId" column="country_id"/> <property name="createTime" column="create_time"/> <property name="name" column="name"/> <property name="status" column="status"/> <property name="updateTime" column="update_time"/> </class> </hibernate-mapping>
|
3.3 主键的自动生成策略(<generator class="native"/>
的取值)
identity 自增长(mysql,db2)
sequence 自增长(序列), oracle中自增长是以序列方法实现**
native 自增长【会根据底层数据库自增长的方式选择identity或sequence】
- 如果是mysql数据库, 采用的自增长方式是identity
如果是oracle数据库, 使用sequence序列的方式实现自增长
increment 自增长(会有并发访问的问题,一般在服务器集群环境使用会存在问题。
3.4 在类路径下建立 hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <?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> <property name="connection.url">jdbc:mysql:///test_v2?serverTimezone=Asia/Shanghai</property> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="connection.username">admin</property> <property name="connection.password">199711</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> <property name="hibernate.connection.autocommit">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property>
<mapping resource="mapping/user.hbm.xml"/> </session-factory> </hibernate-configuration>
|
<property name="hibernate.hbm2ddl.auto"/>
的取值
create:表示启动的时候先drop,再create
create-drop: 也表示创建,只不过再系统关闭前执行一下drop
update: 这个操作启动的时候会去检查schema是否一致,如果不一致会做scheme更新
validate: 启动时验证现有schema与你配置的hibernate是否一致,如果不一致就抛出异常,并不做更新
3.5 java代码使用步骤
初始化 SessiongFacttory,这个对象一个程序中只需要一个。
SessionFactory.openSession() 使用 SessionFactory 获得 Session 对象,一个 Session 对象维护一个 Connection 连接。
Transactoin tx = Session.beginTransaction(); 开启事务,获得事务对象,hibernate的所有操作都需要事务。
session的基本增删改查方法
Session.save() | Session.delete() |Session.update() | Session.find()
tx.commit() 提交事务
session.close() 关闭会话
使用工具类来初始化 SessionFactory,并提供获得 Session 的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class HibernateUtil { private static SessionFactory sessionFactory;
static { StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build(); sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); }
public static Session getSession() { return sessionFactory.openSession(); } public static Session getCurrentSession(){ return sessionFactory.getCurrentSession(); } }
|
3.6 简单的增删改查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| public class TestSimpleCRUD extends TestApplication {
@Test public void testSave() { User user = new User(); user.setBirthday(new Date()); user.setCountryId(102); user.setCreateTime(new Date()); user.setName("刘晓彤"); user.setStatus(0); user.setCreateTime(new Date());
Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); session.save(user); tx.commit(); session.close(); }
@Test public void testFind() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); User user = session.find(User.class, 1002); tx.commit(); session.close(); log.info("查询主键为 1002 的数据包装为 User 对象:" + user.toString()); }
@Test public void testUpdate() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); User user = session.find(User.class, 1003); user.setName("王二麻子"); session.save(user); tx.commit(); session.close(); }
@Test public void testDelete() { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); User user = session.find(User.class, 1003); session.delete(user); tx.commit(); session.close(); } }
|
3.7 HQL,QBC,SQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class TestQL extends TestApplication {
@Test public void testHQL() { Session session = HibernateUtil.getSession(); Query query = session.createQuery("from User"); List res = query.list(); log.info("结果:" + res); }
@Test public void testHQLWithParameter() { Session session = HibernateUtil.getSession(); Query query = session.createQuery("from User where name=?1"); query.setParameter(1, "刘晓彤"); List res = query.list(); log.info("结果:" + res); }
@Test public void testSQL() { Session session = HibernateUtil.getSession(); NativeQuery query = session.createSQLQuery("select * from user where id=?"); query.setParameter(1, "1004"); List res = query.list(); log.info("结果:" + res); }
@Test public void testQBC() { Session session = HibernateUtil.getSession(); Criteria criteria = session.createCriteria(User.class); criteria.add(Restrictions.eq("name", "刘晓彤")); List res = criteria.list(); log.info("结果:" + res); } }
|