先加入spring的包,建立POJO实体。然后写一个配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<resultMap class="com.spring.entity.Emp" id="e-map">
<result property="empno" column="empno"/>
<result property="ename" column="ename"/>
<result property = "dept" column="deptno" select="getDeptById"/>
</resultMap>
<select id="getDeptById" parameterClass="int" resultClass="com.spring.entity.Dept" >
select * from dept where deptno=#deptno#
</select>
<resultMap class="com.spring.entity.Dept" id="d-map">
<result property="deptno" column="deptno"/>
<result property="dname" column="dname"/>
<result property = "loc" column="loc"/>
<result property="emps" column="deptno" select="getEmpsByDept"/>
</resultMap>
<select id="getAllEmp" parameterClass="int" resultClass="com.spring.entity.Emp" >
select * from emp
</select>
<insert id="insertEmp" parameterClass="com.spring.entity.Emp">
insert into emp values(#dept.deptno#,#ename#)
</insert>
<insert id="insertDept" parameterClass="com.spring.entity.Dept">
insert into dept values(#dname#,#loc#)
</insert>
<select id="selectDeptEmp" parameterClass="com.spring.entity.Dept" resultClass="com.spring.entity.Emp">
select * from emp where deptno=#deptno#
</select>
<update id="updateEmp" >
update emp set deptno=3 where deptno=1
</update>
<delete id="deleteDept">
delete from dept where deptno=1
</delete>
</sqlMap>
这个xml文件负责和数据库打交道,ibatis文件配置如下:
<?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/spring/entity/emp.xml"/>
</sqlMapConfig>
然后在spring中的配置文件applicationContext.xml中配置如下的文件
<?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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=mydb"></property>
<property name="username" value="sa"></property>
<property name="password" value=""></property>
</bean>
<!-- sqlMapClient -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="file:src/ibatis.xml"></property>
</bean>
<bean id="EmpDAOImpl" class="com.spring.dao.EmpDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient">
</property>
</bean>
<bean id="EmpImpl" class="com.spring.biz.EmpImpl">
<property name="empDao" ref="EmpDAOImpl">
</property>
</bean>
<bean id="DeptDAOImpl" class="com.spring.dao.DeptDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient">
</property>
</bean>
<bean id="DeptServiceImpl" class="com.spring.biz.DeptServiceImpl">
<property name="deptDao" ref="DeptDAOImpl">
</property>
</bean>
<!-- 事务管理器 -->
<bean id="ibatisManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务拦截器 -->
<bean id="trInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="ibatisManager"></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 自动代理 -->
<bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*DAO</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>trInterceptor</value>
</list>
</property>
</bean>
</beans>
就可以调用了。