mybatis的注解转义符及非空验证

mybatis注解 @Select 、@Insert、 @Update、@Delete 查询使DAO层代码更简洁,使用注解时常需要用到转义符。

常用的一些需要转义的特殊字符:

< < 小于号

> > 大于号

<= 和<= 表示的是一个意思

& & 和

' ’ 单引号

" " 双引号

  如果sql加上script标签,代表该条sql以xml解析的方式解析映射,xml解析方式,< 或者 > 应该都用转义字符。

验证 equipmentCode、equipmentProducer、ipv4三个字段非空:

@Select({"<script>",
            "select * from r_camera where 1=1 and status=1"
                    + "<if test='equipmentCode!=null and equipmentCode!='''>"
                    + "and equipment_code=#{equipmentCode}"
                    + "</if>"
                    + "<if test='equipmentProducer!=null and equipmentProducer!='''>"
                    + "and equipment_producer=#{equipmentProducer}"
                    + "</if>"
                    + "<if test='ipv4!=null and ipv4!='''>"
                    + "and ipv4=#{ipv4}"
                    + "</if>"
                    + "ORDER BY create_time desc"
                    + "</script>"})
    List<CameraVO> findAll(CameraVO cameraVO);

  

@Select({"<script>",
            "select * from r_camera where 1=1 and status=1"
                    + "<if test='equipmentCode!=null and equipmentCode!=&apos;&apos;'>"
                    + "and equipment_code=#{equipmentCode}"
                    + "</if>"
                    + "<if test='equipmentProducer!=null and equipmentProducer!=&apos;&apos;'>"
                    + "and equipment_producer=#{equipmentProducer}"
                    + "</if>"
                    + "<if test='ipv4!=null and ipv4!=&apos;&apos;'>"
                    + "and ipv4=#{ipv4}"
                    + "</if>"
                    + "ORDER BY create_time desc"
                    + "</script>"})
&apos;&apos;等同于''
posted @ 2020-01-06 16:24  Alice口袋有糖  阅读(2490)  评论(0编辑  收藏  举报