需要改的是Dao的实现类,View和Service层不用动
两种方式:
mapper 动态代理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册数据源:C3P0 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 注册属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注册SessionFactory-->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- 生成Dao的代理对象 -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="mySqlSessionFactory"/>
<property name="mapperInterface" value="com.bjpowernode.dao.IStudentDao"/>
</bean>
<!-- 注册Service -->
<bean id="studentService" class="com.bjpowernode.service.StudentServiceImpl">
<property name="dao" ref="studentDao"/>
</bean>
</beans>
基于扫描的动态mapper代理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册数据源:C3P0 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 注册属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注册sqlSessionFactory-->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- 生成Dao的代理对象:当前配置会为指定的基本包中所有的接口生成代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/>
<property name="basePackage" value="com.bjpowernode.dao"/>
</bean>
<!-- 注册Service -->
<bean id="studentService" class="com.bjpowernode.service.StudentServiceImpl">
<!--这里的Dao的注入需要使用ref属性,且其值为接口的简单类名 -->
<property name="dao" ref="IStudentDao"/>
</bean>
</beans>
IStudentDao.xml(mapper.xml)
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjpowernode.dao.IStudentDao">
<insert id="insertStudent">
insert into student(name,age) values(#{name},#{age})
</insert>
<delete id="deleteById">
delete from student where id = #{id}
</delete>
<update id="updateStudent">
update student set name=#{name},age=#{age} where id=#{id}
</update>
<select id="selectAllStudents" resultType="Student">
select id,name,age from student
</select>
<select id="selectStudentById" resultType="Student">
select id,name,age from student where id=#{xxx}
</select>
</mapper>
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.bjpowernode.beans"/>
</typeAliases>
<mappers>
<package name="com.bjpowernode.dao"/>
</mappers>
</configuration>