Lists in MyBatis 'IN' clause
本问题已经有最佳答案,请猛点这里访问。
如何将整数列表传递给MyBatis XML,以便在MySQL查询的in子句中使用?
我在
目前,我正在将该整数列表转换为字符串,并使用字符串替换(
我尝试使用
下面是一个示例Java代码:
1 2 3 4 5 6 |
映射器xml:
1 2 3 4 5 6 | <select id="getStripperDetails" parameterType="java.util.HashMap" resultMap="StripperMap"> SELECT STRIPPER_ID, STAGE_NAME, REAL_NAME, CLUB FROM EXOTIC_DANCERS WHERE CLUB = #{club} AND STRIPPER_ID IN <foreach item="item" index="index" collection="stripperIds" open="(" separator="," close=")"> #{index} </foreach> </select> |
我无法弄清楚要为
能否请您帮助我了解
编辑:
@ 10086,
下面是堆栈跟踪:
1 2 3 4 5 6 7 8 9 10 11 12 13 | org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NullPointerException ### The error may involve com.stripclub.mapper.stripClubMapper.getStripperDetails-Inline ### The error occurred while setting parameters ### Cause: java.lang.NullPointerException at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:67) ~[mybatis-spring-1.0.0-RC3.jar:1.0.0-RC3] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:345) ~[mybatis-spring-1.0.0-RC3.jar:1.0.0-RC3] at com.sun.proxy.$Proxy208.selectList(Unknown Source) ~[na:na] at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:193) ~[mybatis-spring-1.0.0-RC3.jar:1.0.0-RC3] at org.apache.ibatis.binding.MapperMethod.executeForList(MapperMethod.java:85) ~[mybatis-3.0.4.jar:3.0.4] at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:65) ~[mybatis-3.0.4.jar:3.0.4] at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:38) ~[mybatis-3.0.4.jar:3.0.4] at com.sun.proxy.$Proxy209.getTransactionIds(Unknown Source) ~[na:na] |
与列表一起使用时,应该在foreach标记内使用item属性指定的值。用法如下:
1 2 3 | <foreach item="sId" collection="stripperIds" separator="," open="(" close=")"> #{sId} </foreach> |
使用列表时,索引属性不是必需的。有关更多信息,请参阅MyBatis文档部分,或查看DTD-http://mybatis.org/dtd/mybatis-3-mapper.dtd了解有关参数的更多信息:
1 2 3 4 5 6 7 8 9 | <!ELEMENT foreach (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*> <!ATTLIST foreach collection CDATA #REQUIRED item CDATA #IMPLIED index CDATA #IMPLIED open CDATA #IMPLIED close CDATA #IMPLIED separator CDATA #IMPLIED > |
此外,可以在foreach中访问对象列表,如下所示。您通常将其用于INSERT / UPDATE语句:
样品豆:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class StripperBean { public StripperBean(int stripperID, String stripperName, String realName) { this.stripperID = stripperID; this.stripperName = stripperName; this.realName = realName; } private int stripperID; private String stripperName; private String realName; public int getStripperID() { return stripperID; } public void setStripperID(int stripperID) { this.stripperID = stripperID; } public String getStripperName() { return stripperName; } public void setStripperName(String stripperName) { this.stripperName = stripperName; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } } |
在您的实现中:
1 2 3 4 5 6 7 8 | Map<String, Object> input = new HashMap<>(); input.put("club", club); List<StripperBean> strippers = new ArrayList<>(); strippers.add(new StripperBean(1,"Ashley","Jean Grey")); strippers.add(new StripperBean(2,"Candice","Diana Prince")); strippers.add(new StripperBean(3,"Cristal","Lara Croft")); input.put("strippers", strippers); return stripClubMapper.saveStripperDetails(input); |
在映射器xml中:
1 2 3 4 5 6 7 8 9 | <insert id="saveStripperDetails"> INSERT INTO EXOTIC_DANCERS (STRIPPER_ID, STAGE_NAME, REAL_NAME) VALUES <foreach item="stripper" collection="input" separator=","> (#{stripper.stripperID}, #{stripper.stripperName}, #{stripper.realName}) </foreach> </select> |
好的问题顺便说一句:)
使用注释应该更容易
1 2 3 4 5 6 7 | @Select({ "","select"," *","FROM TABLE", "WHERE CONDITION IN " + "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> #{item} </foreach>" + "" }) @Results({ }) List<POJO> selectByKeys(@Param("list") List<String> ids); |
您的xml应该是这样的:
1 2 3 | <foreach item="item" index="index" collection="stripperIds" open="(" separator="," close=")"> #{item} </foreach> |
使用地图(或Map.Entry对象的集合)时,索引将是键对象,项目将是值对象。
您可以在此处参考以获取详细信息。您将对这些属性有扎实的了解。
您的输入是一张地图。因此,您需要在直接调用stripperIds之前从输入中解析stripperIds。