欢迎访问个人网站:打开链接
项目下载链接:打开
本项目是基于最新的Java开发框架使用半注解来开发的SSH工程项目。
1. SSH搭建环境
jdk1.8 + tomcat8.5 + eclipse oxygen
Spring5.0.3 + Hibernate5.0.7 + Struts2.5.13
2. 工程总体框架结构
3. 所需jar包
4. 配置文件
(1)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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 也可以用阿里的德鲁伊 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql:///ssh" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- 使用c3p0连接池 -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///ssh"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<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="packagesToScan">
<list>
<value>com.jinglisen.domain</value>
</list>
</property>
</bean>
<!-- 开启Spring的IOC注解功能 -->
<context:component-scan base-package="com.jinglisen"/>
<!-- Spring的事务管理配置 -->
<!-- 1.事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 2.开启Spring的事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
(2) 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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SSH</display-name>
<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的web监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--ContextLoaderListener监听器默认加载WEB-INF下的applicationContext.xml -->
<!-- 更新ContextLoaderListener读取文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>save.jsp</welcome-file>
</welcome-file-list>
</web-app>
5. 面向接口编程三层架构文件
- (1)CustomerDao
package com.jinglisen.dao;
import com.jinglisen.domain.Customer;
public interface CustomerDao {
public void save(Customer customer);
}
- (2)CustomerDaoImpl
package com.jinglisen.dao.impl;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.jinglisen.dao.CustomerDao;
import com.jinglisen.domain.Customer;
/**
* HibernateDaoSupport的写法
* @author JasonJing
*
*/
@Repository
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
//注入SessionFactory,创建HibernateTemplate
@Resource
public void setMySessionFactory(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
@Override
public void save(Customer customer) {
this.getHibernateTemplate().save(customer);
}
}
- (3)Customer
package com.jinglisen.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_customer")
public class Customer implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="cust_name")
private String custName;
@Column(name="cust_phone")
private String custPhone;
@Column(name="cust_address")
private String custAddress;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
}
- (4)CustomerService
package com.jinglisen.service;
import com.jinglisen.domain.Customer;
public interface CustomerService {
public void save(Customer customer);
}
- (5)CustomerServiceImpl
package com.jinglisen.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jinglisen.dao.CustomerDao;
import com.jinglisen.domain.Customer;
import com.jinglisen.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
//注入CustomerDao
@Resource
private CustomerDao customerDao;
@Override
public void save(Customer customer) {
customerDao.save(customer);
}
}
- (6)CustomerAction
package com.jinglisen.web.action;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.jinglisen.domain.Customer;
import com.jinglisen.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/customer")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
//模型驱动接收页面参数
private Customer customer = new Customer();
@Override
public Customer getModel() {
return customer;
}
/*private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}*/
@Resource
private CustomerService custService;
@Action(value="save",results={@Result(name="success",location="/succ.jsp")})
public String save(){
System.out.println("执行CustomerAction的save");
/*ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
customerService = (CustomerService)ac.getBean("customerService");
customerService.save(customer);*/
//使用Spring的web监听器读取配置信息
/*WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
customerService = (CustomerService)ac.getBean("customerService");
customerService.save(customer);*/
//customerService.save(customer);
custService.save(customer);
return SUCCESS;
}
}
6.前端显示
save.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>客户添加页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="customer/save.action" method="post">
客户姓名:<input type="text" name="custName"/><br/>
客户电话:<input type="text" name="custPhone"/><br/>
客户地址:<input type="text" name="custAddress"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
succ.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>成功提示页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
添加成功!
</body>
</html>
项目下载链接:打开