SSH项目整合
一、maven工程
pom.xml
4.0.0
com.zengqingfa.ssh
ssh
1.0-SNAPSHOT
war
4.2.4.RELEASE
2.3.24
5.0.7.Final
org.apache.struts
struts2-core
${struts.version}
javassist
javassist
org.hibernate
hibernate-core
${hibernate.version}
org.springframework
spring-context
4.2.2.RELEASE
org.apache.struts
struts2-spring-plugin
${struts.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-web
${spring.version}
org.aspectj
aspectjweaver
1.8.7
org.slf4j
slf4j-api
1.7.12
org.slf4j
slf4j-log4j12
1.7.12
c3p0
c3p0
0.9.1.2
jstl
jstl
1.2
javax.servlet
servlet-api
2.5
provided
mysql
mysql-connector-java
5.1.6
runtime
junit
junit
com.alibaba
fastjson
1.2.47
org.apache.struts
struts2-json-plugin
2.3.24
net.sf.json-lib
json-lib
2.4
jdk15
junit
junit
4.12
test
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
1.7
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
8080
/
UTF-8
tomcat7
二、domain
1、Customer.java
package com.zengqingfa.domain;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:22
*
*/
import java.io.Serializable;
/**
* PO(Persistence Object)持久化类
* 7个规范
* 1. 公有类
* 2. 公有无参构造
* 3. 私有属性
* 4. 公有的getter与setter
* 5. 实现java.io.Serializable接口
* 6. 不能用final修饰
* 7. 如果是基础类型,要使用它的包装类
*/
public class Customer implements Serializable {
private Long custId; //id
private String custName; //名字
private Long custUserId; //用户id
private Long custCreateId;
private String custIndustry;
private String custLevel;
private String custLinkman;
private String custPhone;
private String custMobile;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public Long getCustUserId() {
return custUserId;
}
public void setCustUserId(Long custUserId) {
this.custUserId = custUserId;
}
public Long getCustCreateId() {
return custCreateId;
}
public void setCustCreateId(Long custCreateId) {
this.custCreateId = custCreateId;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustLinkman() {
return custLinkman;
}
public void setCustLinkman(String custLinkman) {
this.custLinkman = custLinkman;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustMobile() {
return custMobile;
}
public void setCustMobile(String custMobile) {
this.custMobile = custMobile;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custUserId=" + custUserId +
", custCreateId=" + custCreateId +
", custIndustry='" + custIndustry + '\'' +
", custLevel='" + custLevel + '\'' +
", custLinkman='" + custLinkman + '\'' +
", custPhone='" + custPhone + '\'' +
", custMobile='" + custMobile + '\'' +
'}';
}
}
三、action
1、CustomerAction.java
package com.zengqingfa.action;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:23
*
*/
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Result;
import com.zengqingfa.domain.Customer;
import com.zengqingfa.service.CustomerService;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CustomerAction {
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
@Autowired
private CustomerService customerService;
public String findAll() {
List list = customerService.findAll();
ServletActionContext.getContext().put("list", list);
return "list";
}
}
2、TestCustomerJson.java
package com.zengqingfa.action;
/*
* @ author zengqingfa
* @ created in 2019/2/17 19:18
*
*/
import com.opensymphony.xwork2.ActionSupport;
import com.zengqingfa.domain.Customer;
import com.zengqingfa.service.CustomerService;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestCustomerJson extends ActionSupport {
private String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
private List list = new ArrayList<>();
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Autowired
private CustomerService customerService;
//使用struts2返回json数据,需要在struts.xml配置要返回的数据
public String execute() {
list = customerService.findAll();
return "success";
}
//测试返回json数据方式一
public String testJson() {
try {
List list = customerService.findAll();
//将数据存储在map里,再转换成json类型数据,也可以自己手动构造json类型数据
Map map = new HashMap();
map.put("list", list);
JSONObject json = JSONObject.fromObject(map);//将map对象转换成json类型数据
result = json.toString();//给result赋值,传递给页面
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
//原生方式返回json数据
public void testJson02() {
try {
HttpServletResponse response = ServletActionContext.getResponse();
/*
* 在调用getWriter之前未设置编码(既调用setContentType或者setCharacterEncoding方法设置编码),
* HttpServletResponse则会返回一个用默认的编码(既ISO-8859-1)编码的PrintWriter实例。这样就会
* 造成中文乱码。而且设置编码时必须在调用getWriter之前设置,不然是无效的。
* */
response.setContentType("text/html;charset=utf-8");
//response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
List list = customerService.findAll();
//将数据存储在map里,再转换成json类型数据,也可以自己手动构造json类型数据
Map map = new HashMap();
map.put("list", list);
JSONObject json = JSONObject.fromObject(map);//将map对象转换成json类型数据
out.print(json.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
四、service
1、service接口
package com.zengqingfa.service;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:22
*
*/
import com.zengqingfa.domain.Customer;
import java.util.List;
public interface CustomerService {
List findAll();
}
2、service实现
package com.zengqingfa.service.impl;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:23
*
*/
import com.zengqingfa.dao.CustomerDao;
import com.zengqingfa.domain.Customer;
import com.zengqingfa.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
//注入dao
@Autowired
private CustomerDao customerDao;
@Override
public List findAll() {
return customerDao.findAll();
}
}
五、dao
1、dao接口
package com.zengqingfa.dao;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:21
*
*/
import com.zengqingfa.domain.Customer;
import java.util.List;
public interface CustomerDao {
List findAll();
Customer findCustomerById(long custId);
void save(Customer customer);
}
2、dao实现
package com.zengqingfa.dao.impl;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:32
*
*/
import com.zengqingfa.dao.CustomerDao;
import com.zengqingfa.domain.Customer;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
@Override
public List findAll() {
return (List) this.getHibernateTemplate().find("from Customer");
}
@Override
public Customer findCustomerById(long custId) {
return this.getHibernateTemplate().get(Customer.class, custId);
}
@Override
public void save(Customer customer) {
this.getHibernateTemplate().save(customer);
}
}
六、配置文件
1、web.xml
contextConfigLocation
classpath*:spring/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
openSessionInView
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
openSessionInView
/*
struts
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts
/*
2、struts.xml
/list.jsp
result
3、applicationContext.xml
4、hibernate.cfg.xml
org.hibernate.dialect.MySQL5InnoDBDialect
true
false
update
true
none
5、Customer.hbm.xml
6、log4j.properties
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-ddHH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
七、测试
1、TestMysql
package com.zengqingfa;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:44
*
*/
import com.zengqingfa.dao.CustomerDao;
import com.zengqingfa.domain.Customer;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class TestMysql {
@Test
public void TestFindAll() {
//获取spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDao");
List customers = customerDao.findAll();
for (Customer customer : customers) {
System.out.println(customer);
}
}
@Test
public void TestFindCustomerById() {
//获取spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDao");
Customer customer = customerDao.findCustomerById(11l);
System.out.println(customer);
}
}
2、TestSSH:与junit进行整合
package com.zengqingfa;
/*
* @ author zengqingfa
* @ created in 2019/2/17 16:44
*
*/
import com.zengqingfa.dao.CustomerDao;
import com.zengqingfa.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
public class TestSSH {
@Autowired
private CustomerDao customerDao;
@Test
public void TestFindAll() {
List customers = customerDao.findAll();
for (Customer customer : customers) {
System.out.println(customer);
}
}
@Test
public void TestFindCustomerById() {
Customer customer = customerDao.findCustomerById(11l);
System.out.println(customer);
}
}
八、页面
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Insert title here
${customer.custId } -> ${customer.custName }
九、展示
1、http://localhost:8080/findAll.action
2、http://localhost:8080/test_json.action
"{\"list\":[{\"custCreateId\":0,\"custId\":11,\"custIndustry\":\"\",\"custLevel\":\"\",\"custLinkman\":\"\",\"custMobile\":\"\",\"custName\":\"22\",\"custPhone\":\"\",\"custUserId\":22},{\"custCreateId\":0,\"custId\":33,\"custIndustry\":\"dd\",\"custLevel\":\"\",\"custLinkman\":\"\",\"custMobile\":\"\",\"custName\":\"bb\",\"custPhone\":\"\",\"custUserId\":0},{\"custCreateId\":0,\"custId\":55,\"custIndustry\":\"\",\"custLevel\":\"\",\"custLinkman\":\"\",\"custMobile\":\"\",\"custName\":\"\",\"custPhone\":\"\",\"custUserId\":0},{\"custCreateId\":0,\"custId\":66,\"custIndustry\":\"\",\"custLevel\":\"\",\"custLinkman\":\"\",\"custMobile\":\"\",\"custName\":\"77\",\"custPhone\":\"\",\"custUserId\":0}]}"
3、http://localhost:8080/test_json2.action
{"list":[{"custCreateId":0,"custId":11,"custIndustry":"","custLevel":"","custLinkman":"","custMobile":"","custName":"22","custPhone":"","custUserId":22},{"custCreateId":0,"custId":33,"custIndustry":"dd","custLevel":"","custLinkman":"","custMobile":"","custName":"bb","custPhone":"","custUserId":0},{"custCreateId":0,"custId":55,"custIndustry":"","custLevel":"","custLinkman":"","custMobile":"","custName":"","custPhone":"","custUserId":0},{"custCreateId":0,"custId":66,"custIndustry":"","custLevel":"","custLinkman":"","custMobile":"","custName":"77","custPhone":"","custUserId":0}]}