首页 > 编程语言 > 详细

Spring学习(七)spring整合mybatis

时间:2015-08-27 18:46:13      阅读:236      评论:0      收藏:0      [点我收藏+]

相对于mybatis的平常写法,spring中在使用mybatis时,不需要mybatis-config.xml配置,以及MybatisFactory工厂,在applicationContext.xml中配置即可。
还是使用上次的案例:mybatis传送门
附上applicationContext.xml:

<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-3.2.xsd">

    <!-- 
        在ioc容器中配置sqlSessionFactory
        使用SqlSessionFactoryBean工厂bean
            1 配置数据源
            2 配置映射文件  
                注意classpath前缀
                每在工程中添加一个映射文件,需要在list中添加一个value元素
     -->
     <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="ds"></property>
        <property name="mapperLocations">
            <list>
                <value>classpath:com/etoak/dao/CityDaoIf-mapper.xml</value>
            </list>
        </property>
     </bean>
     <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/yitu"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
     </bean>
     <!-- 
        配置接口对应的实例bean对象
        spring中为了配置接口实例,提供 MapperFactoryBean的工厂bean
      -->
      <bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="ssf"></property>
        <property name="mapperInterface" value="com.etoak.dao.CityDaoIf"></property>        
      </bean>
      <!-- 
        每在工程中添加一个接口,就需要在ioc容器中添加单独的bean节点使用mapperInterface实例化改接口
       -->

</beans>

版权声明:本文为博主原创文章,未经博主允许不得转载。

Spring学习(七)spring整合mybatis

原文:http://blog.csdn.net/frightingforambition/article/details/48030281

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!