有关MyBatis的映射.XML文件的详细说明

(1)typeAliases:对po类进行别名的定义

(2)全局配置代码:

<!-- 自定义别名 -->
    <typeAliases>
        <!-- 单个别名定义 -->
        <!-- type:需要被取代的全限定类名   alias:别名 
            <typeAlias type="com.san.model.User" alias="user"/>
        -->
        <!-- 批量别名定义(推荐) -->
        <!-- package:指定包名称来为该包下的PO类声明别名,默认的别名是类型(首字母大小写都可以) -->
        <package name="com.san.model"/>

    </typeAliases>

(3)映射文件代码:(次数映射文件中的resultType就不需要使用全限定类名,直接使用别名就可以了)

<select id="findUserById" parameterType="int" resultType="user">
        select * from user where id=#{id}
    </select>

全局配置Mapper

<mapper resource=’’/>

(2)使用完全限定路径

<mapper url=’’/>

(3)使用mapper接口的全限定名
注意:此种方法要求mapper接口和mapper映射文件要名称相同,且放到同一个目录下

<mapper class=’’/>

(4)注册指定包下的所有映射文件(推荐)
注意:此种方法要求mapper接口和mapper映射文件要名称相同,且放到同一个目录下

<package name=’’/>

输出映射文件的类型:

1、resultType

(1)使用要求:
a、使用resultType进行结果映射时,需要查询出的列名和映射的对象的属性名一致,才能映射成功。
b、如果查询的列名和对象的属性名全部不一致,那么映射的对象为空。
c、如果查询的列名和对象的属性名有一个一致,那么映射的对象不为空,但是只有映射正确那一个属性才有值。
d、如果查询的sql的列名有别名,那么这个别名就是和属性映射的列名。

(2)映射文件:

<!-- 综合查询,查询用户的总数 -->
    <select id="findUserCount" parameterType="com.san.model.UserQueryvo" resultType="int">
        select count(*) from user 
        <where>
            <include refid="whereClause"></include>
        </where>

    </select>

2、resultMap

(1)使用要求:
使用resultMap进行结果映射时,不需要查询的列名和映射的属性名必须一致。但是需要声明一个resultMap,来对列名和属性名进行映射。

(2)映射文件:

<!-- id标签:专门为查询结果中唯一列映射 -->
    <!-- result标签:映射查询结果中的普通列 -->
    <!-- type标签:返回类型 -->
    <resultMap type="user" id="UserResMap">
        <id column="id_" property="id"/>
        <result column="username_" property="username"/>
        <result column="sex_" property="sex"/>
    </resultMap>    
    <select id="findUserRstMap" parameterType="int" resultMap="UserResMap">
        select id id_,username username_,sex sex_ from user where id=#{id}  
    </select>

3、动态sql

(1)概述:
在mybatis中,它提供了一些动态sql标签,可以让程序员更快的进行mybatis的开发,这些动态sql可以通过sql的可重用性。

(2)常用的动态sql标签:
if标签、where标签、sql片段、foreach标签

(3)映射文件(总的映射文件):

<?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接口的全限定名一致 -->
<mapper namespace="com.san.mapper.UserMapper">

    <!-- 通过ID查询用户 -->
    <select id="findUserById" parameterType="int" resultType="user">
        select * from user where id=#{id}
    </select>

    <!-- 定义sql片段 -->
    <!-- sql片段内,可以定义sql语句中的任何内容 -->
    <!-- sql片段内,最好不要使用where和select关键字声明在内 -->
    <sql id="whereClause">
        <!-- if标签:对输入的参数进行判断 -->
        <!-- test标签:指定判断的表达式 -->
        <if test="user!=null">
            <!-- 判断用户名不为空 -->
            <if test="user.username!=null and user.username!=''">
                and username like '%${user.username}%'
            </if>

            <!-- 判断性别不为空 -->
            <if test="user.sex!=null and user.sex!=''">
                and sex=#{user.sex}
            </if>
        </if>

        <!-- 判断集合 -->
        <!-- collection:表示pojo中集合属性的属性名称 -->
        <!-- item:为遍历出的结果声明一个变量名称 -->
        <!-- open:遍历开始时,需要拼接的字符串 -->
        <!-- close:遍历结束时,需要拼接的字符串 -->
        <!-- separator:遍历中间需要拼接的字符串 -->
        <if test="idList!=null">
            and id in
            <foreach collection="idList" item="id" open="(" close=")" separator=",">
                <!-- and id in (#{id},#{id},#{id}) -->
                #{id}
            </foreach>
        </if>
    </sql>

    <!-- 综合查询,查询用户列表 -->
    <!-- #{}中的参数名称要和包装pojo中的对象层级一致,并且属性名称要一致 -->
    <select id="findUserList" parameterType="com.san.model.UserQueryvo" resultType="user">
        select * from user
        <!-- where标签:默认去掉后面第一个and,如果没有参数,则把自己干掉 -->
        <where> 
            <!-- 引入sql片段 -->
            <include refid="whereClause"></include>
        </where>
    </select>

    <!-- 综合查询,查询用户的总数 -->
    <select id="findUserCount" parameterType="com.san.model.UserQueryvo" resultType="int">
        select count(*) from user 
        <where>
            <include refid="whereClause"></include>
        </where>

    </select>

    <!-- id标签:专门为查询结果中唯一列映射 -->
    <!-- result标签:映射查询结果中的普通列 -->
    <!-- type标签:返回类型 -->
    <resultMap type="user" id="UserResMap">
        <id column="id_" property="id"/>
        <result column="username_" property="username"/>
        <result column="sex_" property="sex"/>
    </resultMap>    
    <select id="findUserRstMap" parameterType="int" resultMap="UserResMap">
        select id id_,username username_,sex sex_ from user where id=#{id}  
    </select>
</mapper>

猜你喜欢

转载自blog.csdn.net/yiyongjiajun521/article/details/81196598