【Mybatis-附件】Mapper.xml 参数配置以及含义

678 阅读1分钟
<?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">
<!-- namespace: mapper.xml对应的mapper接口类 -->
<mapper namespace="com.libra.spring.demo.dao.StudentMapper">
    <!-- cache-ref.namespace: 配置需要缓存的mapper接口 -->
    <cache-ref namespace="com.libra.spring.demo.dao.StudentMapper"/>

    <!-- cache.type 默认值为PERPETUAL,缓存加载器的类型,如果不想使用PERPETUAL,可以传入自定义的集成cache的类-->
    <!-- cache.eviction 默认值为LRU,LRU - 最近最少使用、FIFO - 先进先出、SOFT - 软引用、WEAK - 弱引用 -->
    <!-- cache.flushInterval 缓存数据的刷新时间 -->
    <!-- cache.size 缓存数据的大小 -->
    <!-- cache.readOnly 默认值为false 是否只读 -->
    <!-- cache.blocking 默认值为false 是否阻塞 -->
    <cache type="PERPETUAL" eviction="LRU" flushInterval="6000" size="100" readOnly="false"
           blocking="false"/>

    <!-- parameterMap.id 存储到configuration.parameterMaps中的key -->
    <!-- parameterMap.type 参数对象的类 -->
    <parameterMap id="studentReq" type="com.libra.spring.demo.dao.req.StudentReq">
        <!-- parameter.property 类中的属性名 -->
        <!-- parameter.javaType 类中的属性的类型 -->
        <!-- parameter.jdbcType 类中的属性的数据库类型 -->
        <!-- parameter.mode 参数模式 IN - 入参 OUT - 出参 INOUT - 入参和出参 -->
        <!-- parameter.typeHandler 该类型使用的类型处理器 -->
        <!-- parameter.resultMap 结果对象 如果属性为对象时可以设置返回值为 本类的resultMap的ID -->
        <!-- parameter.scale 小数点保留位数 -->
        <parameter property="id" javaType="java.lang.Long" jdbcType="BIGINT" mode="IN" resultMap=""
                   typeHandler="org.apache.ibatis.type.BigIntegerTypeHandler" scale=""/>

        <parameter property="name" javaType="java.lang.String" jdbcType="VARCHAR" mode="IN"
                   resultMap="" typeHandler="org.apache.ibatis.type.StringTypeHandler" scale=""/>
    </parameterMap>

    <!-- resultMap.id 存储到configuration的resultMaps中的key 或者在引用中使用 -->
    <!-- resultMap.type 返回的对象类型 -->
    <!-- resultMap.extends 继承的resultMap,会取两者并集 -->
    <!-- resultMap.autoMapping 是否启动自动映射 -->
    <resultMap id="BaseResultMap" type="com.libra.spring.demo.model.entity.Student"
               extends="idBaseResultMap" autoMapping="true">
        <!--@Table student-->

        <!-- resultMap.constructor 会使用构造函数 如果内部有懒加载,会多次执行构造函数 -->
        <!-- resultMap.constructor.idArg ID参数,使用会提升性能 (具体原因待考证) -->
        <!-- resultMap.constructor.arg 参数 -->
        <constructor>
            <!-- resultMap.constructor.arg/idArg.column 列名 -->
            <!-- resultMap.constructor.arg/idArg.name 属性名 -->
            <!-- resultMap.constructor.arg/idArg.jdbcType 数据库类型 -->
            <!-- resultMap.constructor.arg/idArg.javaType 返回的数据类型 -->
            <!-- resultMap.constructor.arg/idArg.select 执行的sql -->
            <!-- resultMap.constructor.arg/idArg.typeHandler 类型处理器 -->
            <!-- resultMap.constructor.arg/idArg.resultMap 返回使用的结果映射 (不能和select同时使用) -->
            <idArg name="id" column="id"/>
            <arg column="class_number"
                 name="classNumber"
                 jdbcType="BIGINT"
                 javaType="com.libra.spring.demo.model.entity.ClassInfo"
                 select="com.libra.spring.demo.dao.ClassInfoMapper.selectById"
                 typeHandler="com.libra.spring.demo.typeHandle.ClassInfoHandler"
            />
        </constructor>

        <!-- 所有的result属性一致 -->
        <!-- resultMap.result.property 属性名 -->
        <!-- resultMap.result.column 列名 -->
        <!-- resultMap.result.jdbcType 数据库类型 -->
        <!-- resultMap.result.javaType 属性java类型 -->
        <!-- resultMap.result.typeHandler 类型处理器 -->
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="height" jdbcType="INTEGER" property="height"/>
        <result column="weight" jdbcType="INTEGER" property="weight"/>


        <!-- resultMap.collection/association 对结果查询 collection: 1对N association:1对1-->
        <!-- resultMap.collection/association.property 属性名 -->
        <!-- resultMap.collection/association.column 列名 -->
        <!-- resultMap.collection/association.jdbcType 数据库类型 -->
        <!-- resultMap.collection/association.javaType 属性类 -->
        <!-- resultMap.collection/association.autoMapping 是否自动映射 -->
        <!-- resultMap.collection/association.typeHandler 类型处理器 -->

        <!-- 通过设置的select执行 -->
        <!-- resultMap.collection/association.select 需要执行的sql (可以不设置 -->

        <!-- 设置多结果设置子对象 -->
        <!-- resultMap.collection/association.foreignColumn 查询有多个结果集时,通过设置的字段做映射关系,取设置的resultSet的结果集,映射到设置的property属性,用column比较( 无需和select配置同时使用 处理结果在结果处理器之后所以返回void时无法使用 ) -->
        <!-- resultMap.collection/association.resultSet 返回有多个结果集时所取的结果集 -->
        <!-- resultMap.collection/association.resultMap 结果映射 (和select中的resultMap 不需要重复设置) -->

        <!-- resultMap.collection/association.notNullColumn 不能为空的列(如果为空 就不会创建子对象 - 待认证 -->
        <!-- resultMap.collection/association.columnPrefix 多个表关联查询时使用,用于区分association的值 - 待认证 -->
        <association property="gender" column="gender" jdbcType="VARCHAR"
                     javaType="com.libra.spring.demo.model.entity.GenderInfo"
                     select="com.libra.spring.demo.dao.GenderInfoMapper.selectByPrimaryKey"
                     autoMapping="true"
                     fetchType="eager"
                     notNullColumn="used">
        </association>

        <!-- resultMap.discriminator 对某一个列值做判断,并返回不同的数据映射关系 -->
        <!-- resultMap.discriminator.column 做判断的列名 -->
        <!-- resultMap.discriminator.jdbcType 数据库类型 -->
        <!-- resultMap.discriminator.javaType java类型 -->
        <!-- resultMap.discriminator.typeHandler 类型处理器 -->
        <discriminator column="class_number"
                       jdbcType="VARCHAR"
                       javaType="java.lang.Long">
            <!-- resultMap.discriminator.case.value 判断的值 -->
            <!-- resultMap.discriminator.case.resultType resultMap的数据类型 -->
            <!-- resultMap.discriminator.case.resultMap 满足条件时执行的(resultMap)返回映射 (可以在内部配置result属性) -->
            <case value="1" resultType="com.libra.spring.demo.model.entity.Student">
                <result property="base" column="height"/>
            </case>
            <case value="2" resultType="com.libra.spring.demo.model.entity.Student">
                <result property="base" column="weight"/>
            </case>
        </discriminator>
    </resultMap>

    <resultMap id="idBaseResultMap" type="com.libra.spring.demo.model.entity.Student">
        <id column="id" jdbcType="BIGINT" property="id"/>
    </resultMap>

    <!-- sql.id 使用的引用名 也是存放在sqlFragments的key -->
    <!-- sql.lang 为特定的语句指定语言(驱动类) -->
    <!-- sql.databaseId 数据库方言设置 数据库方言在 mybatis-config.xml 的 databaseIdProvider 中该设置 -->
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id,
        `name`,
        gender,
        class_number,
        height,
        weight
    </sql>

    <!--auto generated by MybatisCodeHelper on 2021-05-24-->
    <!-- select.id 使用的引用名 也是存放在mappedStatements的key -->
    <!-- select.parameterMap 使用的入参映射 -->
    <!-- select.parameterType 使用的入参类型 -->
    <!-- select.resultMap 使用的出参映射 -->
    <!-- select.resultType 使用的出参类型 -->
    <!-- select.resultSetType 返回的结果类型 -->
    <!-- select.statementType 语句类型 STATEMENT|PREPARED|CALLABLE 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED -->
    <!-- select.fetchSize 给驱动的建议值,尝试让驱动程序每次批量返回的结果行数等于这个设置值 -->
    <!-- select.timeout 给驱动的超时时间 单位为秒 -->
    <!-- select.flushCache 是否刷新缓存 SELECT 默认为不刷新 其他为刷新 -->
    <!-- select.useCache 是否使用缓存 SELECT 默认使用,其他使用 -->
    <!-- select.resultOrdered 是否返回多个结果集 默认关闭 -->
    <!-- select.lang 指定语言驱动 -->
    <!-- select.resultSets 返回的结果集对象 -->
    <select id="selectByNameAndId"
            parameterMap="studentReq"
            resultMap="BaseResultMap"
            statementType="PREPARED"
            timeout="5">
        select
        <include refid="Base_Column_List"/>
        from student
        <where>
            <if test="name != null">
                and `name` = #{name,jdbcType=VARCHAR}
            </if>
            <if test="id != null">
                and id = #{id,jdbcType=BIGINT}
            </if>
        </where>
    </select>

    <select id="selValidCollectionForeignColumn"
            resultMap="BaseResultMap"
            resultSets="student,gender"
            parameterType="com.libra.spring.demo.dao.DemoResultHandler">
        select
        <include refid="Base_Column_List"/>
        from student;

                select id,
                       `name`
                from gender_info;
    </select>


    <select id="selVoid" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from student
    </select>


    <select id="selCursor" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from student
    </select>

    <select id="selMap" resultMap="BaseResultMap" statementType="STATEMENT">
        select
        <include refid="Base_Column_List"/>
        from student
    </select>


    <select id="selList" resultSets="" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from student
    </select>


    <!--auto generated by MybatisCodeHelper on 2021-08-30-->
    <update id="updateById" statementType="PREPARED">
        update student
        <set>
            <if test="updated.id != null">
                id = #{updated.id,jdbcType=BIGINT},
            </if>
            <if test="updated.name != null">
                name = #{updated.name,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{updated.id,jdbcType=BIGINT}
    </update>

    <!--auto generated by MybatisCodeHelper on 2021-09-10-->
    <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO student

        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name
            </if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR}
            </if>
        </trim>
    </insert>
</mapper>

文章链接