Mybatis配置解释

一、前言

本文主要记录学习Mybatis的一些配置的解释,通过本文和Mybatis3官方文档的配合希望也能够对初学者有所帮助。
注意仅仅为一些关键配置描述,直接cv并不能直接使用!!!

二、基础核心配置

<?xml version="1.0" encoding="UTF-8"?>

<!--注意这里的configuration,Mapper配置不一样的,不要搞混-->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!--引入外部配置文件,给予下面的${}调用,当然你也可以直接写-->
    <properties resource="db.yaml" />
	
	<!--mybatis的一些设置-->
	<settings>
		<!--开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射-->
        <setting name="mapUnderscoreToCamelCase" value="true" />
        <!--开启二级数据缓存(一级缓存默认开启)-->
        <setting name="cacheEnabled" value="true"/>
    </settings>

	<!--注册别名,方便缩写-->
    <typeAliases>
        <!--扫描这个包下的所有类,没有注解使用类名称,有使用注解名称-->
        <package name="com.twelvet.entity"/>
    </typeAliases>

    <!--默认配置-->
    <environments default="development">

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

    </environments>

    <!--每一个Mpapper.xml都需要在mybatis核心配置文件注册-->
    <mappers>
    	<!--注意mybatis默认不支持通配符的,需要一个一个写,确实需要的请自行改写-->
        <mapper resource="mapper/StudentMapper.xml" />
        <!--<mapper class="com.twelvet.dao.UserMapper" />-->
        <!--<package name="com.twelvet.dao"/>-->
    </mappers>

</configuration>

对应的*Mapper.xml配置

<?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">
<!--需要指定命名空间:防止映射id重复-->
<mapper namespace="com.twelvet.dao.UserMapper">

    <!--在当前mapper.xml中使用二级缓存-->
    <cache
            <!--
                LRU  最近最少使用:移除最长时间不被使用的对象。
                FIFO  先进先出:按对象进入缓存的顺序来移除它们。
                SOFT  软引用:基于垃圾回收器状态和软引用规则移除对象。
                WEAK  弱引用:更积极地基于垃圾收集器状态和弱引用规则移除对象。
            -->
            eviction="FIFO"
            <!--自动刷新时间(秒),仅仅会在调用语句时刷新-->
            flushInterval="60000"
            <!--最大缓存数目-->
            size="512"
            <!--是否只读-->
            readOnly="true"/>

	<!--
        id;对应接口名称
        parameterType:参数类型
        resultType:结果类型
        resultMap:结果类型映射(配置)
    -->
    <select id="queryBlogIf" parameterType="map" resultType="blog">
        select * from blog

        <where>
            <if test="title != null">
	            and title = #{title}
	        </if>
	        <if test="author != null">
	            and author = #{author}
	        </if>
        </where>

    </select>

	<!--
        id:resultMap的属性
        type:结果类型
    --> 
	<resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"></result>
        <result property="name" column="name"></result>

        <!--
            复杂的属性,我们需要单独处理
            对象:association
            property:student属性
            column:数据库列表
            javaType:java类型
			select:调用的xml配置
        -->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    	
    	<!--
            复杂的属性,我们需要单独处理
            集合:collection
            ofType:申明这个集合的类型
        -->
    	<collection property="students" javaType="ArrayList" ofType="Student" select="getTeacher" column="id" />
    </resultMap>
    <!--给予上面的select调用-->
	<select id="getTeacher" resultType="Teacher">
        select * from teacher where id = #{tid}
    </select>

</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值