MyBatis使用@SelectProvider拼接sql语句
2018-01-17 00:00
1916 查看
如果使用MyBatis注解方式写sql,又想要XML写法的判断入参拼接条件,可以通过@SelectProvider方式实现。
这里的para会包含6个值,也就是2倍的入参参数,可以通过key方式或者index方式获取对应参数。
新建Provider
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方式获取对应参数。
新建Entity
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;
编写Mapper方法
@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);
相关文章推荐
- mybatis-plus 使用Condition拼接Sql语句各方法的作用
- ssm项目使用Mybatis动态拼接sql语句,生成的sql中文全部显示为???的问题(配置文件sql语句中文解析问题)
- 使用 SELECT 查询语句拼接 迁移表至其他表空间的 SQL 脚本 语句
- SQL SELECT DISTINCT 语句使用
- sql存储过程,语句拼接,使用游标
- 使用正则表达式检验sql语句(只支持select(不含函数,子
- 教您如何使用SQL中的SELECT LIKE like语句
- 使用 Excel 快速拼接 sql 语句
- 使用in关键字来限制SQL语句中的select查询结果
- 代码里使用字符串操作来拼接sql语句的坏处
- 数据库SQL文使用命令参数代替拼接SQL语句
- 使用SQL语句对Oracle数据库数据进行拼接
- yii框架中findall方法取数据使用总结,包括select各种条件,where条件,order by条件,limit限制以及使用单纯sql语句query时占位符的使用等
- 在ASP中使用SQL语句之1:SELECT 语句
- mybatis下使用log4j打印sql语句和执行结果
- 使用 Excel 快速拼接 sql 语句
- 在编写PL/SQL代码中使用SELECT语句时如何避免例外发生
- SQL SELECT INTO 语句使用问题
- 使用SQL中SELECT语句的使用条件逻辑
- SQL Cookbook:一、检索记录(8)在SELECT语句中使用条件逻辑