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!='''>"
+ "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>"})
''等同于''