19.Mybatis映射文件mapper.xml之结果字段重命名、根据日期范围筛选、多表连接查询动态排序

目录

1.查询结果段代码

(1)字段起别名

(2)字段为null,则结果为false,否则为true

2.查询条件段代码

(1)查询日期范围与条件参数日期范围有交集的数据

(2)根据条件动态排序

(3)根据字段拼音排序


1.查询结果段代码

<!--多张表连接查询,字段起别名-->
  <sql id="resultList">
        SELECT
            a.id AS id,
            c.id AS unitId,
            c.name AS unitName,
            b.title AS title,
            isnull(a.price)!=1 as hasPrice,
            a.modifier AS modifier,
            a.modify_time AS modifyTime
        FROM
            table_a a
            LEFT JOIN table_b b ON a.info_id = b.id
            LEFT JOIN table_c c ON b.unit_id = c.id
    </sql>
  • (1)字段起别名

如上段代码,三张表做关联查询,给c表的name字段起别名为unitName;给a表的modify_time字段起别名为modifyTime;  给isnull(a.price)!=1的结果命名为hasPrice。

  • (2)字段为null,则结果为false,否则为true

如上段代码,其中isnull(a.price)!=1 as hasPrice 表示如果a表的price字段有数据则结果为1,如果a表的price字段为null则结果为0。结果映射到java的布尔类型属性上,1就为true,0就为false

代码执行逻辑分析:isnull(a.price)只有两种结果。结果为1,表示a表的price为空,结果为0,表示a表的price不为空。再判断isnull(a.price)的结果是否不等于1,相当于给isnull(a.price)的结果取反,即得到预期的结果。

2.查询条件段代码

 <include refid="resultList"/>
        <where>1=1
            <if test="startDate != null and endDate != null">
                and ((start_date BETWEEN #{startDate} AND #{endDate})
                OR (end_date BETWEEN #{startDate} AND #{endDate})
                OR (start_date &lt; #{startDate} AND end_date &gt; #{endDate}))
            </if>

            <if test="unitId != null and unitId !=''">
                and c.id=#{unitId}
            </if>

            <if test="title != null and title !=''">
                and b.title like concat('%', #{title}, '%')
            </if>

            /*默认排序:未配置价格的显示在前,已配置显示在后;再根据更新时间倒序排序;最后根据交易单元排序;*/
            <if test="sortParam == null or sortParam.sortName == null">
                order by isnull(a.price)  desc,a.modify_time desc,c.sort asc
            </if>
            <if test="sortParam != null">
                <if test="sortParam.sortName == 'unitName'">
                    /*根据拼音排序*/
                    order by CONVERT(c.abbreviation USING gbk)
                </if>
                <if test="sortParam.sortName == 'title'">
                    order by b.title
                </if>
                <if test="sortParam.sortName == 'hasPrice'">
                    order by isnull(a.price)!=1
                </if>
                <if test="sortParam.sortName == 'modifier'">
                    order by a.modifier
                </if>
                <if test="sortParam.sortName == 'modifyTime'">
                    order by a.modify_time
                </if>

                <if test="sortParam.orderType == 1">
                    asc
                </if>
                <if test="sortParam.orderType == 2">
                    desc
                </if>
            </if>
        </where>

查询条件段完整段代码如上。

  • (1)查询日期范围与条件参数日期范围有交集的数据

需求:查询数据库表字段start_date和end_date日期范围内有落在条件参数[startDate,endDate]之间的日期的数据;

 <if test="startDate != null and endDate != null">
                and ((start_date BETWEEN #{startDate} AND #{endDate})
                OR (end_date BETWEEN #{startDate} AND #{endDate})
                OR (start_date &lt; #{startDate} AND end_date &gt; #{endDate}))
            </if>

代码分析:查询出[start_date,end_date]和[startDate,endDate]有交集的数据;其中&lt;表示小于号<,&gt;表示大于号;

  • (2)根据条件动态排序

需求:如果排序条件参数为空,则自定义默认的排序;否则,根据条件参数名来指定对应的表对应的字段做排序。代码如下:

 /*默认排序:未配置价格的显示在前,已配置显示在后;再根据更新时间倒序排序;最后根据交易单元排序;*/
            <if test="sortParam == null or sortParam.sortName == null">
                order by isnull(a.price)  desc,a.modify_time desc,c.sort asc
            </if>
            <if test="sortParam != null">
                <if test="sortParam.sortName == 'unitName'">
                     /*根据拼音排序*/
                    order by CONVERT(c.abbreviation USING gbk)
                </if>
                <if test="sortParam.sortName == 'title'">
                    order by b.title
                </if>
                <if test="sortParam.sortName == 'hasPrice'">
                    order by isnull(a.price)!=1
                </if>
                <if test="sortParam.sortName == 'modifier'">
                    order by a.modifier
                </if>
                <if test="sortParam.sortName == 'modifyTime'">
                    order by a.modify_time
                </if>

                <if test="sortParam.orderType == 1">
                    asc
                </if>
                <if test="sortParam.orderType == 2">
                    desc
                </if>
            </if>
  • (3)根据字段拼音排序

<if test="sortParam.sortName == 'unitName'">
   /*根据拼音排序*/
   order by CONVERT(c.abbreviation USING gbk)
</if>

在MySQL数据库中,使用UTF-8编码时,默认的排序规则(如utf8_general_ci)并不支持按拼音顺序排序中文字符。常规做法是利用CONVERT函数将字符集转换为GBK,从而实现拼音排序。

MyBatis 是一款使用 XML 或注解配置的持久层框架,它可以自动化地将数据库中的数据映射Java 对象中。在 MyBatis 中,mappermapper.xml 是配对使用的,其中 mapper 是接口,而 mapper.xml映射配置文件mapper 接口中定义了数据库操作的方法,而 mapper.xml 中则定义了这些方法的 SQL 语句以及参数映射规则、结果集映射规则等。 下面是一个简单的例子: 1. 定义 mapper 接口 ```java public interface UserMapper { User selectUserById(Integer id); } ``` 2. 定义 mapper.xml 映射配置文件 ```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"> <mapper namespace="com.example.UserMapper"> <resultMap id="userResultMap" type="com.example.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> </resultMap> <select id="selectUserById" resultMap="userResultMap"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 上述代码中,namespace 属性指定了 mapper 接口的全限定名,resultMap 标签定义了一个结果集映射规则,select 标签定义了一个查询操作,其中 id 属性指定了 mapper 接口中的方法名,resultMap 属性指定了结果集映射规则的 id。 3. 在 MyBatis 配置文件中引入 mapper.xml ```xml <?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> <mappers> <mapper resource="com/example/UserMapper.xml"/> </mappers> </configuration> ``` 上述代码中,mapper 标签指定了映射配置文件的位置。 这样就完成了 mappermapper.xml 的配置。在代码中调用 selectUserById 方法时,MyBatis 会根据 mapper.xml 中的配置自动生成 SQL 语句,并将查询结果映射到 User 对象中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值