MyBatis中 Mapper.xml 文件

发布时间 2023-09-19 11:43:12作者: Leslie_Cheung

 resources 目录下 新建文件夹 mapper (个人习惯全路径与Mapper类对应) 

<?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="对应Mapper的路径">

    <resultMap type="返回的实体类" id="取个名字Result,方便引用">
        <result property="实体类属性" column="表字段"/>
    </resultMap>

    <sql id="selectSQL">
        select * from 表名
    </sql>

    <select id="Mapper中对应的方法名" parameterType="传入的查询数据类型/类名" resultMap="引用上面resultMap的id">
        <include refid="selectSQL"/>
        <where>
            <choose>--此处为if-else
                <when test="查询类的属性名 != null and 查询类的属性名 != ''">
                    and 表字段 like concat('%', #{查询类的属性名}, '%')
                </when>
                <otherwise>
                    and 查询类的属性名 like '值'
                </otherwise>
            </choose>
            <if test="查询类的属性名 != null  and 查询类的属性名 != ''"> and 表字段 &gt;= #{查询类的属性名}</if>--大于等于
            <if test="查询类的属性名 != null  and 查询类的属性名 != ''"> and 表字段 &lt;= #{查询类的属性名}</if>--小于等于
            <if test="查询类的属性名 != null  and 查询类的属性名 != ''"> and (表字段 like concat('%', #{查询类的属性名}, '%') or 表字段 like concat('%', #{查询类的属性名}, '%'))</if>
        </where>
        order by 表字段 desc
    </select>
</mapper>

 

 

 

 

 

 

 

 

                Leslie Cheung 随笔