MyBatis 实用篇(二)配置文件

MyBatis 实用篇(二)配置文件

一、全局配置

全局配置:http://www.mybatis.org/mybatis-3/zh/configuration.html

<?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>
    <!-- 1. 引入配置文件 .properties -->
    <properties resource="conf.properties"/>

    <!-- 2. settings 配置项 -->
    <settings>
        <!--开启数据库下划线转驼峰规则-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <!--开启分步查询的懒加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>

        <!--开启二级缓存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>

    <!-- 3. typeAliases 别名处理器,为 java 类型取别名 -->
    <typeAliases>
        <!-- 3.1 typeAlias: 为 java 类取别名
            type 为类的全路径,alias 默认为类名的小写 -->
        <!-- <typeAlias alias="User" type="com.github.binarylei.spring03.day0902.User"/> -->

        <!-- 3.2 package 为某个包下的所有类批量取别名,默认类名小写,注意别名不区分大小写 -->
        <package name="com.github.binarylei.mybatis.helloworld"/>

        <!-- 3.3 注解取别名 @Alias("user") -->
    </typeAliases>
    
    <!-- 4. typeHandlers 类型处理器 -->
    <typeHandlers/>

    <!-- 5. plugins 插件,如分布插件等 -->
    <!--<plugins>
        <plugin interceptor=""/>
    </plugins>-->

    <!-- 6. environments 数据库配置,mybatis 可以配置多种环境
        environment 配置一个具体的环境,必须有 transactionManager 和 dataSource 两个元素
            transactionManager 事务管理器
                type: JDBC(JdbcTransactionFactory)|MANAGED(ManagedTransactionFactory)
                        自定义事务管理器:实现 TransactionFactory 接口
            dataSource 数据源
                type: POOLEDJNDI(PooledDataSourceFactory)|UNPOOLEDJNDI(PooledDataSourceFactory)|JNDI(JndiDataSourceFactory)
                        自定义数据源:实现 DataSourceFactory 接口
        JDBC|MANAGED 等别名的注册在 org.apache.ibatis.session.Configuration 中定义
     -->
    <environments default="dev">
        <environment id="test">
            <!-- 配置事务管理 ,采用JDBC管理事务-->
            <transactionManager type="JDBC"/>
            <!-- POOLED是mybatis的 数据源 -->
            <!-- JNDI是基于tomcat的数据源 -->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>

        <environment id="dev">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 7. databaseIdProvider -->
    <databaseIdProvider type="DB_VENDOR">
        <property name="SQL Server" value="sqlserver"/>
        <property name="DB2" value="db2"/>
        <property name="Oracle" value="oracle"/>
        <property name="MySQL" value="mysql"/>
        <property name="PostgreSQL" value="postgresql"/>
        <property name="Derby" value="derby"/>
        <property name="HSQL" value="hsql"/>
        <property name="H2" value="h2"/>
    </databaseIdProvider>

    <!-- 8. 将 sql 注册到全局配置中 -->
    <mappers>
        <!--
            mapper: 注册一个 sql 映射
                7.1 resource: 引用 classpath 下的 sql 映射文件 eg: com/github/binarylei/mybatis/helloworld/UserMapper.xml
                7.2 url: 引用网络或磁盘路径下的 sql 映射文件 eg: file:///var/mybatis/UserMapper.xml
                7.3 class: 引用(注册)接口。
                    1. 有 sql 映射文件,映射文件名必须和接口同名,并且放在与接口同一目录下
                    2. 没有 sql 映射文件,所有的接口都写到注解上 eg: @select("select * from user")
         -->
        <!--<mapper resource="com/github/binarylei/mybatis/helloworld/UserMapper.xml"/>-->
        <!--<mapper class="com.github.binarylei.mybatis.helloworld.UserMapper"/>-->

        <!-- 7.4 批量包扫描 -->
        <package name="com.github.binarylei.mybatis"/>
    </mappers>
</configuration>

二、mapper 配置

mapper 配置:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.github.binarylei.mybatis.helloworld.UserMapper">

    <!--eviction: 緩存的回收策路。
            LRU  默认,最近最少使用的:移除最长时间不被使用的对象。
            FTFO 先进先出:按对家进入缓存的顺序来移除它们。
            SOFT 软引用:移除基于垃圾回收器状态和软引用规则的对象。
            WEAK 弱引用:更积极地移除基于拉圾收集器状态和弱引用规则。
        flushInterval: 存刷新间隔缓存多长时间清空一次,默认不清空,设置一个毫秒值。
        readon1y: 是否只读 true。
            true: MyBatis 直接就会将数据在存中的引用交给用户。不安全,速度快
            fa1se: MyBatis 会利用反序列的技术克隆一份新的数据给你。安全,速度慢
        size: 缓存存放多少元素
        type: 指定自定义存的全类名实现 Cache 接口即可
    -->
    <cache eviction="LRU" flushInterval="1000" readOnly="false" size="20"/>

    <!-- 获取自增主键,方式一 -->
    <insert id="save" parameterType="user" useGeneratedKeys="true" keyProperty="id">
        insert into user(name,age,sex) values(#{name},#{age},#{sex})
    </insert>

    <!-- 获取自增主键,方式二 -->
    <insert id="save2" parameterType="user">
        <selectKey resultType="int" keyProperty="id" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user(name,age,sex) values(#{name},#{age},#{sex})
    </insert>

    <!-- useCache: true 表示开启二级缓存。
         flushCache: true 表示每次使用时都会清除缓存,包括一级缓存和二级缓存
    -->
    <select id="getUser" resultType="User" useCache="false" flushCache="true">
        select * from user where id=#{id};
    </select>
</mapper>

每天用心记录一点点。内容也许不重要,但习惯很重要!

原文地址:https://www.cnblogs.com/binarylei/p/9745934.html