ibatis与spring的整合:
在这里我举一个单表,单类的例子
1、新建一个example.Account.java类:
package example;
public class Account {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2、在example包下新建account.xml文件,内容如下:
引用
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Account">
<select id="getAccount" parameterClass="java.lang.String"
resultClass="example.Account">
select * from Account where username = #value#
</select>
<insert id="createAccount" parameterClass="example.Account">
insert into Account (username, password) values ( #username#,
#password# )
</insert>
</sqlMap>
3、编写sql-map-config.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>
<sqlMap resource="example/account.xml" />
</sqlMapConfig>
4、在spring的配置文件中配置数据源和ibatis
引用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.10.233:1521:java</value>
</property>
<property name="username">
<value>wytest</value>
</property>
<property name="password">
<value>wytest</value>
</property>
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="sql-map-config.xml" />
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean name="beanNameAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="transactionProxy" parent="beanNameAutoProxy">
<property name="beanNames">
<list>
<value></value>
</list>
</property>
<property name="interceptorNames">
<value>transactionInterceptor</value>
</property>
</bean>
</beans>
5、在数据库中建表Account,写测试类:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SqlMapClient sqlMap=(SqlMapClient) ctx.getBean("sqlMapClient");
sqlMap.getAccount();//需要传入的参数,这里就不写了。
sqlMap.createAccount();