若是使用MyBatis註解方式寫sql,又想要XML寫法的判斷入參拼接條件,能夠經過@SelectProvider方式實現。sql
public class RateProvider { private final String table_name = "rate_finish"; public String getFinishRate(Map<String, Object> para) { String sql = "SELECT homework_type,device_type,`level`,finish_count,count,create_at FROM rate_finish "; sql += "WHERE 1=1 "; if (para.get("deviceType") != null && !StringUtils.isEmpty(para.get("deviceType").toString())) { sql += "AND device_type = '" + para.get("deviceType").toString() + "' "; } if (para.get("homeworkType") != null && !StringUtils.isEmpty(para.get("homeworkType").toString())) { sql += "AND homework_type = '" + para.get("homeworkType").toString() + "' "; } if (para.get("date") != null && !StringUtils.isEmpty(para.get("date").toString())) { sql += "AND create_at = '" + para.get("date").toString() + "' "; } sql += "ORDER BY homework_type,device_type,`level`"; return sql; } }
這裏的para會包含6個值,也就是2倍的入參參數,能夠經過key方式或者index方式獲取對應參數。app
public class RateFinish extends AbstractModel { private int id; private String deviceType; private String level; private String finishCount; private String count; private String homeworkType; private String createAt;
@SelectProvider(type = RateProvider.class, method = "getFinishRate") @Results({ @Result(property = "homeworkType", column = "homework_type"), @Result(property = "deviceType", column = "device_type"), @Result(property = "level", column = "level"), @Result(property = "finishCount", column = "finish_count"), @Result(property = "count", column = "count"), @Result(property = "createAt", column = "create_at") }) List<RateFinish> getFinishRate( @Param("deviceType") String deviceType, @Param("homeworkType") String homeworkType, @Param("date") String date);