参考文档1:https://www.cnblogs.com/yzoro/p/5847015.html
参考文档2:https://blog.csdn.net/pazezhe314/article/details/79691741?utm_source=blogxgwz7
一、jar包的下载和选择
第一步:下载jar包
(1)spring框架的jar包:https://repo.spring.io/
(2)hibernate框架的jar包:http://hibernate.org/orm/
(3)struts2框架的jar包:https://struts.apache.org/
(4)数据库(以mysql为例)连接的jar包:https://dev.mysql.com/downloads/connector/j/
第二步:选择三个框架的基础jar包文件,准备放入搭建的项目里
本项目下载的SSH框架的jar包版本,分别为:hibernate-release-5.4.0.Final(hibernate)、mysql-connector-java-8.0.13(mysql连接)、spring-framework-5.1.3.RELEASE(spring)、struts-2.5.18(struts2)
(1)struts的基础jar包:
(2)spring的jar包(应为不多,所以暂时全部包括):
(3)hibernate的基础jar包:
(4)mysql连接的jar包:
二、项目搭建
项目搭建工具:eclipse、navicat premium
项目开发环境:①Windows10-64位 ②Tomcat 9.0 ③jdk1.8.0_91 ④MySql 8.0.11
第一步:新建项目,导入jar包,并配置web.xml
(1)新建一个Dynamic Web Project,可命名为ssh_01,直接点击finish按钮
(2)将选择的jar包导入到lib(/WebContent/WEB-INF/lib)中
(3)将导入的jar包添加到项目Libraries中,项目右键==》Properties==》Java Build Path==》Libraries==》选中导入的所有jar包添加到进Libraries中
(4)在/WebContent/WEB-INF/下新建一个web.xml,配置过滤器和监听器
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ssh</display-name>
<welcome-file-list>
<welcome-file>index.action</welcome-file>
</welcome-file-list>
<!-- struts2的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring的监听器配置开始 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
第二步 创建用于分层的四个基础包,并分别添加BookCard的操作类
(1)项目切换到Project Explorer窗口,在/Java Resources/src/目录下创建四个包(package)
(2)根据数据库表的字段编写BookCard(实体类)和BookCard.hbm.xml(映射文件)放到ssh_01.entity包里
BookCard.java:
package ssh_01.entity;
import java.math.BigDecimal;
import java.util.Date;
public class BookCard {
private int cid ;
private String name;
private String sex ;
private Date cardDate;
private BigDecimal deposit;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getCardDate() {
return cardDate;
}
public void setCardDate(Date cardDate) {
this.cardDate = cardDate;
}
public BigDecimal getDeposit() {
return deposit;
}
public void setDeposit(BigDecimal deposit) {
this.deposit = deposit;
}
}
BookCard.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>
<class name="ssh_01.entity.BookCard" table="BookCard">
<!-- 卡号 -->
<id name="cid" column="cid">
<generator class="native"></generator>
</id>
<!-- 姓名 -->
<property name="name" column="name"></property>
<!-- 性别 -->
<property name="sex" column="sex"></property>
<!-- 办卡日期 -->
<property name="cardDate" column="cardDate"></property>
<!-- 押金 -->
<property name="deposit" column="deposit"></property>
</class>
</hibernate-mapping>
(3)在ssh_01.dao包里编写BookCardDao(接口类)和BookCardDaoImpl(实现类)。
BookCardDao.java:
package ssh_01.dao;
import java.util.List;
import ssh_01.entity.BookCard;
public interface BookCardDao {
public List<BookCard> getAllBookCard();
}
BookCardDaoImpl.java:
package ssh_01.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import ssh_01.entity.BookCard;
public class BookCardDaoImpl implements BookCardDao {
//在SSH的设计理念:要使用某个实例,那么就定义声明一个对象,然后
//给它添加set方法(用于spring注入进来)
//实现不要关注这个实例来自于那里,以及怎么创建,或者它是谁
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List<BookCard> getAllBookCard() {
//sessionFactory这个实例可以自己按常规的hibernate传统写法创建
//也可以交给spring去托管
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
//获取session
Session session = sessionFactory.openSession();
//后面当使用JPA的时候,EntityManager 类似于 Session
Query<BookCard> query = session.createQuery("from BookCard");
//将所有的数据查询出来并放到List集合里
List<BookCard> list = query.getResultList();
//将集合遍历循环
for(BookCard bookCard:list){
//打印输出到控制台
System.out.println(bookCard);
}
//关闭session
session.close();
//关闭sessionFactory
sessionFactory.close();
//返回list集合
return list;
}
}
(4)在ssh_01.service包里编写BookCardService(接口类)和BookCardServiceImpl(实现类)。
BookCardService.java:
package ssh_01.service;
import java.util.List;
import ssh_01.entity.BookCard;
public interface BookCardService {
public List<BookCard> getAllBookCard();
}
BookCardServiceImpl.java:
package ssh_01.service;
import java.util.List;
import ssh_01.dao.BookCardDao;
import ssh_01.entity.BookCard;
public class BookCardServiceImpl implements BookCardService{
//dao实例使用注入方式
private BookCardDao id;
//用于注入使用
public void setId(BookCardDao id) {
this.id = id;
}
@Override
public List<BookCard> getAllBookCard() {
//本类应该编写业务逻辑的代码,
//但本例没有业务逻辑,就不用写。
//访问数据库的代码,不会出现在service这一层
//交给dao来操作数据库
List<BookCard> myBookCardList = id.getAllBookCard();
//进行其它的业务逻辑操作,比如增加多一个选项,是否过期
//本例不需要
//....
return myBookCardList;
}
}
(5)在ssh_01.action包里编写IndexAction(action类)。
IndexAction.java:
package ssh_01.action;
import java.text.DecimalFormat;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import ssh_01.entity.BookCard;
import ssh_01.service.BookCardService;
//创建IndexAction(action类)继承ActionSupport接口
public class IndexAction extends ActionSupport {
private static final long serialVersionUID = 1L;
//声明service,但不给它创建具体的实现类的实例,
private BookCardService is = null;
//添加set()方法
public void setIs(BookCardService is) {
this.is = is;
}
//编写execute()方法
public String execute() {
//获取IndexService实例,调用getAllBookCard()方法
//将结果保存到List集合里
List<BookCard> myBookCardList = is.getAllBookCard();
//将查询出来的结构集打印到控制台
System.out.println("结果集:"+myBookCardList.size());
//获取Context上下文对象
ActionContext ac = ActionContext.getContext();
//将myBookCardList集合添加到上下文对象里
ac.put("myBookCardList", myBookCardList);
//返回一个字符串
return "success";
}
//金额格式转换
public String formatDouble(double s){
DecimalFormat fmat=new DecimalFormat("\u00A4##.0");
return fmat.format(s);
}
}
第三步 编写struts.xml(struts配置文件)、applicationContext.xml(spring配置文件)、hibernate.cfg.xml(hibernate配置文件)
(1)在/src/目录下新建配置文件struts.xml、applicationContext.xml、hibernate.cfg.xml
(2)编写各配置文件
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
<struts>
<!-- 告知Struts2运行时使用Spring来创建对象 -->
<constant name="struts.objectFactory" value="spring" />
<package name="default" namespace="/" extends="struts-default">
<action name="Index" class="myIndexAction">
<result name="success">/WEB-INF/jsp/index.jsp</result>
</action>
</package>
</struts>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由spring去管理 -->
<bean id="myIndexAction" class="ssh_01.action.IndexAction" scope="prototype">
<!-- setIs(myIndexService) -->
<property name="is" ref="myIndexService"/>
</bean>
<!-- myIndexService = new ssh_01.service.IndexServiceImpl() -->
<bean id="myIndexService" class="ssh_01.service.BookCardServiceImpl" scope="prototype">
<property name="id" ref="myIndexDao"/>
</bean>
<bean id="myIndexDao" class="ssh_01.dao.BookCardDaoImpl" scope="prototype">
<!-- 把sessionFactory 注入给IndexDao -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 添加sessionFactory bane ,注意,该类是Spring提供的 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" scope="prototype">
<!-- 注入Hibernate 配置文件路径,前面要加上 classpath:-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
</beans>
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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/ssh_01</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- dialect翻译为方言 Hibernate根据你选择的“方言”,针对每种数据库,作调整,如生成不同的SQL语句等 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 设置默认的数据库连接池 -->
<property name="connection.pool_size">5</property>
<!-- 显示SQL -->
<property name="show_sql">true</property>
<!-- 格式化SQL -->
<property name="format_sql">true</property>
<!-- 根据schema更新数据表的工具 -->
<property name="hbm2ddl.auto">update</property>
<!-- 数据表映射配置文件 -->
<mapping resource="ssh_01/entity/BookCard.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第四步 显示结果
(1)在/WebContent/WEB-INF/目录下创建文件夹jsp,再在/jsp/目录下创建一个index.jsp页面
(2)将所有数据取出来显示到index.jsp页面上
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<td>卡号</td>
<td>姓名</td>
<td>性别</td>
<td>办卡日期</td>
<td>押金</td>
</tr>
<!-- 使用struts2标签库中的iterator将所有数据遍历循环显示出来 -->
<s:iterator value="#myBookCardList" status="bcs">
<tr>
<td><s:property value="cid"></s:property></td>
<td><s:property value="name"></s:property></td>
<td><s:property value="sex"></s:property></td>
<td><s:date name="cardDate" format="yyyy年MM月dd日"></s:date></td>
<td><s:property value="%{formatDouble(deposit)}"></s:property></td>
</tr>
</s:iterator>
<!-- 判断查询出来等于0,就显示“没有查找到数据” -->
<s:if test="myBookCardList.size()==0">
<tr>
<td colspan="7">没有查找到数据</td>
</tr>
</s:if>
</table>
</body>
</html>