本案例源代码已经上传,叫做“SSH框架搭建源代码.zip” ,笔者亲自调试通过的可以直接运行。
一、总体结构
其中:
(1)po层
(2)dao层
(3)业务层
(4)控制层
(5)展现层
二、配置文件
1、struts2.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
“http://struts.apache.org/dtds/struts-2.1.dtd”>
goodsListAction
/goodsList.jsp
goodsListAction
goodsListAction
/goodsList.jsp
/pages/updateGoods.jsp
goodsListAction
2、web.xml
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
3、容器applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> org.hibernate.dialect.MySQLDialect true com/po/TGoods.hbm.xml com.mysql.jdbc.Driver jdbc:mysql://127.0.0.1:3306/shopping root
<bean id="springTest" class="com.test.SpringTest"/>
<bean id="goodsDao" class="com.dao.impl.GoodsDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="goodsService" class="com.service.impl.GoodsServiceImpl">
<property name="goodsDao" ref="goodsDao"/>
</bean>
三、各层代码
1、po层
package com.po;
/**
- TGoods entity. @author MyEclipse Persistence Tools
*/
public class TGoods implements java.io.Serializable {
// Fields
private String GId;
private String GName;
private Float GPrice;
// Constructors
/** default constructor */
public TGoods() {
}
/** minimal constructor */
public TGoods(String GId) {
this.GId = GId;
}
/** full constructor */
public TGoods(String GId, String GName, Float GPrice) {
this.GId = GId;
this.GName = GName;
this.GPrice = GPrice;
}
// Property accessors
public String getGId() {
return this.GId;
}
public void setGId(String GId) {
this.GId = GId;
}
public String getGName() {
return this.GName;
}
public void setGName(String GName) {
this.GName = GName;
}
public Float getGPrice() {
return this.GPrice;
}
public void setGPrice(Float GPrice) {
this.GPrice = GPrice;
}
}
<?xml version="1.0" encoding="utf-8"?> 2、dao层 package com.dao;public interface GenerateDao {
public void save(T t);
public void delete(T t);
public void update(T t);
public T get(Class c,String id);
public T load(Class c,String id);
}
package com.dao;
import java.util.List;
import com.po.TGoods;
public interface GoodsDao extends GenerateDao{
//通过hql查询,返回对象
public List goodsList(String hql);
//将sql批量添加到Batch
public void addBatch(String sql);
//批量添加S
public void excuteBatch();
}
package com.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.dao.GoodsDao;
import com.po.TGoods;
public class GoodsDaoImpl extends HibernateDaoSupport implements GoodsDao
{
private Connection conn;
private Statement stm;
public GoodsDaoImpl()
{
}
public List goodsList(String hql) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().find(hql);
}
public void delete(TGoods t) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(t);
}
public TGoods get(Class c, String id) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().get(c, id);
}
public TGoods load(Class c, String id) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().load(c, id);
}
public void save(TGoods t) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(t);
}
public void update(TGoods t) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(t);
}
public void addBatch(String sql) {
// TODO Auto-generated method stub
}
public void excuteBatch() {
// TODO Auto-generated method stub
}
}
3、业务层ceserive
package com.service;
public interface GenerateService {
public void save(T t);
public void delete(T t);
public void update(T t);
public T get(Class c,String id);
public T load(Class c,String id);
}
package com.service;
import java.util.List;
import com.po.TGoods;
public interface GoodsService extends GenerateService{
public List goodsList(String hql);
}
package com.service.impl;
import java.util.List;
import com.dao.GoodsDao;
import com.po.TGoods;
import com.service.GoodsService;
public class GoodsServiceImpl implements GoodsService {
GoodsDao goodsDao;
public void setGoodsDao(GoodsDao goodsDao) {
this.goodsDao = goodsDao;
}
public List goodsList(String hql) {
// TODO Auto-generated method stub
return goodsDao.goodsList(hql);
}
public void delete(TGoods t) {
// TODO Auto-generated method stub
goodsDao.delete(t);
}
public TGoods get(Class c, String id) {
// TODO Auto-generated method stub
return goodsDao.get(c, id);
}
public TGoods load(Class c, String id) {
// TODO Auto-generated method stub
return goodsDao.load(c, id);
}
public void save(TGoods t) {
// TODO Auto-generated method stub
goodsDao.save(t);
}
public void update(TGoods t) {
// TODO Auto-generated method stub
goodsDao.update(t);
}
}
4、控制层
package com.controller;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.po.TGoods;
import com.service.GoodsService;
public class GoodsListAction {
GoodsService goodsService;
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
public String goodsList()
{
List goodsList=goodsService.goodsList(“from TGoods”);
for(TGoods g:goodsList)
{
System.out.println(g.getGId()+" “+g.getGName()+” "+g.getGPrice());
}
ServletActionContext.getRequest().setAttribute(“goodsList”, goodsList);
return “goodsList”;
}
}
//
package com.controller;
import com.po.TGoods;
import com.service.GoodsService;
public class DeleteGoodsAction {
private TGoods goods;
public TGoods getGoods() {
return goods;
}
public void setGoods(TGoods goods) {
this.goods = goods;
}
GoodsService goodsService;
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
public String deleteGoods()
{
if(goods!=null)
{
String gid=goods.getGId();
System.out.println("gid===="+gid);
goodsService.delete(goods);
}
return "deleteGoods";
}
}
//
package com.controller;
import org.apache.struts2.ServletActionContext;
import com.po.TGoods;
import com.service.GoodsService;
public class DeleteSelAction {
GoodsService goodsService;
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
public String deleteSel()
{
String gids[]=ServletActionContext.getRequest().getParameterValues(“chnames”);
for (int i = 0; i < gids.length; i++) {
TGoods goods=new TGoods();
goods.setGId(gids[i]);
goodsService.delete(goods);
System.out.println(“gids[i]======”+gids[i]);
}
return “deleteSel”;
}
}
package com.controller;
import com.po.TGoods;
import com.service.GoodsService;
public class LoadUpdateGoodsAction {
private TGoods goods;
public TGoods getGoods() {
return goods;
}
public void setGoods(TGoods goods) {
this.goods = goods;
}
GoodsService goodsService;
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
public String updateGoods()
{
if(goods!=null)
{
String gid=goods.getGId();
TGoods g=goodsService.get(TGoods.class, gid);
goods.setGId(g.getGId());
goods.setGName(g.getGName());
goods.setGPrice(g.getGPrice());
}
return “updateGoods”;
}
}
//
package com.controller;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.po.TGoods;
import com.service.GoodsService;
public class QueryByNameAction {
GoodsService goodsService;
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
public String queryByName()
{
String gname=ServletActionContext.getRequest().getParameter(“gname”);
List goodsList=goodsService.goodsList(“from TGoods where GName like '%”+gname+"%’");
for(TGoods g:goodsList)
{
System.out.println(g.getGId()+" “+g.getGName()+” "+g.getGPrice());
}
ServletActionContext.getRequest().setAttribute(“goodsList”, goodsList);
return “queryByName”;
}
}
package com.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.po.TGoods;
import com.service.GoodsService;
public class SaveGoodsAction {
private TGoods goods;
public TGoods getGoods() {
return goods;
}
public void setGoods(TGoods goods) {
this.goods = goods;
}
GoodsService goodsService;
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
public String saveGoods()
{
Date d=new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyyMMddHHmmss”);
String dd=simpleDateFormat.format(d);
String gid=“G”+dd;
System.out.println(“goods===”+goods);
if(goods!=null)
{
System.out.println("====gid"+goods.getGId());
System.out.println("====gname"+goods.getGName());
System.out.println("====gprice"+goods.getGPrice());
goods.setGId(gid);
goodsService.save(goods);
}
return “saveGoods”;
}
}
//
package com.controller;
import com.po.TGoods;
import com.service.GoodsService;
public class UpdateGoodsAction
{
private TGoods goods;
public TGoods getGoods() {
return goods;
}
public void setGoods(TGoods goods) {
this.goods = goods;
}
GoodsService goodsService;
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
/*
* 功能:按照商品编号修改商品信息
* 参数:
* 返回值:成功之后跳到列表页面
*/
public String updateGoods()
{
if(goods!=null)
{
goodsService.update(goods);
}
return “updateGoods”;
}
}
5、展现层
(1)goodsList.jsp
<%@ page language=“java” import=“java.util.*” pageEncoding=“gbk”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core”%>
请输入商品名: | ||||
商品编号 | 商品名称 | 商品价格 | 操作 | |
${data.GId} | ${data.GName} | ${data.GPrice} | 删除 修改 |
(2)saveGoods.jsp
<%@ page language=“java” import=“java.util.*” pageEncoding=“gbk”%>
新增商品 | |
商品名称: | |
商品价格: | |
(3)updateGoods.jsp
<%@ page language=“java” import=“java.util.*” pageEncoding=“gbk”%>
修改商品 | |
商品名称: | |
商品价格: | |