java拼接xml,Java代码里拼接SQL语句到mybatis的xml

关键语句:

StringBuilder whereSql = new StringBuilder();

whereSql.append("SQL");

实现类:

public List getList(Map map) {

List> rs = new ArrayList>();

try {

StringBuilder whereSql = new StringBuilder();

if (map.get("userName").toString().length()>0) {

whereSql.append(" AND a.userName in ('" + map.get("userName").toString().replaceAll(",", "\',\'") + "')");//不为空时加入查询条件

}

if (map.get("CURRENTPAGE").toString().length()>0 && map.get("PAGESIZE").toString().length()>0) {//前端有传分页参数时就添加分页查询条件

int currenpage = Integer.parseInt(map.get("CURRENTPAGE").toString());

int pagesize = Integer.parseInt(map.get("PAGESIZE").toString());

currenpage = ((currenpage - 1) * pagesize);

whereSql.append(" limit " + currenpage + "," + pagesize);

}

rs = wmTblWorkorderMapper.getList(whereSql.toString());

return rs;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

mapper:

List> getList(@Param("whereSql") String whereSql);

mapper对应的xml:

SELECT * FROM user a WHERE 1=1 ${whereSql}

ps:想认识博主么?想了解学习更多知识么?不妨关注微信公众号:面向对象缘来是你

你可能感兴趣的:(java拼接xml)