MyBatis通过@SelectProvider方式查询时,传递集合参数的写法

  • 场景:在Mapper接口中有如下接口:
@SelectProvider(type = AiProcessProvider.class, method = "listAiProcessesByBatch")
List<AiProcess> listAiProcessesByBatch(List<String> names);

即是:要根据姓名集合来查询!

  • 在Provider中实现
public String listAiProcessesByBatch(List<String> names) {
		return new SQL () {
			{
				SELECT ("*");
				FROM("ai_process");
				String sql = " check_no IN (";
				for (String param : names) {
					sql += "'"+ param+"',";
				}
				sql = StringUtils.substring(sql, 0, sql.length()-1);
				sql += ")";
				WHERE(sql);
			}
		}.toString();
	}

以上代码直接传递的 names,这样是要报错的!, 正确的应该将names改为list ,如下代码:

public String listAiProcessesByBatch(List<String> list) {
		return new SQL () {
			{
				SELECT ("*");
				FROM("ai_process");
				String sql = " check_no IN (";
				for (String param : list) {
					sql += "'"+ param+"',";
				}
				sql = StringUtils.substring(sql, 0, sql.length()-1);
				sql += ")";
				WHERE(sql);
			}
		}.toString();
	}

你可能感兴趣的:(数据库,mybatis,集合参数)