整合struts2+spring2.5+ibatis

本文介绍如何将Spring框架与Ibatis整合,包括所需jar包、配置文件applicationContext.xml的具体设置,以及SqlMapConfig.xml和Users.xml的定义。文中详细展示了如何配置数据源、事务管理器,并通过注解方式实现类的注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据SSH整合的经验,先整合spring与ibatis(基于扫描+注解)

1.加入spring相关架包以及ibatis相关一个架包
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-logging-1.1.1.jar
log4j-1.2.14.jar
spring.jar
c3p0-0.9.1.2.jar

ibatis.rar

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:context="http://www.springframework.org/schema/context"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<!-- 通过注解方式把类交给spring -->
<context:component-scan base-package="com.ssi" />

<!-- 把Ibatis集成到spring -->
<!-- 减少数据库连接 采用数据源 C3P0比DBCP(dbcp+pool)效率高一点 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" dependency-check="none">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="25000" />
<property name="acquireIncrement" value="1" />
<property name="acquireRetryDelay" value="1000" />
<property name="acquireRetryAttempts" value="30" />
<property name="initialPoolSize" value="3" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="testConnectionOnCheckin" value="true" />
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:SqlMapConfig.xml</value>
</property>
<property name="useTransactionAwareDataSource">
<value>true</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>

<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>

<!--Spring很重要的一个服务:配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 使用基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>


SqlMapConfig.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">

<sqlMapConfig>
<settings cacheModelsEnabled="true" useStatementNamespaces="true"/>

<!-- 此处开始添加SqlMap配置文件 -->
<sqlMap resource="com/ssi/bean/Users.xml" />

</sqlMapConfig>



Users.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>
<!-- 该属性是为了减少书写com.elifefly.User的次数而已 -->
<typeAlias alias="Users" type="com.ssi.bean.Users" />

<!-- 条件查询 parameterClass标识传入参数类型 忽略大小写 -->
<select id="selectUserById" parameterClass="int" resultClass="Users">
select * from users where id=#id#
</select>

<!-- 带多个参数 查询-->
<parameterMap class="java.util.HashMap" id="parameterMap">
<parameter property="name" />
<parameter property="password" />
</parameterMap>
<select id="selectUserByMap" parameterMap="parameterMap"
resultClass="Users">
select * from users where name=? and password=?
</select>
</sqlMap>


详细下载实例....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值