spring-mybatis —— MapperFactoryBean之数据映射文件 水深无声 2021-06-24 14:36 328阅读 0赞 为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象(DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现——MapperFactoryBean。这个类可以让你直接注入数据映射器接口到你的 service 层bean 中。当使用映射器时,你仅仅如调用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring将会为你创建代理。同样,MapperFactoryBean创建的代理控制开放和关闭 session。 在使用MapperFactoryBean时,可以通过映射接口方式提供sql语句,也可以通过映射文件提供sql语句。下面讲解如何使用映射文件提供sql 1、spring.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <context:annotation-config /> <!-- 扫描service、dao组件 --> <context:component-scan base-package="cn.edu.nuc" /> <!-- 分解配置 jdbc.properites --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 数据源c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="${c3p0.pool.size.max}" /> <property name="minPoolSize" value="${c3p0.pool.size.min}" /> <property name="initialPoolSize" value="${c3p0.pool.size.ini}" /> <property name="acquireIncrement" value="${c3p0.pool.size.increment}" /> </bean> <!--MapperFactoryBean 使用mapper的xml配置文件sql方式 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/configuration.xml"/> <property name="mapperLocations" value="classpath*:cn/edu/nuc/map/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <span style="white-space:pre"> </span> <!-- 指定扫描的包名 <span style="white-space:pre"> </span> 如果扫描多个包,每个包中间使用半角逗号分隔 <span style="white-space:pre"> </span> --> <span style="white-space:pre"> </span> <property name="basePackage" value="cn.edu.nuc.map"/> <span style="white-space:pre"> </span> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <span style="white-space:pre"> </span></bean> </beans> <?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> <settings> <!-- changes from the defaults for testing --> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> <setting name="lazyLoadingEnabled" value="false" /> <setting name="logImpl" value="LOG4J" /> </settings> <mappers> </mappers> </configuration> 2、映射接口: package cn.edu.nuc.map; import org.springframework.stereotype.Component; import cn.edu.nuc.bean.User; @Component("UserMapper") public interface UserMapper1 { User findUserById(long id) throws Exception; } 3、映射文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="cn.edu.nuc.map.UserMapper1"> <resultMap type="cn.edu.nuc.bean.User" id="userMap"> <id property="id" column="id" /> <result property="name" column="name" /> </resultMap> <select id="findUserById" parameterType="long" resultMap="userMap"> select * from user where id = #{id} </select> </mapper> 4、测试: import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.edu.nuc.bean.User; import cn.edu.nuc.map.UserMapper1; public class UserServiceMapTest { private ApplicationContext applicationContext; //在setUp这个方法得到spring容器 @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml"); } @Test public void testFindUserById() throws Exception { UserMapper1 userMapper = (UserMapper1)applicationContext.getBean("UserMapper"); //调用userMapper的方法 User user = userMapper.findUserById(1); System.out.println(user.getName()); } } 使用这种方式时,一定要注意一下几点: 1)Mapper映射文件中:namespace必须和数据映射器接口UserMapper的包名一致; 2)Mapper映射文件中:sql的id必须和数据映射器接口UserMapper的方法名一致; 参考:[http://www.oschina.net/question/113302\_228910?fromerr=dJY106iE][http_www.oschina.net_question_113302_228910_fromerr_dJY106iE] [http_www.oschina.net_question_113302_228910_fromerr_dJY106iE]: http://www.oschina.net/question/113302_228910?fromerr=dJY106iE
还没有评论,来说两句吧...