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;
}
}
<hibernate-mapping>
<class name="com.ming.ssh.domain.Customer" table="customer">
<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>
package com.ming.ssh.dao;
import java.util.List;
import com.ming.ssh.domain.Customer;
/**
* 定义持久层接口
* @author Mr.F
*
*/
public interface CustomerDao {
List<Customer> findAllCustomer(); //查询所有客户信息
}
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;
}
}
package com.ming.ssh.service;
import java.util.List;
import com.ming.ssh.domain.Customer;
/**
* 定义业务层接口
* @author Mr.F
*
*/
public interface CustomerService {
List<Customer> findAllCustomer(); //查询所有客户信息
}
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();
}
}
<context:component-scan base-package="com.ming.ssh">context:component-scan>
<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>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.format_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>
<property name="mappingResources">
<list>
<value>com/ming/ssh/domain/Customer.hbm.xmlvalue>
list>
property>
bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager" />
beans>
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/customer_manager?serverTimezone=UTC
jdbc.username=root
jdbc.password=1314
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
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>
<constant name="struts.action.extension" value="action">constant>
<package name="ssh_customer_new" extends="struts-default" namespace="/">
<action name="customer_*" method="{1}" class="customerAction">
<result name="findAllSuccess">/WEB-INF/jsp/index.jspresult>
<allowed-methods>findAllallowed-methods>
action>
package>
struts>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<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>客户IDtd>
<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-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_newdisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
将ssh_customer_new项目工程发布到本地tomcat服务器上,在浏览器地址栏访问http://localhost:8080/ssh_customer_new/customer_findAll.action,其结果如下: