`

SSI整合以及缓存的配置(struts2.2.1.1 + spring3.0.1 + ibatis2.3.0.677)

 
阅读更多

我使用的是myEclipse8.6版本

1.首先导入struts2的jar包和插件包(在下载的struts-2.2.1.1-all/struts-2.2.1.1/apps/struts2-blank/WEB-INF/lib下所有jar包 和struts-2.2.1.1-all/struts-2.2.1.1/lib/struts2-spring-plugin-2.2.1.1.jar)

2.使用myEclipse8.6自带的功能引入spring3.0的jar包

3.加入ehcache-1.6.0.jar,ibatis-2.3.0.677.jar,(网上自己下载)

ojdbc14.jar,org.springframework.context.support-3.0.0.RELEASE.jar

接下来是配置文件:

web.xml:

<!-- 读取spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:cacheContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- struts 过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

struts.xml:

因为有struts2-spring-plugin-2.2.1.1.jar

所有我可以写Action的全路径(com.jungle.action.UserAction)而不用在applicationContext.xml中配置Action

<struts>
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default" namespace="/user">
<action name="user" class="com.jungle.action.UserAction">
<result>/index.jsp</result>
</action>
</package>
</struts>

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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
配置文件读取 需 要加入applicationContext.properties文件<bean
id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"> <list>
<value>classpath:applicationContext.properties</value> </list>
</property> </bean>
-->


<!-- 支持元注释 -->
<context:annotation-config />

<!-- 扫描包目录 -->
<context:component-scan base-package="com"></context:component-scan>

<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:oracle" />
<property name="username" value="chenl" />
<property name="password" value="chenl" />
</bean>
<!-- 大字段 -->
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />

<!-- ibatis 必须 因为需要读取配置文件 classpath:sqlmap-config.xml -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlmap-config.xml" />
<property name="dataSource" ref="dataSource" />
<property name="lobHandler" ref="lobHandler"></property>
</bean>

<!-- 1.第一种方式(必须使用注解的方式@Resource) : 自定义一些方法 如queryForString,queryForBoolean等 -->
<bean id="mySqlMapClientDaoSupport" class="com.jungle.dao.common.MySqlMapClientDaoSupport">
<property name="dataSource" ref="dataSource" />
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>

<!-- 2.第二种方式(必须使用注解的方式@Resource),使用get,set方式需要使用接口的方式才行-->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="dataSource" ref="dataSource" />
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>

</beans>

ibatis的配置文件(sqlmap-config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<!-- ibatis配置文件 -->
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
maxRequests="512"
maxSessions="128"
maxTransactions="32"
useStatementNamespaces="true"
/>

<!-- 引入xml文件 -->
<sqlMap resource="com/jungle/dao/config/common.ibatis.xml" />
</sqlMapConfig>

common.ibatis.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<!-- 例子 -->
<sqlMap namespace="common">
<typeAlias alias="user" type="com.jungle.dao.entity.User" />

<resultMap id="resultUser" class="com.jungle.dao.entity.User">
<result column="NID" property="nid" />
<result column="PASSWORD" property="password" />
<result column="USERNAME" property="username" />
</resultMap>

<!-- 查询一个User-->
<select id="getSingleUser" parameterClass="string" resultMap="resultUser">
<!--[CDATA[
select nid,username,password
from t_user
where nid = #nid#
ORDER BY nid
]]>
</select>

<!-- 查询出列表 -->
<select id="getUserList" resultMap="resultUser">
<![CDATA[
select nid,username,password
from t_user
ORDER BY nid
]]-->
</select>
</sqlMap>

因为web.xml中有加入classpath:cacheContext.xml 这是缓存的文件

所以必须在src下加入cacheContext.xml和ehcache-application.xml两个文件

ehcache-application.xml:(这个文件中最好不要加中文和注释否则可能会有问题)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>

<cache name="coreCache"
maxElementsInMemory="3" eternal="true" overflowToDisk="false"/>


</ehcache>

cacheContext.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"
>

<!--缓存-->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache-application.xml</value>
</property>
</bean>

<!--核心缓存
使用时:
@Resource
private Cache coreCache-->


<bean id="coreCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager"/>
</property>
<property name="cacheName">
<value>coreCache</value>
</property>
</bean>

</beans>

配置文件完成了。

接下来看看如何使用:

UserDaoImpl:

@Component("userDao")
public class UserDaoImpl implements UserDao {
// 使用get,set方式不行 ,因为SqlMapClientTemplate不是接口
@Resource
private SqlMapClientTemplate sqlMapClientTemplate;

@Resource
private MySqlMapClientDaoSupport mySqlMapClientDaoSupport;

UserAction:


public class UserAction extends ActionSupport {
@Resource
private UserDao userDao;

@Override
public String execute() throws Exception {
System.out.println(userDao.getUser("1"));
return SUCCESS;
}
}

PS:原来是有加xml的格式的 可是IE一查看就卡死 所以这里XML才全部没有套格式。

分享到:
评论

相关推荐

    eclipse开发的ssi整合struts2+ibatis2.x+spring2.5

    下面将详细介绍如何在Eclipse中进行这个整合,并提供一个适合新手学习的SSSI(Struts2、Spring、iBatis、SSI)整合的Demo。 1. **Struts2框架**: Struts2是一个基于MVC设计模式的Web应用框架,用于简化Java Web...

    struts2+spring+Ibatis框架包

    在SSi框架中,Spring通常用于管理Bean的生命周期,提供事务控制,以及整合其他框架,如iBatis。 iBatis是一个持久层框架,它将SQL语句与Java代码分离,降低了数据访问层的复杂性。它允许开发者直接编写SQL,然后...

    SSI框架整合(Struts2.1+Spring3.0+Ibatis 2.3)

    SSI框架整合(Struts2.1+Spring3.0+Ibatis 2.3) 下载之后执行.sql文件创建表和sequence 记得改数据库访问地址以及数据库登录用户名密码 项目访问路径 localhost:8080/SSI_Demo1 一个简单的例子(Oracle数据库增删改查...

    Struts2.2+Spring2.5+iBatis2.3的ERP项目

    本人主要是项目的SSI环境的搭建及登录功能,并不是完整的一套ERP系统,里面有相关文档,你可以根据相关文档继续开发学习!

    SSI(struts2+spring2.5+ibatis2.3)项目实例

    **SSI(Struts2+Spring2.5+Ibatis2.3)项目实例详解** **一、Struts2框架** Struts2是一个基于MVC设计模式的Java Web开发框架,它继承了Struts1和WebWork的优点,提供了一种更灵活、更强大的控制层解决方案。在SSI...

    Ssi的整合(Struts2+Spring+Ibatis)

    **SSI整合:Struts2+Spring+Ibatis详解** 在Java Web开发中,Struts2、Spring和Ibatis是常见的三大框架,它们各自负责不同的职责,共同构建出强大的后端应用架构。Struts2作为MVC框架处理请求和视图展示,Spring...

    ssi2(struts2+spring+ibatis)整合加用户增加改查操作

    "ssi2(struts2+spring+ibatis)整合加用户增加改查操作"这个主题涵盖了这三个框架的集成以及基本的用户管理功能,包括用户数据的增删改查。下面将详细讲解这些知识点。 **1. Struts2框架** Struts2是一个基于MVC设计...

    SSI+EXT (Struts2+Spring+Ibatis+Ext)

    **SSI+EXT(Struts2+Spring+Ibatis+Ext)**是一个常见的Java Web开发框架组合,用于构建高效、可扩展的企业级应用。这个框架集合了Struts2作为MVC框架,Spring作为依赖注入和事务管理工具,Ibatis作为持久层解决方案...

    Struts2+Spring2+iBatis2整合的例子

    Struts2、Spring和iBatis是Java Web开发中三个非常重要的框架,它们分别负责表现层、业务层和数据访问层。将这三个框架整合在一起,可以实现MVC(Model-View-Controller)架构,提高应用的灵活性和可维护性。 **...

    图文搭建SSI(struts+spring+ibatis)框架

    本文将详细介绍如何使用MyEclipse8.5进行SSI(Struts2、Spring2.5、iBatis)框架的整合搭建。首先,我们需要一个新的Web项目作为基础。 1. **创建Web Project** 在MyEclipse8.5中,选择`File` -&gt; `New` -&gt; `...

    ssi(struts+spring+maven+mybatis)整合

    **SSI(Struts + Spring + Maven + MyBatis)整合详解** 在现代Java Web开发中,集成多种框架来实现高效、灵活的应用程序架构是常见的做法。`SSI`整合,即Struts、Spring、Maven和MyBatis的组合,旨在提供一个强大...

    struts2 + spring2.5 + ibatis2.3.4整合包文件

    5. 整合过程:通常,整合这三大框架需要配置Struts2的struts.xml、Spring的applicationContext.xml以及iBatis的sqlMapConfig.xml等文件。还需要在Struts2的Action类中注入Spring管理的bean,以便于调用业务服务。...

    MyEclispse下SSI整合JAR包(Struts1+Spring3+ibatis2)

    创建applicationContext.xml文件,定义Bean的配置,包括Spring的AOP、DI配置,以及Spring与Struts1的整合配置。 5. **配置iBATIS/MyBatis**:创建SqlMapConfig.xml文件,配置数据源、事务管理器以及SQL映射文件的...

    Spring3.2.1+struts2.3.15.1+mybatis3.2.2 集合jar包

    Spring3.2.1+struts2.3.15.1+mybatis3.2.2 集合jar包 测试能用,整个包比较精简。基本没有多余的包。 里面带了commons-configuration-1.9.jar(操作xml文件的公共类包,很实用很强大,它依赖的commons-lang-2.3.jar...

    struts2.2+spring3.1+Mybatis3.1(SSI框架)开发整个jar包

    基于struts2.2+spring3.1+Mybatis3.1(SSI框架)框架开发下的JAR包整合!

    struts2.1.8+spring3.0+ibatis2.3优雅框架

    Struts2.1.8、Spring3.0和iBatis2.3是经典的Java Web开发框架组合,它们各自承担着不同的职责,协同工作以构建高效、可维护的Web应用程序。接下来,我们将深入探讨这三个框架的核心特性以及它们如何优雅地整合在一起...

    struts2+spring+ibatis+jquery ajax的登陆注册实时验证

    Struts2、Spring、iBatis和jQuery AJAX是Java Web开发中的四大核心技术,它们共同构建了一个功能强大的MVC(模型-视图-控制器)架构。本文将深入探讨这些技术在登录注册系统中的应用。 首先,Struts2是Apache基金会...

    struts2+spring2.5+ibatis2.3框架整合开发.doc

    1. **添加依赖库**:确保项目中包含了Struts2、Spring和iBatis的相应版本JAR包,以及它们的整合所需的依赖。 2. **配置web.xml**:配置Spring的`ContextLoaderListener`,指定Spring的配置文件位置。配置Struts2的`...

    struts2+spring+ibatis做的增删改查的小例子

    Struts2、Spring和iBatis是Java Web开发中三个非常重要的开源框架,它们的集成应用,即SSI2(Struts2 + Spring + iBatis)整合,是构建企业级应用的常见方式。这个"struts2+spring+ibatis做的增删改查的小例子"是一...

    SSI--struts2+spring2.5+ibatis项目实例

    **SSI(Struts2 + Spring2.5 + iBatis)项目实例详解** SSI,即Struts2、Spring和iBatis的组合,是Java Web开发中常见的技术栈,用于构建高效、灵活的企业级应用程序。这个项目实例展示了如何将这三个框架集成到...

Global site tag (gtag.js) - Google Analytics