mybatis通过list遍历查询数据库

最近开发遇到Service传给Mapper层,需要进行遍历输出,比如批量插入,批量查询,或者in这种判断,如果写for循环逻辑也是可以,但是比较懒。还是想简单一点

对于insert插入。可以采用beach方式,但是我可能需要写到mapper用注释形式。因此采用下面的这种方式

public String insertRoleMenuByList(Map map) {
    	List pp =  (List) map.get("roleMenuMapList");
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO " + roleMenuTable)
                .append(" ( roledly_id,display_id,create_user ) "
                        + "VALUES ");
        MessageFormat mf =
                new MessageFormat("(#'{'roleMenuMapList[{0}].roledlyId}, " + "#'{'roleMenuMapList[{0}].displayId}, "
                        + "#'{'roleMenuMapList[{0}].createUser}" + ")");
        
        for (int i = 0; i < pp.size(); i++) {
            sb.append(mf.format(new Object[] {i}));
            if (i < pp.size() - 1)
                sb.append(",");
        }
        
        return sb.toString();
    }

 这样拼接的就是insert into a表 (字段1,字段2,字段3)values (1,2,3),(x,x,x),(x,x,x),(x,x,x),(x,x,x)这样不用考虑字符串、数字等情况

你可能感兴趣的:(java)