spring与mybatis整合配置文件详解

一 : jar包简单列举 :

1, mybatis官方提供与mybatis与spring整合jar包java

2, spring相关jar包    3,mybatis相关包    4,c3p0链接池     5, MySQL数据库驱动mysql

二 : mybatis配置文件

在classpath下建立mybatis-config.xmlspring

在与spring整合前, mybatis配置文件中配置 数据库链接池 , 事物管理器, 映射文件 等 . 在与spring整合后其中数据库链接池, 事物管理器交给spring进行管理sql

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper .//EN" 
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userMapper">
    <select id="findUserById" parameterType="int" resultType="cn.sang.model.User">
        SELECT * FROM USER WHERE id = #{id}
    </select>
</mapper>

三 : spring配置文件

在classpath下建立applicationContext.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"
    xmlns:task="http://www.springframework.org/schema/task"
    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
        http://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- 注解式扫描基本包实例化类    spring扫描器只扫描Service层,Controller层由springMvc负责扫描,
Dao层有Mybatis复制扫描 -->
           <context:component-scan base-package="cn.sang.service.imp"/>
           
       <!-- 读取properties配置文件  扩展性很差的方式 -->
           <context:property-placeholder location="classpath:jdbc.properties"/>
           
       <!-- 数据库链接池 -->
           <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
destroy-method="close">
            <!-- 驱动 -->
            <property name="driverClass" value="${driverClass}"/>
            <!-- url -->
            <property name="jdbcUrl" value="${jdbcUrl}"/>
            <!-- 用户名 -->
            <property name="user" value="${user}"/>
            <!-- 密码 -->
            <property name="password" value="${password}"/>
           </bean>
           
       <!-- 让spring管理sqlSessionfactory 使用myBatis和spring整合包中的 ,
在定义sqlSessionFactory时指定数据源dataSource和myBatis的配置文件 -->
           <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
               <!-- 数据库链接池 -->
            <property name="dataSource" ref="dataSource" />
            <!-- 加载myBatis的全局配置文件 -->
            <property name="configLocation" value="classpath:mybatis-config.xml" />
           </bean>
           
    <!-- 配置数据源事物  注解式开发 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    <!-- 开启注解驱动 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
           
       <!-- Dao实现类开发 -->
       <!-- 为何要在此处配置dao的实例化,由于spring扫描包实例化并无扫描dao层,须要mybatis进行扫描,
因此此处须要进行实例化的配置 -->
       <!-- 此处配置后,dao实现类被实例化,能够在srevice能扫描到地方使用注解autowraid的方式注入使用-->
           <bean id="userDao" class="cn.sang.dao.imp.UserDaoImp">
               <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
           </bean>
           
           
</beans>

须要配置内容以下 :spring-mvc

1, 事务管理

1.1 此处使用注解式事物管理,  分为两步 , (1配置数据源事物 2 开启注解驱动)mybatis

<!--配置数据源事物  注解式开发 -->

         <beanclass="org.springframework.jdbc.datasource.DataSourceTransactionManager">

              <propertyname="transactionManager"ref="dataSource"/>

         </bean>

     <!--开启注解驱动 -->

<tx:annotation-driven/>

2,  读取jdbc.properties文件 , 两种方式mvc

  2.1扩展性很差的方式app

   <!--加载properties配置文件 -->ui

        <context:property-placeholderlocation="classpath:jdbc.properties"/>

  2.2 扩展性好的方式

<!-- 读取properties文件 -->

 <beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <propertyname="locations">
                          <list>
                                  <value>classpath:jdbc.properties</value>
                          </list>
             </property>
</bean>

3, 数据库链接池

<!-- 数据库链接池 -->
           <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
destroy-method="close">
            <!-- 驱动 -->
            <property name="driverClass" value="${driverClass}"/>
            <!-- url -->
            <property name="jdbcUrl" value="${jdbcUrl}"/>
            <!-- 用户名 -->
            <property name="user" value="${user}"/>
            <!-- 密码 -->
            <property name="password" value="${password}"/>
           </bean>

4, 实例化sqlSessionFactory工厂

   做用 : 在未使用spring管理sqlSessionFactory以前,每次使用sqlSessionFactory都要在代码中读取mybatis配置文件使用sqlSessionFactoryBuilder类建立sqlSessionFactory,获取sqlSession . 使用spring配置文件管理sqlSessionFactory的建立后就能够省去重复建立sqlSessionFactory的代码

<!--让spring管理sqlSessionfactory使用myBatis和spring整合包中的 ,
在定义sqlSessionFactory时指定数据源dataSource和myBatis的配置文件 -->

         <bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

              <!-- 数据库链接池 -->

              <propertyname="dataSource"ref="dataSource"/>

              <!--加载myBatis的全局配置文件 -->

              <propertyname="configLocation"value="classpath:mybatis-config.xml"/>

          </bean>

5, 使用注解式实例化Service

    注意 : spring的扫描器只扫描Service层 , Dao层由Mybatis负责扫描 , 若是Dao层是接口则不须要扫描 .Controller层由SpringMvc负责扫描 , 因此在spring配置文件的扫描器要排除扫描Controller层

       <!-- 注解式扫描基本包实例化类   spring扫描器只扫描Service,Controller层由springMvc负责扫描,Dao层有Mybatis复制扫描 -->

        <context:component-scan base-package="cn.sang.service.imp"/>

6, 使用dao实现类开发

注解式开发中, spring的扫描器只扫描service层, springmvc扫描器扫描controller层 , mybatis扫描dao层 , 若是dao实现类没有被扫描 , 能够在spring配置文件中经过配置bean的方式, 进行实例化 , 而后在service层能够使用autowraid注解进行注入使用

<!-- Dao实现类开发 -->
       <!-- 为何要在此处配置dao的实例化,由于spring扫描包实例化并无扫描dao层,
须要mybatis进行扫描,因此此处须要进行实例化的配置 -->
       <!-- 此处配置后,dao实现类被实例化,能够在srevice能扫描到地方使用注解autowraid的方式注入使用-->
        <bean id="userDao" class="cn.sang.dao.imp.UserDaoImp">
             <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
相关文章
相关标签/搜索