欢迎访问个人博客:打开链接
项目下载地址:打开
本项目是基于最新的Java开发框架使用全注解来开发的SSH工程项目。
1. SSH搭建环境
jdk1.8 + tomcat8.5 + eclipse oxygen
Spring5.0.3 + Hibernate5.0.7 + Struts2.5.13
2. 工程总体框架结构
3. 所需jar包
4. SSH整合配置
- (1)JdbcConfig
package com.jinglisen.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@PropertySource(value="classpath:jdbc.properties")
public class JdbcConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driverClass}")
private String driverClass;
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password;
@Bean(name="dataSource")
public DataSource getDataSource(){
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setJdbcUrl(url);
ds.setDriverClass(driverClass);
ds.setUser(user);
ds.setPassword(password);
return ds;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean(name="sessionFactory")
public LocalSessionFactoryBean getSessionFactory(@Qualifier("dataSource")DataSource dataSource){
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
factory.setDataSource(dataSource);
Properties props = new Properties();
props.setProperty("hibernate.show_sql", "true");
props.setProperty("hibernate.format_sql", "true");
props.setProperty("hibernate.hbm2ddl.auto", "update");
factory.setHibernateProperties(props);
factory.setPackagesToScan("com.jinglisen.domain");
return factory;
}
}
- (2)SpringConfig
package com.jinglisen.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@Configurable
@ComponentScan(value="com.jinglisen")
@Import(value={JdbcConfig.class,TransactionConfig.class})
public class SpringConfig {
}
- (3)TransactionConfig
package com.jinglisen.config;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
public class TransactionConfig {
@Bean(name="transactionManager")
public HibernateTransactionManager getTransactionManager(@Qualifier("sessionFactory")SessionFactory sessionFactory){
HibernateTransactionManager manager = new HibernateTransactionManager();
manager.setSessionFactory(sessionFactory);
return manager;
}
}
- (4)WebStart
package com.jinglisen.config;
import org.springframework.web.context.AbstractContextLoaderInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
/**
* web启动
* @author JasonJing
* */
public class WebStart extends AbstractContextLoaderInitializer{
@Override
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(SpringConfig.class);
return ac;
}
}
- (5)jdbc.properties
jdbc.url=jdbc:mysql:///ssh
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
- (6)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_Anotation</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>
<welcome-file-list>
<welcome-file>save.jsp</welcome-file>
</welcome-file-list>
</web-app>
5. 面向接口编程三层架构文件
- (1)CustomerDao
package com.jinglisen.dao;
import java.util.List;
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;
}
@Resource
private CustomerService custService;
@Action(value="save",results={@Result(name="success",location="/succ.jsp")})
public String save(){
System.out.println("执行CustomerAction的save");
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>
项目下载地址:打开