添加mapper:
<!--排序必须使用$-->
<select id="selectBlogWithSort" resultType="Blog" resultMap="blogResultMap">
select * from Blog order by ${value}
</select>
添加接口:
List<Blog> selectBlogWithSort(String keyWord);
添加测试用例:
@Test
public void selectBlogWithSort() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
List<Blog> blogs = mapper.selectBlogWithSort("title");
sqlSession.close();
logger.info("blog is:{}", blogs);
}
运行结果:
Blog is:[
Blog(id=4, title=a, authorId=4, state=ACTIVE, featured=true, style=222),
Blog(id=5, title=b, authorId=5, state=ACTIVE, featured=true, style=222),
Blog(id=1, title=My Colourful Garden, authorId=1, state=ACTIVE, featured=true, style=pink),
Blog(id=2, title=平凡的世界, authorId=2, state=ACTIVE, featured=true, style=black),
Blog(id=3, title=活着, authorId=3, state=ACTIVE, featured=true, style=res)]
但是:我们发现只有英文的排序了,中文的没有排序???怎么解决呢???
使用convert函数:
<select id="selectBlogWithSort" resultType="Blog" resultMap="blogResultMap">
select * from Blog order by convert (${value} using gbk)
</select>
blog is:[Blog(id=4, title=a, authorId=4, state=ACTIVE, featured=true, style=222),
Blog(id=5, title=b, authorId=5, state=ACTIVE, featured=true, style=222),
Blog(id=1, title=My Colourful Garden, authorId=1, state=ACTIVE, featured=true, style=pink),
Blog(id=3, title=活着, authorId=3, state=ACTIVE, featured=true, style=res),
Blog(id=2, title=平凡的世界, authorId=2, state=ACTIVE, featured=true, style=black)]
使用gbk编码格式即可。。