Spring——第五章 Spring与Mybatis

本文详细介绍了在MyBatis中使用动态代理生成DAO层接口实现的方法,包括通过MapperFactoryBean和MapperScannerConfigurer两种方式,以及如何配置数据源、SessionFactory和Service层,为读者提供了实现DAO层动态代理的全面指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要改的是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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值