Struts2+Spring+ibatis是J2EE的最新流行框架。
以下是部分搭建过程及源码:
1.先组合实现ibatis+Spring3支持,删除hibernate.cfg.xml文件,修改applicationContext.xml文件的内容,增加SessionFactory和dataSource的设置。
2.通过MyEclipse的向导方式,生成POJO类和对应的映射文件。
3.修改applicationContext.xml文件中<property name="mappingResources">元素的内容。
4.编写DAO接口和实现类。
5.修改applicationContext.xml文件,增加对Dao实现类的配置。
6.组合Struts2和Spring3,修改web.xml文件,增加struts2的所需要的过滤器配置。
7.增加struts2相应类库,增加struts2与spring的配置jar包。
8.拷贝struts.xml文件到src根目录下,再修改struts.xml文件,进行常量配置。
9.修改web.xml文件,配置Spring监听器,和上下文变量。并增加OpenSessionInViewFilter的设置。
10.写入action类。
11.配置struts.xml文件。
12.修改applicationContext.xml
13.编写Jsp文件。
14.加载运行项目。
?
1.文档说明:
我的Csdn http://blog.csdn.net/zl563143188
spring3.1 http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/
struts2 http://struts.apache.org/2.x/docs/home.html
hibernate http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/
这是我的QQ空间,欢迎参观 http://user.qzone.qq.com/563143188
程序源码下载地址 10MB:http://115.com/file/c2re58pm #
最新程序源码下载地址,一直在更新中............................... http://zz563143188.iteye.com/blog/1462413
程序lib下载地址 100MB:http://115.com/file/c2re52n6 #
数据库下载地址 (可以将数据转为sql,或者mysql,默认access)http://115.com/file/andcz97r #
由于涉及技术比较多,一般很难运行成功,需要技术指导联系 QQ 563143188,电话 13823045912 张林
Struts2 、 Spring 、 Hibernate 三者整合的过程示例
SSH简单整合( Struts2 .2.3.1+ Spring3 .1.0+ Hibernate4)
Struts2 + Spring + Hibernate 搭建全解!
Hibernate4 Struts2 Spring3 整合
?
功能介绍:
1.支持 Hibernate连接Access Hibernate连接sqlsever Hibernate连接mysql
2.支持 Spring连接access数据库 spring连接mysql spring连接sqlsever数据库
3.支持 Spring动态数据源加载 Spring连接ibaits Spring JDBC连接
4.支持 Spring的事务管理 Spring AOP实现 Spring的权限管理
5.支持 Spring MVC及Spring国际化标签 Spring文件上传下载
6.支持 Spring 数据源加载属性文件
7.支持 struts2+hibernate4+spring3 及struts2+itbaits+spring设计结构
8.支持 ext+json+treepanel实现动态树
9.支持 jasperreport+ireport+excel打印
10.支持 freemarker 生成文件
11.支持 sitemesh修饰网站
12. 支持 oscahe缓存 hibernate+ehcache缓存
13. 支持 struts2国际化
14 支持 velocity模板设计
15.支持 ext+dwr+json处理数据
16.支持 jquery图表制作
17.支持 log4j日志处理
18.支持 hibernate与ibaits同时访问数据库
19.支持 java读dll,ocx控制
20.支持 SSH、SSI框架同时使用
21.支持 Spring实时调度任务
22.支持随意更改BaseDaoImpl<T extends BaseModel,PK extends BasePK> extends BaseDaoIbaSpringImpl<T,PK>
二.下面是具体的代码实现:
1.顶层DAOp实现 :BaseDaoIbaSpringImpl
package com.senlo.analyze.core.common.dao.Impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.senlo.analyze.core.common.bean.BaseModel;
import com.senlo.analyze.db.impOrExp.bean.TableBean;
@Repository
public class BaseDaoIbaSpringImpl<T extends BaseModel> extends
SqlMapClientDaoSupport {
private String namespace;
@Autowired
@Resource(name = "sqlMapClient")
private SqlMapClient sqlMapClient;
@PostConstruct
public void initSqlMapClient() {
super.setSqlMapClient(sqlMapClient);
}
public int getCount(String and) {
int count = 0;
try {
Object obj = getSqlMapClientTemplate().queryForObject(
namespace + ".getCount", and);
if (obj != null)
count = Integer.parseInt(obj + "");
} catch (Exception e) {
System.out.println(namespace + ".getCount(String)异常"
+ e.getMessage());
}
return count;
}
public int getMaxId(String and) {
int count = 0;
try {
Object obj = getSqlMapClientTemplate().queryForObject(
namespace + ".getMaxId", and);
if (obj != null)
count = Integer.parseInt(obj + "");
} catch (Exception e) {
System.out.println(namespace + ".getMaxId(String)异常"
+ e.getMessage());
}
return count;
}
public List<T> getList(String and) {
List<T> list = new ArrayList<T>();
try {
list = getSqlMapClientTemplate().queryForList(
namespace + ".getList", and);
} catch (Exception e) {
System.out.println(namespace + ".getList(" + and + ")异常!"
+ e.getMessage());
}
return list;
}
@SuppressWarnings("unchecked")
public List<T> getPageList(String and, int pageSize, int pageNo) {
List<T> list = new ArrayList<T>();
try {
int startIndex = (pageNo - 1) * pageSize;
list = getSqlMapClientTemplate().queryForList(
namespace + ".getList", and, startIndex, pageSize);
} catch (Exception e) {
System.out.println(namespace + ".getList(" + and + ")异常!"
+ e.getMessage());
}
return list;
}
/*
* @SuppressWarnings("unchecked") public List<T> getRowList(String and, int
* pageSize, int pageNo) { List<T> list = new ArrayList<T>(); try { int
* startIndex = (pageNo - 1) * pageSize; list =
* client.queryForList(namespace + ".getList", and , startIndex, pageSize);
* } catch (Exception e) { epln(namespace + ".getList(" + and + ")异常!" +
* e.getMessage()); } return list; }
*/
@SuppressWarnings("unchecked")
public List<T> getRowList(String and, int pageSize, int pageNo) {
List<T> list = new ArrayList<T>();
try {
int startIdx = (pageNo - 1) * pageSize;
int endIdx = pageNo * pageSize;
Map<Object, Object> map = new HashMap<Object, Object>();
map.put("condition", and);
map.put("startIdx", startIdx);
map.put("endIdx", endIdx);
list = getSqlMapClientTemplate().queryForList(
namespace + ".getRowList", map);
} catch (Exception e) {
System.out.println(namespace + ".getRowList(" + and + ")异常!"
+ e.getMessage());
}
return list;
}
public List<TableBean> getListBySQL(String Sql) {
List<TableBean> list = new ArrayList<TableBean>();
try {
list = getSqlMapClientTemplate().queryForList(
namespace + ".getListBySQL", Sql);
} catch (Exception e) {
System.out.println(namespace + ".getListBySQL(" + Sql + ")异常!"
+ e.getMessage());
}
return list;
}
public int deleteByAnd(String and) {
int cnt = 0;
try {
cnt = getSqlMapClientTemplate().delete(namespace + ".delete", and);
} catch (Exception e) {
System.out.println(namespace + ".deleteByAnd(" + and + ")异常!"
+ e.getMessage());
}
return cnt;
}
public boolean updateById(T bean) {
boolean flag = false;
try {
flag = getSqlMapClientTemplate().update(namespace + ".updateById",
bean) > 0;
} catch (Exception e) {
System.out.println(namespace + ".updateById(" + bean + ")异常!"
+ e.getMessage());
}
return flag;
}
public T getBeanById(String id) {
T bean = null;
List<T> list = getList(" and fid='" + id + "'");
if (list.size() > 0) {
bean = list.get(0);
}
return bean;
}
public String insert(T bean) {
try {
getSqlMapClientTemplate().insert(namespace + ".insert", bean);
return "1";
} catch (Exception e) {
System.out.println(namespace + ".insert(" + bean + ")异常!"
+ e.getMessage());
return "0";
}
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
}
2.顶层IBaseDao接口
public interface IBaseIbaDao<M extends BaseModel> {
public int getMaxId(String and);
public int getCount(String and);
public List<M> getList(String and);
@SuppressWarnings("unchecked")
public List<M> getPageList(String and, int pageSize, int pageNo);
public String insert(M bean);
public M getBeanById(String id);
public boolean updateById(M bean);
public int deleteByAnd(String and);
public List<M> getRowList(String and, int pageSize, int pageNo);
}
3.与顶层DAO的中间层类BaseDaoImpl
package com.senlo.analyze.core.common.dao.Impl;
//BaseHibernateDaoImpl,BaseHibernateSpringDaoImpl,BaseDaoIbaImp,BaseDaoIbaSpringImp
//BaseHibernate4SpringDaoImpl,BaseHibernate3SpringDaoImpl 方法与数据连接
public class BaseDaoImpl<T extends BaseModel,PK extends BasePK> extends BaseDaoIbaSpringImp <T,PK> {}
4.具体的DAO层接口
package com.senlo.analyze.main.dao;
import com.senlo.analyze.core.common.dao.IBaseIbaDao;
import com.senlo.analyze.main.bean.SampleResult;
public interface SampleResultIbaDao extends IBaseIbaDao<SampleResult> {
}
5.具体的DAO层实现 BaseDaoImpl
package com.senlo.analyze.main.dao.Impl;
import org.springframework.stereotype.Repository;
import com.senlo.analyze.core.common.dao.Impl.BaseDaoIbaSpringImpl;
import com.senlo.analyze.main.bean.SampleResult;
import com.senlo.analyze.main.dao.SampleResultIbaDao;
import com.senlo.analyze.main.service.SampleResultSvr;
@Repository ("sampleResultIbaDaoImpl")
public class SampleResultIbaDaoImpl extends BaseDaoIbaSpringImpl<SampleResult>
implements SampleResultIbaDao {
public SampleResultIbaDaoImpl() {
super.setNamespace("sampleResult");
}
}
5.ibaits的sampleResult的xml文件,与数据库对应
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="sampleResult">
<resultMap id="sampleResult_map" class="com.senlo.analyze.main.bean.SampleResult">
<result property="fid" column="fid" />
<result property="item" column="item" />
<result property="fullName" column="fullName" />
<result property="itemResult" column="itemResult" />
<result property="reRange" column="reRange" />
<result property="unit" column="unit" />
<result property="prompt" column="prompt" />
<result property="sampleTrayID" column="sampleTrayID" />
<result property="sampleLocation" column="sampleLocation" />
<result property="testDate" column="testDate" />
<result property="remark" column="remark" />
<result property="isReTest" column="isReTest" />
<result property="testMomentSign" column="testMomentSign" />
<result property="reTestSign" column="reTestSign" />
<result property="itemID" column="itemID" />
<result property="reagentTrayID" column="reagentTrayID" />
<result property="modifySign" column="modifySign" />
<result property="modifyTime" column="modifyTime" />
<result property="originalityResult" column="originalityResult" />
<result property="modifyUserName" column="modifyUserName" />
<result property="testResult" column="testResult" />
<result property="reactionChroma" column="reactionChroma" />
</resultMap>
<select id="getMaxId" resultClass="int">
</select>
<select id="getCount" resultClass="int">
</select>
<select id="sampleResult.getList" resultMap="sampleResult_map"
parameterClass="java.lang.String">
select ItemParameter.EnglishShortening as Item,ItemParameter.ChineseFullName as FullName,SampleTestData.ItemResult as checkResult,SampleTestData.ItemResult,SampleTestData.InGearRange as ReRange,SampleTestData.ItemResultUnit as Unit,SampleTestData.TestVerdict as Prompt,SampleBaseInfor.SampleTrayID,SampleBaseInfor.SampleLocation,SampleTestData.TestDate,SampleTestData.Bak as Remark ,null as IsReTest,
SampleTestData.TestMomentSign,SampleTestData.ReTestSign,SampleTestData.ID as fid,SampleTestData.ItemID,ItemParameter.ReagentTrayID,
SampleTestData.ModifySign ,SampleTestData.ModifyTime,SampleTestData.OriginalityResult,SampleTestData.ModifyUserName,SampleTestData.TestResult,SampleTestData.ReactionChroma,SampleTestData.ResultExceptionSign,ItemParameter.CalcItemSign
from SampleTestData INNER JOIN ItemParameter ON SampleTestData.ItemID = ItemParameter.ID,SampleBaseInfor where
SampleTestData.SampleID=$value$ and (SampleTestData.connItem=0 or ItemParameter.CalcItemSign='1') AND SampleBaseInfor.ID=$value$
order by ItemParameter.PrintSerial
</select>
<select id="getbeanById" resultMap="sampleResult_map"
parameterClass="java.lang.String">
</select>
<select id="getRowList" resultMap="sampleResult_map"
parameterClass="java.lang.String">
</select>
<update id="update" parameterClass="com.senlo.analyze.main.bean.SampleResult">
</update>
<delete id="delete" parameterClass="java.lang.String">
</delete>
<delete id="deleteByAnd" parameterClass="java.lang.String">
</delete>
<insert id="insert" parameterClass="com.senlo.analyze.main.bean.SampleResult">
</insert>
<update id="updateById" parameterClass="com.senlo.analyze.main.bean.SampleResult">
</update>
</sqlMap>
6.service层的顶层接口
package com.senlo.analyze.core.common.service;
import java.util.List;
import com.senlo.analyze.core.common.bean.BaseModel;
public interface IBaseIbaSvr<M extends BaseModel> {
public int getMaxId(String and);
public int getCount(String and);
public List<M> getList(String and);
@SuppressWarnings("unchecked")
public List<M> getPageList(String and, int pageSize, int pageNo);
public String insert(M bean);
public M getBeanById(String id);
public boolean updateById(M bean);
public int deleteByAnd(String and);
public List<M> getRowList(String and, int pageSize, int pageNo);
}
7.service层的顶层实现
package com.senlo.analyze.core.common.service.Impl;
import java.util.List;
import com.senlo.analyze.core.common.bean.BaseModel;
import com.senlo.analyze.core.common.dao.IBaseIbaDao;
public class BaseSvrIbaImpl<M extends BaseModel> {
IBaseIbaDao dao;
public IBaseIbaDao getDao() {
return dao;
}
public void setDao(IBaseIbaDao dao) {
this.dao = dao;
}
public int getCount(String and) {
return dao.getCount(and);
}
public int getMaxId(String and) {
return dao.getMaxId(and);
}
public List<M> getList(String and) {
return dao.getList(and);
}
@SuppressWarnings("unchecked")
public List<M> getPageList(String and, int pageSize, int pageNo) {
return dao.getPageList(and, pageSize, pageNo);
}
@SuppressWarnings("unchecked")
public List<M> getRowList(String and, int pageSize, int pageNo) {
return dao.getRowList(and, pageSize, pageNo);
}
public int deleteByAnd(String and) {
return dao.deleteByAnd(and);
}
public boolean updateById(M bean) {
return dao.updateById(bean);
}
public M getBeanById(String id) {
return (M) dao.getBeanById(id);
}
public String insert(M bean) {
return dao.insert(bean);
}
}
8.service具体实现的接口:
package com.senlo.analyze.main.service;
import com.senlo.analyze.core.common.service.IBaseIbaSvr;
import com.senlo.analyze.main.bean.SampleResult;
public interface SampleResultSvr extends IBaseIbaSvr<SampleResult> {
}
9.service具体实现的父类:
package com.senlo.analyze.core.common.service.Impl;
import java.util.List;
import com.senlo.analyze.core.common.bean.BaseModel;
import com.senlo.analyze.core.common.dao.IBaseIbaDao;
public class BaseSvrIbaImpl<M extends BaseModel> {
IBaseIbaDao dao;
public IBaseIbaDao getDao() {
return dao;
}
public void setDao(IBaseIbaDao dao) {
this.dao = dao;
}
public int getCount(String and) {
return dao.getCount(and);
}
public int getMaxId(String and) {
return dao.getMaxId(and);
}
public List<M> getList(String and) {
return dao.getList(and);
}
@SuppressWarnings("unchecked")
public List<M> getPageList(String and, int pageSize, int pageNo) {
return dao.getPageList(and, pageSize, pageNo);
}
@SuppressWarnings("unchecked")
public List<M> getRowList(String and, int pageSize, int pageNo) {
return dao.getRowList(and, pageSize, pageNo);
}
public int deleteByAnd(String and) {
return dao.deleteByAnd(and);
}
public boolean updateById(M bean) {
return dao.updateById(bean);
}
public M getBeanById(String id) {
return (M) dao.getBeanById(id);
}
public String insert(M bean) {
return dao.insert(bean);
}
}
10.service具体实现:
package com.senlo.analyze.main.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.senlo.analyze.core.common.service.Impl.BaseSvrIbaImpl;
import com.senlo.analyze.main.bean.SampleResult;
import com.senlo.analyze.main.dao.HospitalSectionOfficeDao;
import com.senlo.analyze.main.dao.SampleResultIbaDao;
import com.senlo.analyze.main.dao.Impl.SampleResultIbaDaoImpl;
import com.senlo.analyze.main.service.SampleResultSvr;
@Service("sampleResultSvrImpl")
public class SampleResultSvrImpl extends BaseSvrIbaImpl<SampleResult> implements
SampleResultSvr {
//SampleResultIbaDao dao;// =new SampleResultIbaDaoImpl();
public SampleResultIbaDao getDao() {
return (SampleResultIbaDao) super.getDao();
}
@Resource(name = "sampleResultIbaDaoImpl")
public void setDao(SampleResultIbaDao dao) {
super.setDao(dao);
}
}
11.代码测试;
package com.senlo.analyze.main.test;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
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 org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.senlo.analyze.core.common.bean.JSONTreeNode;
import com.senlo.analyze.core.common.bean.Menu;
import com.senlo.analyze.core.common.service.MenuSvr;
import com.senlo.analyze.main.bean.SampleResult;
import com.senlo.analyze.main.dao.Impl.SampleResultIbaDaoImpl;
import com.senlo.analyze.main.service.SampleResultSvr;
import com.senlo.analyze.main.service.impl.SampleResultSvrImpl;
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class TIbatisDao {
@Autowired
// SampleResultSvr dao;
MenuSvr svr;
@Test
public void showData() {
String sql = "";
// List<SampleResult> list = dao.getList(sql);
// System.out.println(list.size() + "------ibaits");
// List<Menu> list2 = dao2.FindAll();
// System.out.println(list2.size());
}
@Test
public void tree() {
ArrayList<JSONTreeNode> TreeNodeArray = null;
StringBuffer parentIDBuffer = new StringBuffer();
List<Menu> listPID = svr.FindAll(" and leaf=0 ");
parentIDBuffer.append("|");
// parentIDBuffer.append(listPID.get(i).getText());// 这里是取得父结点
String parentIDString = parentIDBuffer.toString();
List<Menu> listLeaf = svr.FindAll(" and 1=1 ");
TreeNodeArray = new ArrayList<JSONTreeNode>();
for (int i = 0; i < listPID.size(); i++) {
for (int j = 0; j < listLeaf.size(); j++) {
Menu mBean = listLeaf.get(j);
JSONTreeNode TreeNode = new JSONTreeNode();
TreeNode.setId(mBean.getFid());
TreeNode.setText(mBean.getText());
TreeNode.setDescription(mBean.getRemark());
TreeNode.setHref(mBean.getHref()); // TreeNode.setId
TreeNode.setHrefTarget(mBean.getHrefTarget());
if (listPID.get(i).getFid() != listLeaf.get(j).getPid()) //子节点
// //TreeNode.setId
{
TreeNode.setCls("folder");
TreeNode.setLeaf(false);
TreeNode.setExpandable(false);
} else // 父节点
{
TreeNode.setCls("file");
TreeNode.setLeaf(true);
TreeNode.setExpandable(false);
}
TreeNodeArray.add(TreeNode);
}
}
JSONArray JsonArray = JSONArray.fromObject(TreeNodeArray); // 得到JSON数组
System.out.println(JsonArray.toString());
}
}
由于配置文件太多 详细说明 :http://zz563143188.iteye.com/admin/blogs/1462413
万福!