spring,ibatis整合

这里我只是用到了spring的ioc,dao访问代码都是用的ibatis的api,没有用spring提供的支持库。

数据源配置,META-INF下的context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource
    name="jdbc/ds"
    type="javax.sql.DataSource"
    driverClassName="oracle.jdbc.driver.OracleDriver"
    password="billing"
    maxIdle="2"
    maxWait="5000"
    username="billing"
    url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
    maxActive="10"/>
</Context>

 

 spring配置文件,classpath根路径下,src目录下面就可以了

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="jndiFacroty"
		class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
		<property name="jndiName">
			<value><![CDATA[java:comp/env/jdbc/ds]]></value>
		</property>
	</bean>

	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" scope="singleton">
		<property name="configLocation"
			value="samwoo/sms/db/sqlmap/SqlMapConfigExample.xml" />
		<property name="dataSource" ref="jndiFacroty" />
	</bean>

	<bean id="operDao" class="samwoo.sms.db.dao.SysOperDAOImpl">
		<constructor-arg ref="sqlMapClient"></constructor-arg>
	</bean>

	<bean id="userDao" class="samwoo.sms.db.dao.SysUserDAOImpl">
		<constructor-arg ref="sqlMapClient"></constructor-arg>
	</bean>
</beans>

 ibatis文件配置,可以将ibator生成的代码放在另一个工程中,利用fatjar将代码打包成jar,供其他工程引用

<?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">
<!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>
	<properties resource="samwoo/sms/db/sqlmap/SqlMapConfig.properties" />

	<settings cacheModelsEnabled="true" enhancementEnabled="true"
		lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
		maxTransactions="5" useStatementNamespaces="true" />

	<typeAlias alias="order" type="testdomain.Order" />
	
	<!--
		<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
		<property name="JDBC.Driver" value="${jdbc.driver}" />
		<property name="JDBC.ConnectionURL" value="${jdbc.url}" />
		<property name="JDBC.Username" value="${jdbc.username}" />
		<property name="JDBC.Password" value="${jdbc.password}" />
		</dataSource> 
		
		<dataSource type="jndi">
		<property name="DataSource" value="java:comp/env/jdbc/ds" />
		</dataSource>
		</transactionManager>
	-->
	
	<sqlMap resource="${sqlmap.path}/BILLING_COLM_OPER_SqlMap.xml" />
	<sqlMap resource="${sqlmap.path}/BILLING_COLM_ROLE_SqlMap.xml" />
	<sqlMap resource="${sqlmap.path}/BILLING_SYS_COLM_SqlMap.xml" />
	<sqlMap resource="${sqlmap.path}/BILLING_SYS_OPER_SqlMap.xml" />
	<sqlMap resource="${sqlmap.path}/BILLING_SYS_ROLE_SqlMap.xml" />
	<sqlMap resource="${sqlmap.path}/BILLING_SYS_USER_SqlMap.xml" />
	<sqlMap resource="${sqlmap.path}/SysUserSqlMap.xml" />


</sqlMapConfig>

 

包装的BeanFactory

package samwoo.sms;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class BeanFacroty {
	private static BeanFactory factory = null;

	public static Object getBean(String beanName) {
		if (factory == null) {
			Resource res = new ClassPathResource("applicationContext.xml");
			factory = new XmlBeanFactory(res);
		}

		return factory.getBean(beanName);
	}
}

 

实例化dao

SysUserDAO userDao = (SysUserDAO) BeanFacroty.getBean("userDao");

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值