接着iBATIS的入门实例来说,ibatis与Spring的整合其实跟hibernate与Spring的整合大同小异,都是一个模子的,回想hibernate整合spring:主要是在spring中配置,需要数据源,需要hibernate配置文件,需要sessionFactory。iBATIS走的路线完全一致,直接看配置就明白
先是src/sqlmap.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/SysDeliverCompany.xml" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/SysArea.xml" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/SmsActivityExpense.xml" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/deptMgr.xml" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/userMgr.xml" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/SysCost.xml" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/orders.xml" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/privilege.xml" />
<sqlMap resource="com/javacrazyer/myweb/ibatis/app.xml" />
</sqlMapConfig>
很显然这个配置里没有配数据源,配置到spring即可
applictionContext.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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--数据源,用的是spring容器自带的数据源,-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost:1433;databaseName=dbName
</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>123</value>
</property>
<property name="connectionProperties">
<props>
<!--
<prop key="useUnicode">true</prop>
<prop key="characterEncoding">utf-8</prop>
连接池中连接用完时,新的请求等待时间,毫秒,-1表示无限等待
<prop key="maxWait">1000</prop>
-->
<!-- 是否清理空闲连接 -->
<prop key="removeAbandoned">true</prop>
<!-- 活动连接的最大空闲时间,秒 -->
<prop key="removeAbandonedTimeout">60</prop>
<!-- 初始连接数量 -->
<prop key="initialSize">3</prop>
<!-- 连接池支持的最大连接数,0表示无限制 -->
<prop key="maxActive">50</prop>
<!-- 连接池中最多可空闲maxIdle个连接,0表示无限制 -->
<prop key="maxIdle">10</prop>
<!-- 连接池中最小空闲maxIdle个连接 -->
<prop key="minIdle">2</prop>
</props>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sqlmap.xml" />
<!-- 从IBATIS基本示例可以看到,sqlmap.xml中是应该配置dataSource的,不过没配置也没关系,可以在spring中继续配 -->
<property name="dataSource" ref="dataSource" />
<!--
<property name="useTransactionAwareDataSource"
value="true"></property>
-->
</bean>
<!--根据sqlMapClien创建一个SqlMapClient模版类-->
<!-- 那么这个sqlMapClientTemplate就相当于sessionFactory获取的session -->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
</beans>
然后在需要的类写上个private SqlMapClientTemplate sqlMapClientTemplate 当做字段并提供SETTER方法,之后在applicationContext.xml中注入sqlMapClientTemplate即可直接使用了,具体的方法与入门实例中sqlMapper的方
法一样
相关推荐
ibatis与spring 框架整合
一个iBatis与Spring整合的例子一个iBatis与Spring整合的例子一个iBatis与Spring整合的例子
iBatis和Spring整合 iBatis和Spring整合
ibatis与spring的整合,文档doc格式,学习可用
iBatis和Spring整合的详细例子,数据库用的是mysql,开发环境是Eclipse3.2。
Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。 与hibernate类似,Spring 提供了SqlMapClientDaoSupport对象,我们...
Ibatis和Spring整合例子,实现增删改查功能.
spring与ibatis整合集成实例 spring与ibatis整合集成实例
ibatis与spring整合,内附文档
jbpm4.4 ibatis-spring 整合
ibatis与Spring的整合,包含基本操作功能及测试代码。
Spring与iBATIS的集成 iBATIS似乎已远离众说纷纭的OR框架之列,通常人们对非常流行的Hibernate情有独钟。但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比...
使用ibatis+spring+struts2 做的开发例子 帮助大家一起学习ibatis的整合
整合了ibatis和spring 采用了数据源的配置 ibatis有详细的解释
最近想在最新的Spring5.0中集成ibatis(不是mybatis),发现已经不在支持SqlmapClientTemplate和SqlmapClientFactoryBean,于是搞了这个工具jar来进行支持如下配置 <bean id="sqlMapClient" class="org.spring...
ibatis和Spring整合的详细例子 iBatis和Spring整合的详细例子,数据库用的是mysql,开发环境是Eclipse3.2。 1、首先把用到的包导入进来: spring-framework-1.2.7.jar iBATIS_DBL-2.1.7.597.jar mysql-...
spring3.2与Ibatis整合,在servlet中获取bean
自己写的一个ibatis与spring进行整合的demo 供各位参考
博文链接:https://liran-email.iteye.com/blog/227498
ibatis+Spring+struts2整合实例