ibatis in Spring(简单配置)
(实现Spring与ibaties的简单)
这里我们重点探讨Spring框架下的ibatis应用,特别是在容器事务管理模式下的ibatis应用开发。针对ibatis,Spring配置文件如下:
1、Ibatis-Context.xml:
(下面我将对本文档进行详细说明)
<?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"
destroy-method="close">
<property name="driverClassName">
<value>net.sourceforge.jtds.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:jtds:sqlserver://127.0.0.1:1433/Sample</value>
</property>
<property name="username">
<value>test</value>
</property>
<property name="password">
<value>changeit</value>
</property>
</bean>
这里的sqlMapClient节点实际上配置了一个sqlMapClient的创
建工厂类。 configLocation
属性配置了ibatis映射文件的名称。
<beanid="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"> <value>SqlMapConfig.xml</value>
</property>
</bean>
transactionManager节点, 这里我们的transactionManager配置与之前JDBC示例中相同,都采用了DataSourceTransactionManager
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactio
nManager">
<property name="dataSource"><ref
local="dataSource"/></property>
</bean>
userDAO节点
对应的,UserDAO需要配置两个属性,sqlMapClient和DataSource,
sqlMapClient将从指定的DataSource中获取数据库连接。
<bean id="userDAO" class="net.xiaxin.dao.UserDAO">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="sqlMapClient">
<ref local="sqlMapClient" />
</property>
</bean>
下面是事务管理
<bean id="userDAOProxy"
class="org.springframework.transaction.interceptor.TransactionPro
xyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="userDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
2、sqlMapConfig.xml:
<sqlMapConfig>
<sqlMap resource="net/xiaxin/dao/entity/user.xml"/>
</sqlMapConfig>
3、net/xiaxin/dao/entity/user .xml
<sqlMap namespace="User">
<typeAlias alias="user" type="net.xiaxin.dao.entity.User" />
<insert id="insertUser" parameterClass="user">
INSERT INTO users ( username, password) VALUES ( #username#,
#password# )
</insert>
</sqlMap>
4、UserDAO.java
public class UserDAO extends SqlMapClientDaoSupport implements
IUserDAO {
public void insertUser(User user) {
getSqlMapClientTemplate().update("insertUser", user);
}
}
5、User .java
public class User {
public Integer id;
public String username; public String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
扼要
6、测试代码
InputStream is = new FileInputStream("Ibatis-Context.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);
IUserDAO userdao = (IUserDAO)factory.getBean("userDAOProxy");
User user = new User();
user.setUsername("Sofia");
user.setPassword("mypass");
userdao.insertUser(user);
posted on 2009-03-24 13:26
重庆理工小子 阅读(1165)
评论(0) 编辑 收藏 所属分类:
Spring2