Spring两种引入mybatis配置文件方法

Spring两种引入MyBatis配置文件方法

1、(推荐)单独新建一个mybatis-config.xml文件,然后依赖注入进Spring容器里

spring容器里引入mybatis-config,mybatis-config里有Mapper映射文件地址就直接获取了
mybatis-config.xml:



<configuration>
    <settings>
        
        <setting name="logImpl" value="STDOUT_LOGGING" />
    settings>
    <typeAliases>
    	
        <package name="cn.yznu.pojo" />
    typeAliases>
    
    <mappers>
    	
        <mapper resource="mappers/UserMapper.xml" />
    mappers>
configuration>

spring.xml:

引入mybatis配置文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    bean>
    <context:component-scan base-package="cn.yznu"/>
beans>

2、直接在Spring容器里用注入

直接获取Mapper映射文件地址,就不需要写mybatis-config.xml文件

引入Mapper映射文件地址

spring.xml:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
    bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    bean>
    <context:component-scan base-package="cn.yznu"/>
beans>

你可能感兴趣的:(JavaEE,mybatis,spring)