Hibernate

早期流行的Java ORM框架

基础配置

  • 添加jar包

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.9.Final</version>
    </dependency>

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
    </dependency>
  • 创建java项目,添加hibernate配置

创建.png
生成hibernate.png

配置hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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>
<!--数据库方言配置,hibernate做sql语句生成,
这里使用的是Mysql5.7版本就设置的是MySQL57Dialect-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 是否打印执行的sql,在控制台输出 -->
<property name="show_sql">true</property>
<!-- 是否格式化显示sql语句 -->
<property name="format_sql">true</property>
</session-factory>
</hibernate-configuration>

生成实体类和配置

创建实体类.png
生成实体类.png

hbm.xml配置

  • 修改映射文件的id生成策略
    1. identity:自增
    2. uuid:自动生成uuid
    3. native:根据字段属性,自动选择自增或者生成uuid
    4. assigned:开发人员自己维护,不自动生成
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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="io.jtxyh.entity.Uinfo" table="cinfo" schema="mybatis">
<id name="uid" column="uid">
<!-- 根据字段属性,自动选择 -->
<generator class="native"></generator>
</id>
<property name="uname" column="uname"/>
</class>
</hibernate-mapping>

查询

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
// 获取sqlSessionFactory对象
SessionFactory sq = HibernateUtil.getSessionFactory();
// 获取session对象
session = sq.opernSession();

/**
* 查询所有信息
*/
//Sysuser不是表名,是实体类的名称(严格大小写)
String hql = "from Uinfo";
// 创建查询
Query query = session.createQuery(hql);
// 返回多条记录使用list()
List<Sysuser> list = query.list();

/**
* 条件查询
*/
String hql = "from Uinfo uname:un order by uid";
Query query = session.createQuery(hql);
// 对cd进行赋值查询
query.setParameter("un","111");
// 获取单条记录使用getSingleResult()
Sysuser y = (Sysuser)query.getSingleResult();

/**
* 查询部分字段
*/
String hql = "select uname,uphone from Uinfo order by uid";
// 或者使用这个
// String hql = "select new Uinfo (uname,uphone) from Uinfo where uid = :uid order by uid";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();


/**
* 查询结果封装为map
*/
String hql = "select new map (uname,uphone) from Uinfo where uid = :uid order by uid";
Query query = session.createQuery(hql);
query.setParameter("uid",24);
Map<String,Object> result = (Map<String,Object>)query.getSingleResult();


/**
* 多表连接查询
*/
// 根据id查询用户,并查询角色
// 左外链接:返回的是对象数组
// 迫切左外连接 fetch:返回是单个对象,持有另外一个对象。两个对象之间做了关联
// 需要在实体类中创建一个为连接查询信息的字段
String hql = "from Cinfo r left join fetch r.uinfo where r.rid=:rid";
// 或者
// String hql = "select u from Uinfo u left join u.cinfo where u.uid=:uid";
Query query = session.createQuery(hql);
query.setParameter("uid",24);
Sysuser result = (Sysuser)query.getSingleResult();

session.close();
// 关闭sqlSessionFactory对象
sq.close()

添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 获取sqlSessionFactory对象
SessionFactory sq = HibernateUtil.getSessionFactory();
// 获取session对象
session = sq.opernSession();

// 开启事务
Transaction transaction = session.beginTransaction();
Uinfo u = new Uinfo();
u.setUname("123");
u.setUpwd("123");
//保存对象到数据库表
session.save(u);
// 提交事务
transaction.commit();

session.close();
// 关闭sqlSessionFactory对象
sq.close()

修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 获取sqlSessionFactory对象
SessionFactory sq = HibernateUtil.getSessionFactory();
// 获取session对象
session = sq.opernSession();

// 开启事务
Transaction transaction = session.beginTransaction();
Uinfo u = new Uinfo();
u.setUname("123修改");
u.setUpwd("123修改");
//修改对象
session.update(u);
// 提交事务
transaction.commit();

session.close();
// 关闭sqlSessionFactory对象
sq.close()

删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 获取sqlSessionFactory对象
SessionFactory sq = HibernateUtil.getSessionFactory();
// 获取session对象
session = sq.opernSession();

// 开启事务
Transaction transaction = session.beginTransaction();
Uinfo u = new Uinfo();
u.setUid("1001");
//删除对象
session.delete(u);
// 提交事务
transaction.commit();

session.close();
// 关闭sqlSessionFactory对象
sq.close()

相关文章

数据库连接池

SpringIOC

Junit和Spring

Tomcat

Servlet

Request,Response和ServletContext

Cookie和Session

JSP和EL和Jstl

Filter和Listener

Mybatis