Java 中 mybatis 的 SQL 语句 分组 分割 字符串

下面是 mybatis 中的一个例子
<!-- count-select -->
<select id="countByYKenan" parameterType="java.lang.String" resultType="com.YKenan.YKenan.pojo.Counts">
    select
    <if test="groupBy != null and groupBy != ">
        SUBSTRING_INDEX(${
          
   groupBy}, _, 1) as tName,
    </if>
    count(0) as tCounts
    from y_kenan
    where y_kenan_one like #{
          
   yKenanOne,jdbcType=VARCHAR}
    <if test="yKenanTwo != null and yKenanTwo != ">
        AND y_kenan_two = #{
          
   yKenanTwo,jdbcType=VARCHAR}
    </if>
    <if test="yKenanThree != null and yKenanThree != ">
        AND y_kenan_three = #{
          
   yKenanThree,jdbcType=VARCHAR}
    </if>
    <if test="yKenanFour != null and yKenanFour != ">
        AND y_kenan_four = #{
          
   yKenanFour,jdbcType=VARCHAR}
    </if>
    <if test="groupBy != null and groupBy != ">
        GROUP by SUBSTRING_INDEX(${
          
   groupBy}, _, 1)
    </if>
</select>
Counts 类中的属性要与 sql 输出的列 as 后面的名字及数量都要相同 SUBSTRING_INDEX(str,delim,count) 此 sql 函数是指: (1) str 指 要进行处理的字符串 (2) delim 指 要以什么符号进行分割 (3) count 指 要以此符号分割的数量
例子:
经验分享 程序员 微信小程序 职场和发展