这里我只是用到了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");
407

被折叠的 条评论
为什么被折叠?



