springboot2 mybatis-plus 自定义查询
使用的是mybatis-plus代码生成器生成的文件
1、在mapper接口文件中使用如下方式定义:(有点复杂,删了不少,但功能比较全了,理解灵魂)
@Select({"<script>",
"SELECT",
"DR.id", // 删了很多,查询的值,标题为id
"DR.type AS type", // ... 查询的值,重命名为type
"DATE_FORMAT(DR.date,'%Y-%m-%d %H:%i:%s') AS time", // 返回的日期格式
"FROM table DR",
"INNER JOIN table_one D ON DR.one_id = D.id", // 内联查询
"LEFT JOIN table_two UM ON DR.two_id = UM.id",
"LEFT JOIN table_two UR ON DR.man_id = UR.id",
"WHERE DR.teamId = #{teamId} and DR.state = 1",
"<when test='userId != null'> AND DR.userId = #{userId} </when>", // 根据参数增加条件
"<when test='date != null'> AND DR.date BETWEEN STR_TO_DATE(CONCAT(#{date}, ' 00:00:00'),'%Y-%m-%d %H:%i:%s') AND STR_TO_DATE(CONCAT(#{date}, ' 23:59:59'),'%Y-%m-%d %H:%i:%s') </when>", // 时间段
"ORDER BY DR.date DESC", // 排序
"LIMIT #{start}, #{count}", // 分页
"</script>",})
List<Map> getList(@Param("teamId") String teamId,
@Param("start") Integer start,
@Param("count") Integer count,
@Param("userId") String userId,
@Param("state") Integer state,
@Param("date") String date,
);
2、在实现类中如此调用:
public List<Map> getList( String teamId, Integer start, Integer count, String userId, Integer state, String date, ) { return this.baseMapper.getList(teamId, start, count, userId, state, date); }
3、接口中使用时注入实现类,调用实现类方法,传入参数。
知止而后有定;定而后能静;静而后能安;安而后能虑;虑而后能得。

浙公网安备 33010602011771号