Mybatis整合Spring


根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持。因此由Mybatis社区自己开发了一个Mybatis-Spring用来满足Mybatis用户整合Spring的需求。下面就将通过Mybatis-Spring来整合Mybatis跟Spring的用法做一个简单的介绍。

MapperFactoryBean

       首先,我们需要从Mybatis官网上下载Mybatis-Spring的jar包添加到我们项目的类路径下,当然也需要添加Mybatis的相关jar包和Spring的相关jar包。我们知道在Mybatis的所有操作都是基于一个SqlSession的,而SqlSession是由SqlSessionFactory来产生的,SqlSessionFactory又是由SqlSessionFactoryBuilder来生成的。但是Mybatis-Spring是基于SqlSessionFactoryBean的。在使用Mybatis-Spring的时候,我们也需要SqlSession,而且这个SqlSession是内嵌在程序中的,一般不需要我们直接访问。SqlSession也是由SqlSessionFactory来产生的,但是Mybatis-Spring给我们封装了一个SqlSessionFactoryBean,在这个bean里面还是通过SqlSessionFactoryBuilder来建立对应的SqlSessionFactory,进而获取到对应的SqlSession。通过SqlSessionFactoryBean我们可以通过对其指定一些属性来提供Mybatis的一些配置信息。所以接下来我们需要在Spring的applicationContext配置文件中定义一个SqlSessionFactoryBean。

 

Xml代码 复制代码  收藏代码
  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  2.        <property name="dataSource" ref="dataSource" />  
  3.        <property name="mapperLocations"  
  4.               value="classpath:com/tiantian/ckeditor/mybatis/mappers/*Mapper.xml" />  
  5.        <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model" />  
  6. </bean>  
 

       在定义SqlSessionFactoryBean的时候,dataSource属性是必须指定的,它表示用于连接数据库的数据源。当然,我们也可以指定一些其他的属性,下面简单列举几个:

  •  mapperLocations:它表示我们的Mapper文件存放的位置,当我们的Mapper文件跟对应的Mapper接口处于同一位置的时候可以不用指定该属性的值。
  • configLocation:用于指定Mybatis的配置文件位置。如果指定了该属性,那么会以该配置文件的内容作为配置信息构建对应的SqlSessionFactoryBuilder,但是后续属性指定的内容会覆盖该配置文件里面指定的对应内容。
  •  typeAliasesPackage:它一般对应我们的实体类所在的包,这个时候会自动取对应包中不包括包名的简单类名作为包括包名的别名。多个package之间可以用逗号或者分号等来进行分隔。
  •  typeAliases:数组类型,用来指定别名的。指定了这个属性后,Mybatis会把这个类型的短名称作为这个类型的别名,前提是该类上没有标注@Alias注解,否则将使用该注解对应的值作为此种类型的别名。
Xml代码 复制代码  收藏代码
  1. <property name="typeAliases">  
  2.     <array>  
  3.         <value>com.tiantian.mybatis.model.Blog</value>  
  4.         <value>com.tiantian.mybatis.model.Comment</value>  
  5.     </array>  
  6. </property>  
 
  •  plugins:数组类型,用来指定Mybatis的Interceptor。
  •  typeHandlersPackage:用来指定TypeHandler所在的包,如果指定了该属性,SqlSessionFactoryBean会自动把该包下面的类注册为对应的TypeHandler。多个package之间可以用逗号或者分号等来进行分隔。
  •  typeHandlers:数组类型,表示TypeHandler。

        接下来就是在Spring的applicationContext文件中定义我们想要的Mapper对象对应的MapperFactoryBean了。通过MapperFactoryBean可以获取到我们想要的Mapper对象。MapperFactoryBean实现了Spring的FactoryBean接口,所以MapperFactoryBean是通过FactoryBean接口中定义的getObject方法来获取对应的Mapper对象的。在定义一个MapperFactoryBean的时候有两个属性需要我们注入,一个是Mybatis-Spring用来生成实现了SqlSession接口的SqlSessionTemplate对象的sqlSessionFactory;另一个就是我们所要返回的对应的Mapper接口了。

       定义好相应Mapper接口对应的MapperFactoryBean之后,我们就可以把我们对应的Mapper接口注入到由Spring管理的bean对象中了,比如Service bean对象。这样当我们需要使用到相应的Mapper接口时,MapperFactoryBean会从它的getObject方法中获取对应的Mapper接口,而getObject内部还是通过我们注入的属性调用SqlSession接口的getMapper(Mapper接口)方法来返回对应的Mapper接口的。这样就通过把SqlSessionFactory和相应的Mapper接口交给Spring管理实现了Mybatis跟Spring的整合。

Spring的applicationContext.xml配置文件:

 

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.      http://www.springframework.org/schema/context  
  8.    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.      http://www.springframework.org/schema/mvc  
  10.      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  11.     <context:component-scan base-package="com.tiantian.mybatis"/>  
  12.     <context:property-placeholder location="classpath:config/jdbc.properties"/>  
  13.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  14.        destroy-method="close">  
  15.        <property name="driverClassName" value="${jdbc.driver}" />  
  16.        <property name="url" value="${jdbc.url}" />  
  17.        <property name="username" value="${jdbc.username}" />  
  18.        <property name="password" value="${jdbc.password}" />  
  19.     </bean>  
  20.    
  21.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  22.        <property name="dataSource" ref="dataSource" />  
  23.        <property name="mapperLocations" value="classpath:com/tiantian/mybatis/mapper/*.xml"/>  
  24.        <property name="typeAliasesPackage" value="com.tiantian.mybatis.model" />  
  25.     </bean>  
  26.    
  27.     <bean id="blogMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
  28.        <property name="mapperInterface"  
  29.            value="com.tiantian.mybatis.mapper.BlogMapper" />  
  30.        <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
  31.     </bean>  
  32.    
  33. </beans>  
 

BlogMapper.xml文件

 

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3.   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4.   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="com.tiantian.mybatis.mapper.BlogMapper">  
  6. <!--  新增记录  -->  
  7.     <insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id">  
  8.         insert into t_blog(title,content,owner) values(#{title},#{content},#{owner})  
  9.     </insert>  
  10. <!--  查询单条记录  -->  
  11.     <select id="selectBlog" parameterType="int" resultMap="BlogResult">  
  12.        select * from t_blog where id = #{id}  
  13.     </select>  
  14. <!--  修改记录  -->  
  15.     <update id="updateBlog" parameterType="Blog">  
  16.         update t_blog set title = #{title},content = #{content},owner = #{owner} where id = #{id}  
  17.     </update>  
  18. <!--  查询所有记录  -->  
  19.     <select id="selectAll" resultType="Blog">  
  20.         select * from t_blog  
  21.     </select>  
  22. <!--  删除记录  -->  
  23.     <delete id="deleteBlog" parameterType="int">  
  24.        delete from t_blog where id = #{id}  
  25.     </delete>  
  26.      
  27. </mapper>  
 
获取【下载地址】 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值