【mybatis】第二篇:@Select注解中加入字段判断

简介: 【mybatis】第二篇:@Select注解中加入字段判断

背景

因需要链表操作,所以采用了@select注解来写sql,抛弃了传统的xml形式。

1.错误性示范代码

@Select({"<script>",
         "select a.*,b.uuid,b.denoter_name as denoterName,b.spelling,b.public_name as publicName from t_denoter_qrcode a right join t_bz_denoter b on a.denoter_uuid=b.denoter_uuid ",
             "where 1=1 ",
                 "<if test='qrcodeRequest.denoterAddress !=null and qrcodeRequest.denoterAddress!="" '>" ,
                    "  and a.address like concat('%',#{qrcodeRequest.denoterAddress},'%')" ,
                 "</if> ",
                 "<if test='qrcodeRequest.denoterName !=null and qrcodeRequest.denoterName!="" '>" ,
                    " and b.denoter_name like concat('%',#{qrcodeRequest.denoterName},'%') " ,
                 "</if> ",
                 "<if test='qrcodeRequest.publicName !=null and qrcodeRequest.publicName!="" '> " ,
                    " and b.public_name like concat('%',#{qrcodeRequest.publicName},'%')" ,
                 "</if> ",
                    " <if test='qrcodeRequest.xzqhdm !=null and qrcodeRequest.xzqhdm!="" '>" ,
                    " and b.zzzz9998 like concat(#{qrcodeRequest.xzqhdm},'%') " ,
                 "</if> ",
            "</script>"})
    List<DenoterQrcodeEntity> denoterQueryList(@Param("qrcodeRequest") QrcodeRequest qrcodeRequest);

会报如下错误:

if元素类型必须后跟属性规范、">“或"/>”

这是因为我们在写sql时候没有转义导致的!

2、正确的写法,看仔细哦

@Select({"<script>",
            "select a.*,b.uuid,b.denoter_name as denoterName,b.spelling,b.public_name as publicName from t_denoter_qrcode a right join t_bz_denoter b on a.denoter_uuid=b.denoter_uuid ",
              "where 1=1 ",
                 "<if test='qrcodeRequest.denoterAddress !=null and qrcodeRequest.denoterAddress!=\"\" '>" ,
                    "  and a.address like concat('%',#{qrcodeRequest.denoterAddress},'%')" ,
                 "</if> ",
                 "<if test='qrcodeRequest.denoterName !=null and qrcodeRequest.denoterName!=\"\" '>" ,
                    " and b.denoter_name like concat('%',#{qrcodeRequest.denoterName},'%') " ,
                 "</if> ",
                 "<if test='qrcodeRequest.publicName !=null and qrcodeRequest.publicName!=\"\" '> " ,
                    " and b.public_name like concat('%',#{qrcodeRequest.publicName},'%')" ,
                 "</if> ",
                 "<if test='qrcodeRequest.xzqhdm !=null and qrcodeRequest.xzqhdm!=\"\" '>" ,
                    " and b.zzzz9998 like concat(#{qrcodeRequest.xzqhdm},'%') " ,
                 "</if> ",
            "</script>"})
    List<DenoterQrcodeEntity> denoterQueryList(@Param("qrcodeRequest") QrcodeRequest qrcodeRequest);

可以看到上面写法中,在判断空字符串的时候加入了转义,这样就可以解决问题!

相关文章
|
2天前
|
数据库
mybatisplus返回指定字段的两种方式
mybatisplus返回指定字段的两种方式
55 1
|
2天前
|
SQL Java 数据库连接
Mybatis Plus字段为空值时未更新解决方案
Mybatis Plus字段为空值时未更新解决方案
|
2天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
2天前
|
XML Java 数据库连接
MyBatis返回Map时值为null的字段会丢失
MyBatis返回Map时值为null的字段会丢失
|
2天前
|
数据库
MybatisPlus属性字段为数据库关键字
MybatisPlus属性字段为数据库关键字
22 0
|
2天前
|
Java 数据库连接 mybatis
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
24 0
|
2天前
|
Java 数据库连接 网络安全
mybatis使用全注解的方式案例(包含一对多关系映射)
mybatis使用全注解的方式案例(包含一对多关系映射)
13 0
|
2天前
|
关系型数据库 Java 数据库连接
如何利用Mybatis-Plus自动生成代码(超详细注解)
如何利用Mybatis-Plus自动生成代码(超详细注解)
47 1
|
2天前
|
Java 关系型数据库 数据库连接
【JavaEE进阶】 MyBatis使用注解实现增删改查
【JavaEE进阶】 MyBatis使用注解实现增删改查
|
2天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
86 1