Struts和Hibernate的简单示例

Easter79
• 阅读 655

最近给一个非常好学却找不到方向的朋友讲struts和hibernate框架的基础入门,突然发现自己对这两个框架有些生疏了。这一年来的工作中都没有使用过struts和hibernate做开发,所以在给他讲解的同时也是自己复习和加深印象的过程,有些技术细节确实需要用心记忆。
今天去面试,收获了3点感想:

  1. 在学习技术的过程中,博学是好事,但专精更加重要。
  2. 不要过于依赖搜索引擎和API手册,这样会使自己对任何事情都拿不准主意,总是模棱两可,而且会养成惰性,养成不愿意记忆的坏毛病。
  3. 加强对技术细节的掌握,只有对技术细节有着精准的掌握之后再在此基础上进行更高层次的扩展和延伸,才能成为一个优秀的架构师。

下面把给朋友讲的struts和hibernate框架的基本使用的代码贴上来,主要是贴代码,需要注意的地方会另外文字说明。
参考了北大青鸟的教材《开发基于Struts/Spring/Hibernate/Ajax的网上信息发布平台》一书中的示例项目,在此我做了精简。
主要功能就是会员登陆、注册,还对有房屋信息的CRUD操作。

需要用的lib有这些:

antlr-2.7.6.jar
c3p0-0.9.1.jar
commons-beanutils.jar
commons-collections-3.1.jar
commons-digester.jar
commons-fileupload.jar
commons-logging.jar
commons-validator.jar
dom4j-1.6.1.jar
hibernate3.jar
jakarta-oro.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j-1.2.15.jar
mysql-connector-java-5.0.4-bin.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.6.jar
struts.jar

数据库SQL脚本(MySQL):

CREATE DATABASE IF NOT EXISTS house DEFAULT CHARACTER SET = 'utf8';
USE house;

CREATE TABLE IF NOT EXISTS member
(
    id INT UNSIGNED AUTO_INCREMENT NOT NULL,
    login_id VARCHAR(32) NOT NULL,
    login_pwd VARCHAR(32) NOT NULL,
    PRIMARY KEY (id)
) ENGINE = 'InnoDB' CHARACTER SET 'utf8';

CREATE TABLE IF NOT EXISTS house_type
(
    id INT UNSIGNED AUTO_INCREMENT NOT NULL,
    type_name VARCHAR(32) NOT NULL,
    PRIMARY KEY (id)
) ENGINE = 'InnoDB' CHARACTER SET = 'utf8';

CREATE TABLE IF NOT EXISTS house_info
(
    id INT UNSIGNED AUTO_INCREMENT NOT NULL,
    member_id INT UNSIGNED NOT NULL,
    type_id INT UNSIGNED NOT NULL,
    living_room INT UNSIGNED NOT NULL,
    bedroom INT UNSIGNED NOT NULL,
    information VARCHAR(500) NOT NULL,
    rent DECIMAL(10, 4) NOT NULL,
    title VARCHAR(200) NOT NULL,
    post_date DATETIME NOT NULL,
    telephone VARCHAR(11) NOT NULL,
    real_name VARCHAR(20) NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (member_id) REFERENCES member(id),
    FOREIGN KEY (type_id) REFERENCES house_type(id)
) ENGINE = 'InnoDB' CHARACTER SET 'utf8';

Hibernate.cfg.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">
        <![CDATA[
            jdbc:mysql://127.0.0.1/house?useUnicode=true&characterEncoding=utf-8
        ]]>
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="c3p0.min_size">5</property>
        <property name="c3p0.max_size">20</property>
        <property name="c3p0.timeout">300</property>
        <property name="c3p0.max_statements">50</property>
        <property name="c3p0.idle_test_period">3000</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>

        <mapping resource="com/house/entity/HouseInfo.hbm.xml" />
        <mapping resource="com/house/entity/HouseType.hbm.xml" />
        <mapping resource="com/house/entity/Member.hbm.xml" />
    </session-factory>
</hibernate-configuration>

Hibernate的SessionFactory工具类,代码来自《Hibernate实战 第2版 (Java Persistence with Hibernate)》:

package com.house.util;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

下面是HouseInfo.hbm.xml映射文件的代码,其它的映射文件省略:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.house.entity">
    <class name="HouseInfo" table="house_info">
        <id name="id" column="id" type="integer">
            <generator class="identity" />
        </id>
        <property name="livingRoom" column="living_room" not-null="true"
            type="integer" />
        <property name="bedroom" column="bedroom" not-null="true"
            type="integer" />
        <property name="information" column="information" not-null="true"
            type="string" />
        <property name="rent" column="rent" not-null="true" type="big_decimal" />
        <property name="title" column="title" not-null="true" type="string" />
        <property name="postDate" column="post_date" not-null="true"
            type="date" />
        <property name="telephone" column="telephone" not-null="true"
            type="string" />
        <property name="realName" column="real_name" not-null="true"
            type="string" />

        <many-to-one name="member" column="member_id" not-null="true"
            class="Member" lazy="false" />
        <many-to-one name="houseType" column="type_id" not-null="true"
            class="HouseType" lazy="false" />
    </class>
</hibernate-mapping>

InitUpdateAction代码,这步是更新初始化的操作。
我们需要先进行查询将值放入其对应的form中,然后JSP页面中就可以使用struts标签来显示form中的值。

package com.house.action.house;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.house.dao.HouseInfoDao;
import com.house.dao.HouseTypeDao;
import com.house.entity.HouseInfo;
import com.house.entity.HouseType;
import com.house.form.UpdateHouseForm;

public class InitUpdateAction extends Action {

    private HouseInfoDao houseInfoDao;
    private HouseTypeDao houseTypeDao;

    public InitUpdateAction() {
        houseInfoDao = new HouseInfoDao();
        houseTypeDao = new HouseTypeDao();
    }

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Integer id = Integer.valueOf(request.getParameter("id"));
        // initialize houseInfo
        HouseInfo houseInfo = houseInfoDao.searchHouseInfo(id);
        UpdateHouseForm updateHouseForm = (UpdateHouseForm) form;
        // initialize houseInfo form
        updateHouseForm.setId(id);
        updateHouseForm.setMember(houseInfo.getMember());
        updateHouseForm.setHouseType(houseInfo.getHouseType());
        updateHouseForm.setLivingRoom(houseInfo.getLivingRoom());
        updateHouseForm.setBedroom(houseInfo.getBedroom());
        updateHouseForm.setInformation(houseInfo.getInformation());
        updateHouseForm.setRent(houseInfo.getRent());
        updateHouseForm.setTitle(houseInfo.getTitle());
        updateHouseForm.setTelephone(houseInfo.getTelephone());
        updateHouseForm.setRealName(houseInfo.getRealName());
        // initialize houseType
        List<HouseType> houseTypeList = houseTypeDao.searchAllHouseType();
        request.setAttribute("houseTypeList", houseTypeList);
        return mapping.findForward("update");
    }

}

上面的Action所对应的UpdateHouseForm代码如下:

package com.house.form;

import java.math.BigDecimal;
import java.util.Date;
import org.apache.struts.action.ActionForm;
import com.house.entity.HouseType;
import com.house.entity.Member;

public class UpdateHouseForm extends ActionForm {

    private static final long serialVersionUID = 1L;

    private Integer id;
    private Member member = new Member();
    private HouseType houseType = new HouseType();
    private Integer livingRoom;
    private Integer bedroom;
    private String information;
    private BigDecimal rent;
    private String title;
    private Date postDate;
    private String telephone;
    private String realName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public HouseType getHouseType() {
        return houseType;
    }

    public void setHouseType(HouseType houseType) {
        this.houseType = houseType;
    }

    public Integer getLivingRoom() {
        return livingRoom;
    }

    public void setLivingRoom(Integer livingRoom) {
        this.livingRoom = livingRoom;
    }

    public Integer getBedroom() {
        return bedroom;
    }

    public void setBedroom(Integer bedroom) {
        this.bedroom = bedroom;
    }

    public String getInformation() {
        return information;
    }

    public void setInformation(String information) {
        this.information = information;
    }

    public BigDecimal getRent() {
        return rent;
    }

    public void setRent(BigDecimal rent) {
        this.rent = rent;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getPostDate() {
        return postDate;
    }

    public void setPostDate(Date postDate) {
        this.postDate = postDate;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

}

注意上面的UpdateHouseForm,里面的包含有其它类型的对象,但在这个地方被初始化了。其目的是因为在更新操作的form提交时,若表单中包含有类似以member.loginId的元素,则其对应的form中的member必须是被实例化,否则会引发异常。

下面是Struts标签库中的html标签的基本实用,这里实现了表单元素的value值的初始化。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Expires" content="0" />
<link type="text/css" href="https://my.oschina.net/house/style/style.css" rel="stylesheet" />
<script type="text/javascript">
function validateForm() {
    var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].value == "") {
            alert("请将表单信息补充完整!");
            return false;
        }
    }
    return true;
}
</script>
<title>编辑房屋信息</title>
</head>

<body>
<h1>欢迎使用房屋信息管理系统</h1>
<html:form action="/updateHouseInfo" method="post"
    onsubmit="return validateForm();">
    <table border="0" cellspacing="1" cellpadding="0" class="detail">
        <tr>
            <th colspan="2"><h2>编辑房屋信息</h2></th>
        </tr>
        <tr>
            <th>编号</th>
            <td><html:text property="id" readonly="true" /></td>
        </tr>
        <tr>
            <th>发布者</th>
            <td>
                <html:text property="member.loginId" readonly="true" />
                <html:hidden property="member.id" />
            </td>
        </tr>
        <tr>
            <th>类型</th>
            <td>
            <html:select property="houseType.id">
                <html:optionsCollection name="houseTypeList"
                    label="typeName" value="id" />
            </html:select>
            </td>
        </tr>
        <tr>
            <th>厅</th>
            <td><html:text property="livingRoom" /></td>
        </tr>
        <tr>
            <th>室</th>
            <td><html:text property="bedroom" /></td>
        </tr>
        <tr>
            <th>描述</th>
            <td><html:text property="information" /></td>
        </tr>
        <tr>
            <th>租金</th>
            <td><html:text property="rent" /></td>
        </tr>
        <tr>
            <th>标题</th>
            <td><html:text property="title" /></td>
        </tr>
        <tr>
            <th>电话</th>
            <td><html:text property="telephone" /></td>
        </tr>
        <tr>
            <th>联系人</th>
            <td><html:text property="realName" /></td>
        </tr>
        <tr>
            <th colspan="2" style="text-align: right;">
                <input type="submit" value="更新" />
                <input type="button" value="返回" />
            </th>
        </tr>
    </table>
</html:form>
</body>
</html>

页面效果如下图(表单元素都已经有默认值):

Struts和Hibernate的简单示例

内容很基础,不啰嗦了,明天还要上班。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
2个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
8个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k