实验五Hibernate 基础应用——基于Hibernate 框架的用户登录模块

一、基础实验——Hibernate 框架搭建

实验内容及步骤

新建maven项目

1635126000453.png1635126062494.png

搭建项目框架

新建数据库

  1. create table customer
  2. (
  3. customerID int not null
  4. primary key,
  5. Account varchar(20) null,
  6. Password varchar(20) null,
  7. Name varchar(20) null,
  8. Sex tinyint null,
  9. Birthday date null,
  10. Phone varchar(20) null,
  11. Email varchar(100) null,
  12. Address varchar(200) null,
  13. Zipcode varchar(10) null,
  14. Fax varchar(20) null
  15. );
  16. INSERT INTO hibernatedb.customer (customerID, Account, Password, Name, Sex, Birthday, Phone, Email, Address, Zipcode, Fax) VALUES (1, 'zjut', 'Zjut', null, null, null, null, null, null, null, null);
  17. INSERT INTO hibernatedb.customer (customerID, Account, Password, Name, Sex, Birthday, Phone, Email, Address, Zipcode, Fax) VALUES (2, 'admin', 'Admin', null, null, null, null, null, null, null, null);
  18. INSERT INTO hibernatedb.customer (customerID, Account, Password, Name, Sex, Birthday, Phone, Email, Address, Zipcode, Fax) VALUES (3, 'temp', 'Temp', null, null, null, null, null, null, null, null);

新建目录

1635127197810.png1635127171703.png

在pom.xml中导入如下代码:

  1. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
  2. <dependency>
  3. <groupId>org.hibernate</groupId>
  4. <artifactId>hibernate-core</artifactId>
  5. <version>5.4.32.Final</version>
  6. </dependency>
  7. <!-- mysql驱动包-->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <version>8.0.26</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.struts</groupId>
  15. <artifactId>struts2-core</artifactId>
  16. <version>2.3.15</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>commons-logging</groupId>
  20. <artifactId>commons-logging</artifactId>
  21. <version>1.2</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. <version>RELEASE</version>
  27. <scope>compile</scope>
  28. </dependency>

下面搭建所有类,框架结构如图

1635135393107.png

新建Hibernate 相关文件

新建Customer.java

  1. package cn.edu.zjut.po;
  2. @Data
  3. public class Customer implements java.io.Serializable{
  4. private int customerId;
  5. private String account; private String password;
  6. private String name; private Boolean sex;
  7. private Date birthday; private String phone;
  8. private String email; private String address;
  9. private String zipcode; private String fax;
  10. }

新建Customer.hbm.xml,在resource目录下

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
  3. 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="cn.edu.zjut.po.Customer" table="customer"
  7. catalog="hibernatedb">
  8. <id name="customerId" type="int">
  9. <column name="customerID" />
  10. <generator class="assigned" />
  11. </id>
  12. <property name="account" type="string">
  13. <column name="account" length="20" unique="true" />
  14. </property>
  15. <property name="password" type="string">
  16. <column name="password" length="20" />
  17. </property>
  18. <property name="name" type="string">
  19. <column name="name" length="20" />
  20. </property>
  21. <property name="sex" type="java.lang.Boolean">
  22. <column name="sex" />
  23. </property>
  24. <property name="birthday" type="date">
  25. <column name="birthday" length="10" />
  26. </property>
  27. <property name="phone" type="string">
  28. <column name="phone" length="20" />
  29. </property>
  30. <property name="email" type="string">
  31. <column name="email" length="100" />
  32. </property>
  33. <property name="address" type="string">
  34. <column name="address" length="200" />
  35. </property>
  36. <property name="zipcode" type="string">
  37. <column name="zipcode" length="10" />
  38. </property>
  39. <property name="fax" type="string">
  40. <column name="fax" length="20" />
  41. </property>
  42. </class>
  43. </hibernate-mapping>

新建hibernate.cfg.xml,在resource目录下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory name="HibernateSessionFactory">
  7. <property name="hibernate.connection.driver_class">
  8. com.mysql.jdbc.Driver</property>
  9. <property name="hibernate.connection.url">
  10. jdbc:mysql://localhost:3306/hibernatedb</property>
  11. <property name="hibernate.connection.username">
  12. zhangkun</property>
  13. <property name="hibernate.connection.password">
  14. zhangkun
  15. </property>
  16. <property name="hibernate.dialect">
  17. org.hibernate.dialect.MySQLDialect</property>
  18. <mapping resource="/Customer.hbm.xml" />
  19. </session-factory>
  20. </hibernate-configuration>

注意中的resource值,为同级目录下的Customer.hbm.xml

新建Struts相关文件

在hibernate-prj1 中新建cn.edu.zjut.dao 包,并在其中创建数据库操作类CustomerDAO.java,具体代码如下:

  1. public class CustomerDAO {
  2. private Log log = LogFactory.getLog(CustomerDAO.class);
  3. public List findByHql(String hql) {
  4. log.debug("finding LoginUser instance by hql");
  5. SessionFactory sf= new Configuration()
  6. .configure().buildSessionFactory();
  7. Session session=sf.openSession();
  8. try {
  9. String queryString = hql;
  10. Query queryObject = session.createQuery(queryString);
  11. return queryObject.list();
  12. } catch (RuntimeException re) {
  13. log.error("find by hql failed", re);
  14. throw re;
  15. } finally{
  16. session.close();
  17. }
  18. }
  19. }

在hibernate-prj1 中新建cn.edu.zjut.service 包,并在其中创建UserService.java,用于实现登录逻辑,具体代码如下:

  1. public class UserService {
  2. public boolean login(Customer loginUser) {
  3. String account = loginUser.getAccount();
  4. String password = loginUser.getPassword();
  5. String hql = "from Customer as user where account='"
  6. +account+ "' and password='" + password +"'";
  7. CustomerDAO dao = new CustomerDAO();
  8. List list = dao.findByHql(hql);
  9. if(list.isEmpty()) {
  10. return false;
  11. } else {
  12. return true;
  13. }
  14. }
  15. }

在hibernate-prj1 中新建cn.edu.zjut.action 包,并在其中创建UserAction.java,定义login()方法用于调用登录逻辑,具体代码如下:

  1. @Data
  2. public class UserAction {
  3. private Customer loginUser;
  4. public String login() {
  5. UserService userServ = new UserService();
  6. if (userServ.login(loginUser)) {
  7. return "success";
  8. }
  9. return "fail";
  10. }
  11. }

在工程hibernate-prj1 的resource目录中创建struts.xml 文件,用于配置Action 并设置页面导航,代码片段如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <package name="strutsBean" extends="struts-default" namespace="/">
  7. <action name="login" class="cn.edu.zjut.action.UserAction">
  8. <result name="success">/loginSuccess.jsp</result>
  9. <result name="fail">/login.jsp</result>
  10. </action>
  11. </package>
  12. </struts>

编辑Web 应用的web.xml 文件,增加Struts2 核心Filter 的配置,代码片段如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  6. <display-name>Archetype Created Web Application</display-name>
  7. <welcome-file-list>
  8. <welcome-file>/login.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- 定义Struts2 的核心Filter -->
  11. <filter>
  12. <filter-name>struts2</filter-name>
  13. <filter-class>
  14. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  15. </filter-class>
  16. </filter>
  17. <!-- 让Struts2 的核心Filter 拦截所有请求-->
  18. <filter-mapping>
  19. <filter-name>struts2</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. </web-app>

配置视图页面

新建login.jsp页面

  1. <body>
  2. <form action="login" method="post">
  3. <s:textfield name="loginUser.account"/><br>
  4. <s:textfield name="loginUser.password"/><br>
  5. <s:submit name="submit" value="登录" method="login"/>
  6. </form>
  7. </body>

新建loginSuccess.jsp页面

  1. <body>
  2. 登录成功
  3. </body>

测试

配置Tomcat

启动服务器,输入账号密码如下

1635135449574.png1635135468992.png

登录成功

实验总结

  1. 运行结果截图;
  2. 观察工程hibernate-prj1 中的Hibernate 配置文件hibernate.cfg.xml,查找相关资料,总结配置文件中各元素及其属性的作用,并记录下来; | 属性 | 作用说明 | | —- | —- | | Hibernate.show_sql | 是否在运行时候sql语句输出到控制台,编码阶段便于测试的。(默认设置为true) | | Hibernate.format_sql | 输出在控制台sql语句是否进行排版,便于阅读。(默认设置为true) | | Hbm2ddl.auto | 可帮助由java代码生成数据库脚本,进而生成具体表结构。如:create/update/create-drop/validate | | Hibernate.default_schema | 默认数据库,如果设置了,则创建表的时候,所有表有前缀。 | | Hibernate.dialect | 配置hibernate方言,可针对特定的数据库优化。 |

  3. 观察工程hibernate-prj1 中的JAVA 持久化类Customer.java、Hibernate 映射文件Customer.hbm.xml,总结持久化类与数据库表的映射关系,以及映射文件中主要元素及其属性的作用,并记录下来;

  4. 根据实验过程,总结Action、Service 和DAO 之间的调用关系,思考实验一的扩展实验中的DAO 类与本实验中DAO 类的区别,并记录下来;
  5. 碰到的问题及解决方案或思考;

    1. Hibernate配置路径问题
      一开始是将hibernate.cfg.xml的
      路径resource默认配置为”cn/edu/zjut/po/Customer.hbm.xml”,Customer.hbm.xml文件放在Customer.java同级目录下.
      但是启动失败了,因为在target文件夹中的Customer.hbm.xml文件并没有复制进target/hibernate-prj1/WEB-INF/classes/cn/edu/zjut/po/,查看Customer.hbm.xml可知该文件读取Customer.java不一定需要配置在同级目录下,因为读取文件是依靠中的name=”cn.edu.zjut.po.Customer”,故为了方便将Customer.hbm.xml放在resource目录下,同时修改hibernate.cfg.xml的配置
      1. <mapping resource="/Customer.hbm.xml" />
      1. <hibernate-mapping>
      2. <class name="cn.edu.zjut.po.Customer" table="customer"
      3. catalog="hibernatedb">
      4. .....
  6. 实验收获及总结。

二、提高实验——持久化对象与Hibernate 映射文件

实验内容及步骤

创建新表

  1. create table item
  2. (
  3. ISBN varchar(20) not null
  4. primary key,
  5. title varchar(30) null,
  6. cost float null,
  7. description varchar(100) null,
  8. image blob null
  9. );
  10. INSERT INTO hibernatedb.item (ISBN, title, cost, description, image) VALUES ('978-7-121-12345-1', 'JAVAEE技术
  11. 实验指导教程', 19.95, 'WEB程序设计知识回顾、轻量级JAVAEE
  12. 应用框架、企业级EJB组件编程技术、
  13. JAVAEE综合应用开发', null);
  14. INSERT INTO hibernatedb.item (ISBN, title, cost, description, image) VALUES ('978-7-121-12345-2', 'JAVAEE技术', 29.95, 'Struts框架、Hibernate框架、Spring框架、
  15. 会话Bean、实体Bean、消息驱动Bean', null);

搭建Hibernate相关文件

在hibernate-prj1 的cn.edu.zjut.po 包中手动创建JAVA 持久化类Item.java,使其与item 数据表相映射,代码片段如下

  1. @Data
  2. public class Item {
  3. private String itemID;
  4. private String title;
  5. private String description;
  6. private float cost;
  7. private Blob image;
  8. }

在resource目录下中手动创建Hibernate 映射文件Item.hbm.xml,并参照Customer.hbm.xml(基础实验步骤8)填写Item.hbm.xml 中的内容;

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
  3. 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="cn.edu.zjut.po.Item" table="item"
  7. catalog="hibernatedb">
  8. <id name="itemID" type="string">
  9. <column name="ISBN" length="20" />
  10. <generator class="assigned"></generator>
  11. </id>
  12. <property name="title" type="string">
  13. <column name="title" length="30" />
  14. </property>
  15. <property name="description" type="string">
  16. <column name="description" length="100" />
  17. </property>
  18. <property name="cost" type="float">
  19. <column name="cost" />
  20. </property>
  21. <property name="image" type="java.sql.Blob">
  22. <column name="image" />
  23. </property>
  24. </class>
  25. </hibernate-mapping>

修改hibernate-prj1 的Hibernate 配置文件hibernate.cfg.xml,增加Item.hbm.xml映射文件声明,代码片段如下:

  1. <mapping resource="/Item.hbm.xml" />

搭建Struts相关文件

在hibernate-prj1 的cn.edu.zjut.dao 包中创建数据库操作类ItemDAO.java,具体代码如下:

  1. public class ItemDAO {
  2. private static final Log log = LogFactory.getLog(ItemDAO.class);
  3. public List findAll() {
  4. log.debug("finding all Item instances");
  5. SessionFactory sf= new Configuration()
  6. .configure().buildSessionFactory();
  7. Session session=sf.openSession();
  8. try {
  9. String queryString = "from Item";
  10. Query queryObject = session.createQuery(queryString);
  11. return queryObject.list();
  12. } catch (RuntimeException re) {
  13. log.error("find all failed", re);
  14. throw re;
  15. } finally{
  16. session.close();
  17. }
  18. }
  19. }

在hibernate-prj1 的cn.edu.zjut.service 包中创建ItemService.java,用于获取所有商品信息,具体代码如下:

  1. public class ItemService {
  2. private List items = new ArrayList();
  3. public List getAllItems() {
  4. ItemDAO dao = new ItemDAO();
  5. List items = dao.findAll();
  6. return items;
  7. }
  8. }

在hibernate-prj1 的cn.edu.zjut.action 包中创建ItemAction.java,并在其中定义getAllItems()方法用于调用“获取所有商品信息”逻辑,参照UserAction 类(基础实验步骤16)填写ItemAction 类的内容;

  1. import javax.servlet.http.HttpServletRequest;
  2. @Data
  3. public class ItemAction {
  4. private Item item;
  5. private HttpServletRequest req;
  6. public String getAllItems(){
  7. ItemService itemService=new ItemService();
  8. req = ServletActionContext.getRequest(); //req的获得必须在具体的方法中实现
  9. ServletContext context=req.getServletContext();
  10. req.getSession().setAttribute("items", itemService.getAllItems());
  11. return "allItems";
  12. }
  13. }

修改hibernate-prj1 的struts.xml 文件,增加ItemAction 的配置并设置页面导航;

  1. <action name="allItems" class="cn.edu.zjut.action.ItemAction" method="getAllItems">
  2. <result name="allItems">/itemList.jsp</result>
  3. </action>

新建视图页面

在hibernate-prj1 中新建itemList.jsp 页面,作为显示所有商品信息的视图,代码片段如下:

  1. <body>
  2. <center>商品列表</center>
  3. <table border=1>
  4. <tr>
  5. <th>编号</th>
  6. <th>书名</th>
  7. <th>说明</th>
  8. <th>单价</th>
  9. </tr>
  10. <s:iterator value="#session.items" >
  11. <tr>
  12. <td><s:property value="itemID"/></td>
  13. <td><s:property value="title"/></td>
  14. <td><s:property value="description"/></td>
  15. <td><s:property value="cost"/></td>
  16. </tr>
  17. </s:iterator>
  18. </table>
  19. </body>

修改hibernate-prj1 的loginSuccess.jsp 页面,在视图中增加超链接,用于查看所有商品信息,代码片段如下;

  1. <a href="./allItems">查看所有商品信息</a>

查看效果

1635174809827.png

设置复合主键

假设使用持久化类Item.java 中的itemID 属性和title 属性作为复合主键,则需要将这两个属性封装成一个类,代码片段如下:

  1. @Data
  2. public class ItemPK implements Serializable {
  3. private String itemID;
  4. private String title;
  5. }

修改Item.java,将ItemPK 主键作为其属性之一,代码片段如下:

  1. @Data
  2. public class Item implements java.io.Serializable{
  3. private ItemPK ipk;
  4. private String description;
  5. private float cost;
  6. private Blob image;
  7. }

修改Hibernate 映射文件Item.hbm.xml,将主键类中每个属性和表中的列对应,并指定复合主键的类型,代码片段如下:

  1. <hibernate-mapping>
  2. <class name="cn.edu.zjut.po.Item" table="item" >
  3. <composite-id name="ipk" class="cn.edu.zjut.po.ItemPK">
  4. <key-property name="itemID" column="ISBN"/>
  5. <key-property name="title" column="title"/>
  6. </composite-id>
  7. ……
  8. </class>
  9. </hibernate-mapping>

修改itemList.jsp 页面,使其页面中能正确显示书本的编号与书名;

  1. <body>
  2. <center>商品列表</center>
  3. <table border=1>
  4. <tr>
  5. <th>编号</th>
  6. <th>书名</th>
  7. <th>说明</th>
  8. <th>单价</th>
  9. </tr>
  10. <s:iterator value="#session.items" >
  11. <tr>
  12. <td><s:property value="ipk.itemID"/></td>
  13. <td><s:property value="ipk.title"/></td>
  14. <td><s:property value="description"/></td>
  15. <td><s:property value="cost"/></td>
  16. </tr>
  17. </s:iterator>
  18. </table>
  19. </body>

查看结果

1635174809827.png

实验总结

  1. 运行结果截图;
  2. 结合实验过程,查找相关资料,总结POJO 模式下持久化类的规范,并记

录下来;

标签 作用
标签主要用于指定持久化类和数据库表名
name 属性指定持久化类得全局路径
table 属性指定数据库表名
class标签 包含一个,和多个
id 用于持久化类的唯标识与数据库表的主键字段的映射,通过定义主键的生成策略
property 用于持久化类的其他属性和数据表中的非主键字段的映射:
name 持久化类属性名,以小写字母开头
column 数据库字段名
type 数据库字段类型
length 数据库字段定义的长度
not-null 布尔变量,指定字段是否为空
unique 布尔变量,指定字段是否唯一
lazy 布尔变量,指定是否延迟抓取
  1. 结合实验过程,查找相关资料,总结映射文件中主要元素(如class、id、

generator、property)及其属性的含义与作用,并记录下来;

  1. 结合实验过程,查找相关资料,总结设置复合主键的方法和步骤,并记录

下来;

  1. 查找相关资料,总结Hibernate 映射文件中主键各种生成策略的作用,并记

录下来;

策略 说明
assigned 主键由外部程序负责生成,在 save() 之前必须指定一个。Hibernate不负责维护主键生成。与Hibernate和底层数据库都无关,可以跨数据库。在存储对象前,必须要使用主键的setter方法给主键赋值,至于这个值怎么生成,完全由自己决定,这种方法应该尽量避免。
increment 由Hibernate从数据库中取出主键的最大值(每个session只取1次),以该值为基础,每次增量为1,在内存中生成主键,不依赖于底层的数据库,因此可以跨数据库。
hilo hilo(高低位方式high low)是hibernate中最常用的一种生成方式,需要一张额外的表保存hi的值。保存hi值的表至少有一条记录(只与第一条记录有关),否则会出现错误。可以跨数据库
seqhilo 与hilo类似,通过hi/lo算法实现的主键生成机制,只是将hilo中的数据表换成了序列sequence,需要数据库中先创建sequence,适用于支持sequence的数据库,如Oracle。
sequence 采用数据库提供的sequence机制生成主键,需要数据库支持sequence。如oralce、DB、SAP DB、PostgerSQL、McKoi中的sequence。MySQL这种不支持sequence的数据库则不行(可以使用identity)。
identity identity由底层数据库生成标识符。identity是由数据库自己生成的,但这个主键必须设置为自增长,使用identity的前提条件是底层数据库支持自动增长字段类型,如DB2、SQL Server、MySQL、Sybase和HypersonicSQL等,Oracle这类没有自增字段的则不支持。
native native由hibernate根据使用的数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式,灵活性很强。如果能支持identity则使用identity,如果支持sequence则使用sequence。
uuid Hibernate在保存对象时,生成一个UUID字符串作为主键,保证了唯一性,但其并无任何业务逻辑意义,只能作为主键,唯一缺点长度较大,32位(Hibernate将UUID中间的“-”删除了)的字符串,占用存储空间大,但是有两个很重要的优点,Hibernate在维护主键时,不用去数据库查询,从而提高效率,而且它是跨数据库的,以后切换数据库极其方便。
guid GUID:Globally Unique Identifier全球唯一标识符,也称作 UUID,是一个128位长的数字,用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID。从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义上)3240年不重复。Hibernate在维护主键时,先查询数据库,获得一个uuid字符串,该字符串就是主键值,该值唯一,缺点长度较大,支持数据库有限,优点同uuid,跨数据库,但是仍然需要访问数据库。
foreign 使用另外一个相关联的对象的主键作为该对象主键。主要用于一对一关系中。
select 使用触发器生成主键,主要用于早期的数据库主键生成机制,能用到的地方非常少
  1. 碰到的问题及解决方案或思考;
  2. 实验收获及总结。

三、扩展实验——粒度设计

实验内容及步骤

将用户的联系方式单独封装到类

在hibernatedb 数据库里的customer 数据表中,包括phone、email、address、zipcode、fax 在内的字段都是用户的联系方式,基于设计的粒度设计将用户的联系方式单独封装到类ContactInfo,代码片段如下:

  1. @Data
  2. public class ContactInfo {
  3. private String phone;
  4. private String email;
  5. private String address;
  6. private String zipcode;
  7. private String fax;
  8. }

粒度设计

修改Customer.java,将ContactInfo 实例作为Customer 的属性,用于表示用户的联系方式,代码片段如下:

  1. @Data
  2. public class Customer implements java.io.Serializable{
  3. private int customerId;
  4. private String account;
  5. private String password;
  6. private String name;
  7. private Boolean sex;
  8. private Date birthday;
  9. private ContactInfo contactInfo;
  10. }

修改hibernate配置文件

修改Hibernate 映射文件Customer.hbm.xml,将customer 表与两个类(Customer和ContactInfo)映射,具体代码如下:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
  3. 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="cn.edu.zjut.po.Customer" table="customer"
  7. catalog="hibernatedb">
  8. <id name="customerId" type="int">
  9. <column name="customerID" />
  10. <generator class="increment" />
  11. </id>
  12. <property name="account" type="string">
  13. <column name="account" length="20" unique="true" />
  14. </property>
  15. <property name="password" type="string">
  16. <column name="password" length="20" />
  17. </property>
  18. <property name="name" type="string">
  19. <column name="name" length="20" />
  20. </property>
  21. <property name="sex" type="java.lang.Boolean">
  22. <column name="sex" />
  23. </property>
  24. <property name="birthday" type="date">
  25. <column name="birthday" length="10" />
  26. </property>
  27. <component name="contactInfo" class="cn.edu.zjut.po.ContactInfo">
  28. <property name="phone" type="string">
  29. <column name="phone" length="20" />
  30. </property>
  31. <property name="email" type="string">
  32. <column name="email" length="100" />
  33. </property>
  34. <property name="address" type="string">
  35. <column name="address" length="200" />
  36. </property>
  37. <property name="zipcode" type="string">
  38. <column name="zipcode" length="10" />
  39. </property>
  40. <property name="fax" type="string">
  41. <column name="fax" length="20" />
  42. </property>
  43. </component>
  44. </class>
  45. </hibernate-mapping>

修改hibernate-prj1 的Hibernate 配置文件hibernate.cfg.xml,开启增删改操作的事务自动提交,代码片段如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory name="HibernateSessionFactory">
  7. ……
  8. <property name="connection.autocommit">true</property>
  9. </session-factory>
  10. </hibernate-configuration>

新建视图页面

在hibernate-prj1 中新建register.jsp 页面,作为用户注册的视图(可参考“实验二Struts 基础应用”中提高实验里的页面代码,并根据数据库表customer设计页面内容);新建regSuccess.jsp 页面,作为注册成功的视图(可重用“实验二Struts 基础应用”中提高实验里的页面代码);

  1. <%@ taglib prefix="s" uri="/struts-tags" %>
  2. <%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
  3. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  4. <html>
  5. <head>
  6. <title>Title</title>
  7. <sx:head/>
  8. </head>
  9. <body>
  10. <s:form action="register" method="post">
  11. <s:textfield name="loginUser.account" label="请输入用户名"/>
  12. <s:textfield name="loginUser.name" label="请输入用户名"/>
  13. <s:password name="loginUser.password" label="请输入密码"/>
  14. <%-- <s:password name="loginUser.repassword" label="请再次输入密码"/>--%>
  15. <s:radio name="loginUser.sex" list="#{1 : '男', 0 : '女'}" label
  16. ="请输入性别"/>
  17. <sx:datetimepicker name="loginUser.birthday" displayFormat
  18. ="yyyy-MM-dd" label="请输入生日"/>
  19. <s:textfield name="loginUser.contactInfo.phone" label="请输入电话"/>
  20. <s:textfield name="loginUser.contactInfo.email" label="请输入邮箱"/>
  21. <s:textfield name="loginUser.contactInfo.address" label="请输入联系地址"/>
  22. <s:textfield name="loginUser.contactInfo.zipcode" label="请输入邮政编码"/>
  23. <s:textfield name="loginUser.contactInfo.fax" label="请输入传真号码"/>
  24. <s:submit value="注册"/>
  25. <s:reset value="重置"/>
  26. </s:form>
  27. </body>
  28. </html>

其中,<%@ taglib prefix=”sx” uri=”/struts-dojo-tags” %>和需要用maven导入struts-dojo-tags包

  1. <dependency>
  2. <groupId>org.apache.struts</groupId>
  3. <artifactId>struts2-dojo-plugin</artifactId>
  4. <version>2.3.15</version>
  5. </dependency>

修改Struts配置

修改cn.edu.zjut.dao 包中的CustomerDAO.java,增加“添加新用户”的操作,代码片段如下:

  1. public void save(Customer customer) {
  2. log.debug("saving customer instance");
  3. SessionFactory sf= new Configuration().
  4. configure().buildSessionFactory();
  5. Session session=sf.openSession();
  6. try {
  7. Transaction transaction = session.beginTransaction();
  8. System.out.println(customer.toString());
  9. session.save(customer);
  10. // session.flush();
  11. transaction.commit();
  12. log.debug("save successful");
  13. } catch (RuntimeException re) {
  14. log.error("save failed", re);
  15. throw re;
  16. } finally{
  17. session.close();
  18. }
  19. }

修改cn.edu.zjut.service 包中的UserService.java,增加用户注册逻辑,代码片段如下

  1. public class UserService {
  2. // ...
  3. public void register(Customer loginUser) {
  4. CustomerDAO dao = new CustomerDAO();
  5. dao.save(loginUser);
  6. }
  7. // ...
  8. }

修改cn.edu.zjut.action 包中的UserAction.java,定义register()方法用于调用用户注册逻辑,代码片段如下:

  1. public class UserAction {
  2. // ……
  3. public String register() {
  4. UserService userServ = new UserService();
  5. userServ.register(loginUser);
  6. return "registersuccess";
  7. }
  8. }

修改工程hibernate-prj1 中的struts.xml 文件,为“用户注册”增加Action 配置并设置页面导航;

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <package name="strutsBean" extends="struts-default" namespace="/">
  7. <action name="login" class="cn.edu.zjut.action.UserAction">
  8. <result name="success">/loginSuccess.jsp</result>
  9. <result name="fail">/login.jsp</result>
  10. </action>
  11. <action name="allItems" class="cn.edu.zjut.action.ItemAction" method="getAllItems">
  12. <result name="allItems">/itemList.jsp</result>
  13. </action>
  14. <action name="register" class="cn.edu.zjut.action.UserAction" method="register">
  15. <result name="registersuccess">/regSuccess.jsp</result>
  16. </action>
  17. </package>
  18. </struts>

查看效果

image.pngimage.png
image.png

粒度设计2

在hibernatedb 数据库里的item 数据表中,image 字段表示商品的照片,使用Blob 类型,所占空间较大,如果该字段不常被使用,则基于性能的粒度设计将表item 映射为两个类,其中一个类为ItemDetail,映射表中所有的字段,而另一个类ItemBasic,映射表中除了image 之外的字段,具体代码如下:

  1. @Data
  2. public class ItemBasic {
  3. private String itemID;
  4. private String title;
  5. private String description;
  6. private float cost;
  7. }
  1. @Data
  2. public class ItemDetail extends ItemBasic {
  3. private Blob image;
  4. }

在ItemBasic.java 和ItemDetail.java 的同一目录下中手动创建Hibernate 映射文件ItemBasic.hbm.xml 和ItemDetail.hbm.xml,ItemDetail.hbm.xml 的具体代码如下:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
  3. 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="cn.edu.zjut.po.ItemDetail" table="item"
  7. catalog="hibernatedb">
  8. <id name="itemID" type="string">
  9. <column name="ISBN" length="20" />
  10. <generator class="assigned" />
  11. </id>
  12. <property name="title" type="string">
  13. <column name="title" length="30" />
  14. </property>
  15. <property name="description" type="string">
  16. <column name="description" length="100" />
  17. </property>
  18. <property name="cost" type="float">
  19. <column name="cost" />
  20. </property>
  21. </class>
  22. </hibernate-mapping>
  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
  3. 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="cn.edu.zjut.po.ItemDetail" table="item"
  7. catalog="hibernatedb" polymorphism="explicit">
  8. <id name="itemID" type="string">
  9. <column name="ISBN" length="20" />
  10. <generator class="assigned" />
  11. </id>
  12. <property name="title" type="string">
  13. <column name="title" length="30" />
  14. </property>
  15. <property name="description" type="string">
  16. <column name="description" length="100" />
  17. </property>
  18. <property name="cost" type="float">
  19. <column name="cost" />
  20. </property>
  21. <property name="image" type="java.sql.Blob">
  22. <column name="image" />
  23. </property>
  24. </class>
  25. </hibernate-mapping>

修改hibernate-prj1 的Hibernate 配置文件hibernate.cfg.xml,增加ItemDetail.hbm.xml 和ItemBasic.hbm.xml 映射文件声明,代码片段如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory name="HibernateSessionFactory">
  7. <property name="hibernate.connection.driver_class">
  8. com.mysql.jdbc.Driver</property>
  9. <property name="hibernate.connection.url">
  10. jdbc:mysql://localhost:3306/hibernatedb</property>
  11. <property name="hibernate.connection.username">
  12. zhangkun</property>
  13. <property name="hibernate.connection.password">
  14. zhangkun
  15. </property>
  16. <property name="hibernate.dialect">
  17. org.hibernate.dialect.MySQLDialect</property>
  18. <property name="connection.autocommit">true</property>
  19. <mapping resource="/Customer.hbm.xml" />
  20. <mapping resource="/ItemDetail.hbm.xml" />
  21. <mapping resource="/ItemBasic.hbm.xml" />
  22. </session-factory>
  23. </hibernate-configuration>

删除Item.hbm.xml映射文件声明
相应地修改ItemDAO 将hibernate-prj1 重新部署在Tomcat 服务器上;

  1. public class ItemDAO {
  2. private static final Log log = LogFactory.getLog(ItemDAO.class);
  3. public List findAll() {
  4. log.debug("finding all Item instances");
  5. SessionFactory sf= new Configuration()
  6. .configure().buildSessionFactory();
  7. Session session=sf.openSession();
  8. try {
  9. String queryString = "from ItemBasic ";
  10. Query queryObject = session.createQuery(queryString);
  11. System.out.println("queryObject.list()="+queryObject.list());
  12. return queryObject.list();
  13. } catch (RuntimeException re) {
  14. log.error("find all failed", re);
  15. throw re;
  16. } finally{
  17. session.close();
  18. }
  19. }
  20. }

修改ItemList.jsp

  1. <body>
  2. <center>商品列表</center>
  3. <table border=1>
  4. <tr>
  5. <th>编号</th>
  6. <th>书名</th>
  7. <th>说明</th>
  8. <th>单价</th>
  9. </tr>
  10. <s:iterator value="#session.items" >
  11. <tr>
  12. <td><s:property value="itemID"/></td>
  13. <td><s:property value="title"/></td>
  14. <td><s:property value="description"/></td>
  15. <td><s:property value="cost"/></td>
  16. </tr>
  17. </s:iterator>
  18. </table>
  19. </body>

查看效果
image.png

实验总结

  1. 运行结果截图;
  2. 结合实验过程,总结两种粒度设计的方法及特点;
  3. 根据实验步骤4,查找相关资料,写出Hibernate 配置文件hibernate.cfg.xml中的“connection.autocommit”属性的作用;
  • hibernate中hibernate.connection.autocommit属性用来设置获取到的jdbc connection的autoCommit属性,默认是false。
  • session.connection().getAutoCommit()方法可以获取session对应的connection的autoCommit属性
  • hibernate.connection.autocommit 允许被缓存的 JDBC 连接开启自动提交。
  1. 碰到的问题及解决方案或思考;

    1. 最后一步继承问题,hql语句查询ItemBasic时会查询出重复数据,问题主要是多态查询,详细链接如下
      1. 问题如图所示%P6SUNV5A@GQFO6RV@Y7633.png
      2. 解决方案参考链接https://blog.csdn.net/pursuer211/article/details/17318379
      3. 解决方案是 子类映射文件class加polymorphism=”explicit”属性
        1. <class name="cn.edu.zjut.po.ItemDetail" table="item"
        2. catalog="hibernatedb" polymorphism="explicit">
        3. ......
  2. 实验收获及总结。