mybatis排序

添加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编码格式即可。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值