spring和ibatis的整合

1.在applicationContext.xml中加入以下代码:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	    <property name="url" value="jdbc:mysql://localhost:3306/spmanager"/>
	    <property name="username" value="root"/>
	    <property name="password" value="root"/>
  	</bean>
	
	<!-- Spring提供的iBatis的SqlMap配置-->
	 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
	   <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
	   <property name="dataSource" ref="dataSource"/>
	 </bean>


将数据库的连接交由spring进行管理,而不用将其写在SqlMapConfig.xml中
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

 
  <sqlMap resource="com/xx/demo/Users.xml"/>

</sqlMapConfig>



2.在Dao层的代码中将Dao类继承Spring中的SqlMapClientDaoSupport,然后调用其的getSqlMapClientTemplate():
public class UserDaoImpl extends SqlMapClientDaoSupport  implements UserDao{

	public void add(Users users) {
		getSqlMapClientTemplate().update("insertUsers", users);
	}

}

你可能感兴趣的:(DAO,spring,sql,xml,ibatis)