Mybatis学习第3节 -- 排序

Posted 积水成渊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis学习第3节 -- 排序相关的知识,希望对你有一定的参考价值。

首先在接口中添加方法
List<Shop> getShopListOrderById();
在mapper映射文件中添加相应语句
注意: 返回类型仍然是元素的类型,而不需要list, mybatis会自动组装成list
<select id="getShopListOrderById"  resultMap="simpleResultMap" >
select * from tb_shop ORDER BY `shop_id`
</select>
添加测试用例
@Test
public void testGetShopListOrderById() {
String template = "查询结果: %s";
SqlSession session = MyBatisUtil.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
System.out.printf(template, mapper.getShopListOrderById());
session.close();
}
结果
查询结果: [Shop{id=1, ownerId=1, areaId=3, categoryId=14, name=‘正式店铺名称‘, desc=‘测试描述‘, addr=‘正式地址‘, phone=‘13810524086‘, image=‘/upload/item/shop/1/2017091621545314507.jpg‘, priority=10, createTime=2017-08-03, lastEditTime=2017-09-16, enableStatus=0, advice=‘审核中‘}, Shop{id=28, ownerId=1, areaId=2, categoryId=22, name=‘小黄人主题奶茶店‘, desc=‘不接受预订,请直接来店里进行消费‘, addr=‘位于东苑2号‘, phone=‘13810524086‘, image=‘/upload/images/item/shop/28/2017092601041469991.png‘, priority=50, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}, Shop{id=29, ownerId=1, areaId=3, categoryId=22, name=‘暴漫奶茶店‘, desc=‘过来喝喝就知道啦,你是我的奶茶‘, addr=‘西苑1号‘, phone=‘1211334565‘, image=‘/upload/images/item/shop/29/2017092601054939287.jpg‘, priority=40, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}, Shop{id=30, ownerId=1, areaId=2, categoryId=20, name=‘彪哥大排档‘, desc=‘敢说不好吃吗‘, addr=‘东苑1号‘, phone=‘13628763625‘, image=‘/upload/images/item/shop/30/2017092601063878278.jpg‘, priority=30, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}, Shop{id=31, ownerId=1, areaId=2, categoryId=20, name=‘威哥大排档‘, desc=‘干掉彪哥大排档‘, addr=‘东苑南路‘, phone=‘126554437261‘, image=‘/upload/images/item/shop/31/2017092601072177572.jpg‘, priority=20, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}, Shop{id=32, ownerId=1, areaId=2, categoryId=22, name=‘你是我的奶茶‘, desc=‘奶茶店再次来袭‘, addr=‘东苑六路‘, phone=‘13652384615‘, image=‘/upload/images/item/shop/32/2017092601081463136.jpg‘, priority=10, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}, Shop{id=35, ownerId=8, areaId=2, categoryId=22, name=‘奶茶来了‘, desc=‘奶茶来了‘, addr=‘西苑7路‘, phone=‘null‘, image=‘null‘, priority=0, createTime=null, lastEditTime=null, enableStatus=0, advice=‘null‘}, Shop{id=38, ownerId=1, areaId=2, categoryId=10, name=‘杰克的店铺abcd‘, desc=‘etc test‘, addr=‘etc test‘, phone=‘etc test‘, image=‘etc test‘, priority=null, createTime=2018-06-02, lastEditTime=null, enableStatus=1, advice=‘审核中-‘}]Resetting autocommit to true on JDBC Connection [[email protected]]
Closing JDBC Connection [[email protected]]
如果根据名称排序的话,需要对字符串进行统一的编码转换,不然中文可能会出问题
gb2312中的魅字排序会出问题,而gbk没有问题
<select id="getShopListOrderByName"  resultMap="simpleResultMap" >
select * from tb_shop ORDER BY CONVERT(shop_name USING gbk)
</select>

 

以上是关于Mybatis学习第3节 -- 排序的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis学习第4节 -- 多参数传递

Mybatis学习第23节 -- 鉴别器 discriminator

Mybatis学习第20节 -- 嵌套结果

Mybatis学习第25节 -- 懒加载 积极与不积极

Mybatis学习第5节 -- 插入并获取ID

Mybatis学习第2节 -- 模糊查询之#和$的区别