Java&JAVA EE

mybatis和Spring整合配置文件

2018-06-19  本文已影响6人  寒风凛凛

mybatis-config.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:context="http://www.springframework.org/schema/context"
    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-4.0.xsd">
    <!-- 
        "classpath:"要全小写 ,错误写法"classPath:"
    -->
    <context:property-placeholder
        location="classpath:dbcp.properties"/>
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- 
        SqlSessionFactoryBean:注入dataSource和部分mybatisConfig.xml
        没使用Spring获取SqlSessionFactory过程:
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        使用了build设计思想
     -->
    <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation"
            value="classpath:mybatis-cfg.xml"></property>
    </bean>
    <!-- 
        MapperScannerConfigurer:将Mapper接口转成Bean,可以直接使用ctx.getBean(IMapper.class)来获取
            basePackage:扫描位置
            sqlSessionFactoryBeanName:sqlSessionFactory的名字
            annotationClass:扫描有指定注解的Mapper接口
     -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="myTest.dao"></property>
        <property name="sqlSessionFactoryBeanName"
            value="sqlSessionFactory"></property>
        <property name="annotationClass"
            value="org.springframework.stereotype.Repository"></property>
    </bean>
</beans>

mybatis-config.xml文件

<?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>
        <!-- 插入空值时对应的java类 -->
        <setting name="jdbcTypeForNull" value="NULL"/>
        <!-- 开启全局延迟加载 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 当设置为‘true’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。-->
        <setting name="aggressiveLazyLoading" value="false"/>
        <!-- 开启二级缓存 -->
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <mappers>
        <package name="myTest.dao"/>
    </mappers>
</configuration>
上一篇下一篇

猜你喜欢

热点阅读