1.枚举类
/**
* 排序枚举
*/
public enum OrderByEnum {
/**
* 降序
*/
desc,
asc,
;
}
2.mapper
2.1注意para参数类型为string
/**
* 展示个人账单以以及关联的种类
* @param userId
* @return
*/
List<SingleAccount> selectSingleAccountListAndCategory(@Param("userId") Long userId, @Param("orderByEnum")String orderByEnum);
2.2mapper调用
List<SingleAccount> list = singleAccountMapper.selectSingleAccountListAndCategory(userId,orderBy.name());
- 动态sql
<select id="selectSingleAccountListAndCategory" resultMap="SingleAccountResultAndCategory">
SELECT sa.money,sa.category_id,sa.remark,sa.expenses_income, c.id,c.category,c.icon,c.type
FROM single_account sa
LEFT JOIN category c ON sa.category_id = c.id
WHERE sa.user_id = 1
and c.category IS NOT NULL
order by sa.money
<choose>
<when test="orderByEnum == 'asc'">asc</when>
<when test="orderByEnum == 'desc'">desc </when>
</choose>
</select>