spring boot mybatis注解中如何使用Array?
How to use Array in spring boot mybatis annotation?
我从 Frontend 收到了一组列。我需要SELECT ---- WHERE [Received Array] = 1。但是,数组的长度是动态的。
也就是说,应该为数组中收到的每一列编写值为 1 的查询。
@Select("SELECT * FROM reservation WHERE product_type = #{product_type} AND product_location = #{product_location} AND $(myArray) = 1")
List<product> getOptionedProduct(@Param("product_type") String product_type, @Param("product_location") String product_location, @Param("myArray") List<String> myArray);
myArray 有 [A, B, C]
我想写成
@Select("SELECT * FROM reservation WHERE product_type = #{product_type} AND product_location = #{product_location} AND A = 1 AND B = 1 AND C = 1")
可以吗?
您需要使用 <foreach />
标签 (doc)。
要在注释中使用标签,您需要用 <script />
标签将整个语句括起来。
@Select({
"<script>",
"SELECT * FROM reservation WHERE product_type = #{product_type} ",
"AND product_location = #{product_location} ",
"<if test='myArray != null'>",
" <foreach item='columnName' collection='myArray' separator=' and ' open=' and '>",
" ${columnName} = 1",
" </foreach>",
"</if>",
"</script>"
})
List<product> getOptionedProduct(
@Param("product_type") String product_type,
@Param("product_location") String product_location,
@Param("myArray") List<String> myArray);
如果您使用 Java 15 或更高版本,您可以受益于文本块 (JEP-378)。
@Select("""
<script>
SELECT * FROM reservation WHERE product_type = #{product_type}
AND product_location = #{product_location}
<if test="myArray != null">
<foreach item="columnName" collection="myArray" separator=" and " open=" and ">
${columnName} = 1
</foreach>
</if>
</script>
""")
由于 myArray
是列名列表(对吗?),您必须使用 ${}
而不是 #{}
。
因此,您需要确保 myArray
不包含任何意外的字符串以避免 SQL 注入等。
详情见FAQ entry。
我从 Frontend 收到了一组列。我需要SELECT ---- WHERE [Received Array] = 1。但是,数组的长度是动态的。
也就是说,应该为数组中收到的每一列编写值为 1 的查询。
@Select("SELECT * FROM reservation WHERE product_type = #{product_type} AND product_location = #{product_location} AND $(myArray) = 1")
List<product> getOptionedProduct(@Param("product_type") String product_type, @Param("product_location") String product_location, @Param("myArray") List<String> myArray);
myArray 有 [A, B, C] 我想写成
@Select("SELECT * FROM reservation WHERE product_type = #{product_type} AND product_location = #{product_location} AND A = 1 AND B = 1 AND C = 1")
可以吗?
您需要使用 <foreach />
标签 (doc)。
要在注释中使用标签,您需要用 <script />
标签将整个语句括起来。
@Select({
"<script>",
"SELECT * FROM reservation WHERE product_type = #{product_type} ",
"AND product_location = #{product_location} ",
"<if test='myArray != null'>",
" <foreach item='columnName' collection='myArray' separator=' and ' open=' and '>",
" ${columnName} = 1",
" </foreach>",
"</if>",
"</script>"
})
List<product> getOptionedProduct(
@Param("product_type") String product_type,
@Param("product_location") String product_location,
@Param("myArray") List<String> myArray);
如果您使用 Java 15 或更高版本,您可以受益于文本块 (JEP-378)。
@Select("""
<script>
SELECT * FROM reservation WHERE product_type = #{product_type}
AND product_location = #{product_location}
<if test="myArray != null">
<foreach item="columnName" collection="myArray" separator=" and " open=" and ">
${columnName} = 1
</foreach>
</if>
</script>
""")
由于 myArray
是列名列表(对吗?),您必须使用 ${}
而不是 #{}
。
因此,您需要确保 myArray
不包含任何意外的字符串以避免 SQL 注入等。
详情见FAQ entry。