您的位置:首页 > 其它

SSH框架整合详解

2019-01-20 18:02 1816 查看
版权声明:本文为博主原创文章,未经博主允许不得转载,博客地址:https://blog.csdn.net/Mr_FLM 。 https://blog.csdn.net/Mr_FLM/article/details/86564205

开发环境

  • 开发工具:Eclipse
  • JDK版本:9.0.4
  • MySQL版本:8.0.12
  • 服务器:Apache-Tomcat-9.0.12
  • SSH版本:Spring-5.0.5.RELEASE + Struts-2.5.18 + Hibernate-5.2.12.Final

1、创建动态web工程

 (1)工程目录结构

  (2)导入所需jar包

2、持久层实现

customer_manager数据库中customer表的构建:

Customer:

package com.ming.ssh.domain;
import java.io.Serializable;
/**
* 定义实体类
* @author Mr.F
*
*/
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
private Long c_id;
private String c_name;
private String c_password;
private String c_address;
private String c_phone;
private String c_email;
public Long getC_id() {
return c_id;
}
public void setC_id(Long c_id) {
this.c_id = c_id;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public String getC_password() {
return c_password;
}
public void setC_password(String c_password) {
this.c_password = c_password;
}
public String getC_address() {
return c_address;
}
public void setC_address(String c_address) {
this.c_address = c_address;
}
public String getC_phone() {
return c_phone;
}
public void setC_phone(String c_phone) {
this.c_phone = c_phone;
}
public String getC_email() {
return c_email;
}
public void setC_email(String c_email) {
this.c_email = c_email;
}
}

Customer.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">
<!--
定义实体类与数据库中表的映射
@author Mr.F
-->
<hibernate-mapping>
<class name="com.ming.ssh.domain.Customer" table="customer">
<!-- 当实体类的属性名与表中的字段名相同时,可省略column -->
<id name="c_id">
<generator class="native"></generator>
</id>
<property name="c_name"></property>
<property name="c_password"></property>
<property name="c_address"></property>
<property name="c_phone"></property>
<property name="c_email"></property>
</class>
</hibernate-mapping>

CustomerDao:

package com.ming.ssh.dao;
import java.util.List;
import com.ming.ssh.domain.Customer;
/**
* 定义持久层接口
* @author Mr.F
*
*/
public interface CustomerDao {
List<Customer> findAllCustomer(); //查询所有客户信息
}

CustomerDaoImpl:

package com.ming.ssh.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.ming.ssh.dao.CustomerDao;
import com.ming.ssh.domain.Customer;
/**
* 实现持久层接口
* @author Mr.F
*
*/
@Repository("customerDao")
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao{
@Resource(name="sessionFactory")
public void setSessionFactoryOverride(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
@SuppressWarnings("unchecked")
@Override
public List<Customer> findAllCustomer() {
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
List<Customer> list = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria);
return list;
}
}

3、业务层实现

CustomerService:

package com.ming.ssh.service;
import java.util.List;
import com.ming.ssh.domain.Customer;
/**
* 定义业务层接口
* @author Mr.F
*
*/
public interface CustomerService {
List<Customer> findAllCustomer(); //查询所有客户信息
}

CustomerServiceImpl:

package com.ming.ssh.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.ming.ssh.dao.CustomerDao;
import com.ming.ssh.domain.Customer;
import com.ming.ssh.service.CustomerService;
/**
* 实现业务层接口
* @author Mr.F
*
*/
@Service("customerService")
public class CustomerServiceImpl implements CustomerService{
@Resource(name="customerDao")
private CustomerDao customerDao;
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
@Override
public List<Customer> findAllCustomer() {
return customerDao.findAllCustomer();
}
}

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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=
"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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描包下的注解 -->
<context:component-scan base-package="com.ming.ssh"></context:component-scan>
<!-- 加载MySQL数据库属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注册数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库属性 -->
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 连接池属性 -->
<property name="initialPoolSize" value="3"></property>
<property name="minPoolSize" value="3"></property>
<property name="maxPoolSize" value="5"></property>
<property name="acquireIncrement" value="3"></property>
<property name="maxStatements" value="8"></property>
<property name="maxStatementsPerConnection" value="5"></property>
<property name="maxIdleTime" value="1800"></property>
</bean>
<!-- 注册Session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 装配数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 装配Hibernate属性  -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--  装配实体类映射文件 -->
<property name="mappingResources">
<list>
<value>com/ming/ssh/domain/Customer.hbm.xml</value>
</list>
</property>
</bean>
<!-- 注册Hibernate事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

jdbc.properties:

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/customer_manager?serverTimezone=UTC
jdbc.username=root
jdbc.password=1314

log4j.properties:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger= info, stdout

4、控制层实现

CustomerAction:

package com.ming.ssh.web;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.ming.ssh.domain.Customer;
import com.ming.ssh.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 定义控制层实现类
* @author Mr.F
*
*/
@Controller("customerAction")
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
private static final long serialVersionUID = 1L;
private Customer customer = new Customer();
@Override
public Customer getModel() {
return this.customer;
}
@Resource(name="customerService")
private CustomerService customerService;
public String findAll() {
List<Customer> list = customerService.findAllCustomer();
ServletActionContext.getRequest().setAttribute("customerList", list);
return "findAllSuccess";
}
}

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 设置操作方法后缀名 -->
<constant name="struts.action.extension" value="action"></constant>
<!-- 配置处理用户请求的Action和逻辑视图 -->
<package name="ssh_customer_new" extends="struts-default" namespace="/">
<action name="customer_*" method="{1}" class="customerAction">
<result name="findAllSuccess">/WEB-INF/jsp/index.jsp</result>
<allowed-methods>findAll</allowed-methods>
</action>
</package>
</struts>

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>客户管理系统</title>
</head>
<body>
<h2 align="center">客户管理系统</h2>
<table border="1px" align="center" width="600px">
<tr align="center">
<td>客户ID</td>
<td>客户姓名</td>
<td>客户密码</td>
<td>客户地址</td>
<td>客户手机</td>
<td>客户邮箱</td>
</tr>
<s:iterator value="#request.customerList" >
<tr align="center">
<td><s:property value="c_id"/></td>
<td><s:property value="c_name"/></td>
<td><s:property value="c_password"/></td>
<td><s:property value="c_address"/></td>
<td><s:property value="c_phone"/></td>
<td><s:property value="c_email"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssh_customer_new</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置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>
</web-app>

5、工程测试

  将ssh_customer_new项目工程发布到本地tomcat服务器上,在浏览器地址栏访问http://localhost:8080/ssh_customer_new/customer_findAll.action,其结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: