Struts2+Spring+Ibatis集成合并
上一篇博客讲述了Struts2+Spring的集成合并,主要是利用了一个中间jar包,这篇博客在加上Ibatis持久层框架,三个框架进行合并。其中Struts2和Spring部分和前边的一样,主要是讲解Spring和Ibatis之间的合并,这里也涉及到Spring的AOP编程思想,声明式事务的使用。
一,看一下分工吧:
Struts2:负责流程控制,主要针对的是从JSP页面到action类这一块的步骤。
Spring:负责各个类,对象的创建,包括action,service,dao,数据连接对象,Ibatis框架的核心对象sqlMapClient,Spring自身的事务处理扩展对象transactionManager。
Ibatis:负责对JDBC的封装,简化访问数据的程序。
二,环境的搭建:
1,需要考入的jar包:
commons-fileupload-1.2.1.jar(Struts2:文件上传)
commons-io-1.3.2.jar(Struts2:文件上传)
freemarker-2.3.15.jar(Struts2:视图展现技术)
ognl-2.7.3.jar(Struts2:对象图形导航语言,用于做数据操作)
struts2-core-2.1.8.1.jar(Struts2:核心jar包)
xwork-core-2.1.6.jar(Struts2:webwork框架的核心jar包)
commons-logging.jar(Spring:日志输出操作,实现日志输出的转换)
junit-3.8.2.jar(非必须:单元测试jar包)
log4j-1.2.15.jar(非必须:日志输出jar包)
spring.jar(Spring:核心jar包)
struts2-spring-plugin-2.1.8.1.jar(Struts2和Spring的合并jar包)
c3p0-0.9.1.2.jar(做数据源操作的jar包)
ibatis-2.3.4.726.jar(Ibatis:核心jar包)
ojdbc14.jar(oracle数据库的连接jar包)
aspectjrt.jar(Spring:支持AOP功能的jar包)
aspectjweaver.jar(Spring:支持AOP功能的jar包)
cglib-nodep-2.1_3.jar(Spring:实现基于继承的动态代理)
2,拷贝进去的配置文件:
Struts2:struts.xml
Spring:applicationContext.xml
Ibatis:SqlMapConfig.xml 和SqlMap.xml
日志输出的属性文件:log4j.properties
配置数据库连接信息:db.properties
三,各个配置文件的编写:
1,struts.xml中,主要用来配置action类,
<package name="system" namespace="/system" extends="struts-default"> <action name="insertUser" class="userAction" method="insert">
<result name="success" type="redirect">/userlist.jsp</result>
<result name="error" type="redirect">/error.jsp</result>
</action>
</package>
2,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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="loginAction" class="com.ljh.action.LoginAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<!-- action层类的设置,关联service -->
<bean id="userAction" class="com.ljh.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<!-- service层类的设置,关联dao -->
<bean id="userService" class="com.ljh.service.UserService" >
<property name="userDao" ref="userDao"></property>
</bean>
<!-- dao层类的设置 ,关联sqlMapClient-->
<bean id="userDao" class="com.ljh.dao.UserDao">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<!-- 表示把属性资源文件的信息加载到Spring环境中进行利用 -->
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<!-- 配置C3P0数据源连接池,通过读取属性文件 -->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driverClass}"></property>
</bean>
<!-- IBatis核心对象sqlMapClient的声明 ,通过工厂SqlMapClientFactoryBean和IBatis核心配置文件-->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="c3p0"></property>
<property name="configLocations">
<list>
<value>classpath:SqlMapConfig.xml</value>
</list>
</property>
</bean>
<!-- 配置功能扩展对象 - 事务管理,通过c3p0 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0"></property>
</bean>
<!-- 声明事务管理AOP功能 -->
<aop:config>
<aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.ljh.service.*.*(..))"/>
</aop:config>
<!--事务的配置-->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
3,SqlMapConfig.xml
<sqlMapConfig>
<!-- 只需配置映射文件即可 -->
<sqlMap resource="com/ljh/bean/User.xml" /> </sqlMapConfig>
4,User.xml(SqlMap.xml)
5,属性文件db.properties:
url=jdbc:oracle:thin:@127.0.0.1:1521:ljh
driverClass=oracle.jdbc.driver.OracleDriver
username=scott
password=ti
四,对于jsp和其他三层这里不再给出。但是提醒一点,service中需要将Exception剖出,这样才会被框架捕捉到,事务才能起作用。Dao层可以直接使用sqlMapClient来调用其中的增删改查的方法。
这样我们的三个框架的合并就算完成,在实际中,根据业务进行相关类的添加,相关配置文件的补充即可。总之多运用。
Struts2+Spring+Ibatis集成合并的更多相关文章
- Eclipse rap 富客户端开发总结(11) : rcp/rap与spring ibatis集成
1. rcp/rap 与 spring 集成 Activator 是rcp/rap 启动时需要加载的类, 只需要加载一遍,所以与spring 集成的时候一般是在这个类里面加载spring 的Appli ...
- struts2,spring,ibatis学习
1.1 什么是struts2? MVC思想给网站设计带来了巨大的好处,但是MVC毕竟只是一种思想,不同的程序员写出来的基于MVC思想的应用,风格可能不一样.影响程序的标准化,Struts是为了规范MV ...
- zTree的调用设使用(跨两个系统,两类技术实现的项目案例SpringMVC+Spring+MyBatis和Struts2+Spring+ibatis框架组合)
1.从zTree官网上下载zTree的包,zTree的官方网址是:http://www.ztree.me/v3/main.php#_zTreeInfo 2.引入zTree所需的依赖,例如(jQuery ...
- Spring+Ibatis集成开发实例
首先简历数据库demo(本文选mysql) 数据库脚本: CREATE TABLE `ibatis` ( `id` varchar(20) NOT NULL, `name` varchar(20) ...
- 把elipse非maven的Struts2+Spring+Ibatis项目导入Idea中
1.按图示操作 2.选中自己要得到的项目 3.之后设定得到的项目放在哪里 项目得到之后,对项目点击右键Open Module Settings,点击Project,设置生成的编译文件存储路径 4.单击 ...
- Struts2+Spring集成合并
前边单独总结了Struts2,Spring和Ibaits框架了,那么怎么结合使用呢?这次先来看一下Sturts2和Spring的集成合并.其实挺简单的,就是导入各自的jar包以及连接彼此的jar包,分 ...
- Struts2+Spring+Hibernate 三大框架的合并集成
这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样. 首先看一 ...
- 集成Struts2+Spring+Hibernate_两种方案
集成Struts2+Spring+Hibernate 第一种方案:让Spring创建Struts2的Action,不让Spring完全管理Struts2的Action Struts2 Act ...
- spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingExcept ...
随机推荐
- JSTL 格式化输出 Calendar
今天遇到一个jstl在页面输出Calendar对象的问题,网上各种百度都说不能直接格式化,但是经过自己的尝试,原来是可以直接格式化的. 做个备忘吧. 对象的createTime字段类型是Calenda ...
- 关闭SQL Server 数据库所有使用连接
使用存储过程终止:在查询分析器下创建终止数据库所有接连的存储过程,通过调用该存储过程可以关闭所有使用该数据库的连接操作.--创建终止使用数据库下所有进程的存储过程,参数为数据库名称use maste ...
- VB中DateDiff 函数解释
VB中DateDiff 函数使用方法 DateDiff (interval, Date1 , Date2[,firstweekofyear[,firstweekofyear]]) 返回一个Varia ...
- Ext Radio 取消选中
今天,做项目的时候遇到了要吧Ext Radio单选按钮取消选中状态,由于没有在formpanel中写, 导致不能用reset()方法,试了各种方法,最后这样写管用. radio1.setValue(f ...
- Android系统,动态找出一个包下所有的类
最近在写一个android应用,由于针对不同机型功能很不同,为了隔离变化,希望将各项功能插件化,通过编译开关来控制不同版本的功能,而不是在代码中通过逻辑来判断. 我想了一个办法,用表驱动的方法,结合插 ...
- 如何在程序退出的时候清除activity栈
在公司里接手了一个后期的项目,由于项目前期对activity栈管理的不够谨慎,所以导致了在某些情况下程序退出的时候没有将activity栈清除掉.在网上找到的无非就是那几种例子,都不是最好的解决办法. ...
- js的原型继承小结
考虑:有一个想要复用的对象,并且想要创建的第二个对象需要从第一个对对象中获取其功能. 实现如下: //要继承的对象 var parent = { name:"Papa" }; // ...
- Linux下安装MySQL步骤
1.下载安装包(这里是32位的): MySQL-client-5.6.13-1.rhel5.i386.rpm MySQL-server-5.6.13-1.rhel5.i386.rpm 2.安装 rpm ...
- Android网络连接的两种方法:apache client和httpurlconnection的比较
另见http://blog.csdn.net/mingli198611/article/details/8766585 在官方blog中,android工程师谈到了如何去选择apache client ...
- Django中文无法转换成latin-1编码的解决方案
在Ubuntu上用Django做Web开发的时候遇到了中文保存到Cookie无法解析的问题,经过了下面几个步骤终于把问题解决了: 修改/usr/lib/python3.4/wsgiref/header ...