07 MyBatis——三种查询方式以及返回类型

三种查询方式

在使用SQLSession调用查询方式时,有三种查询方式

1.selectList(方法) 返回值为List<ResultType 属性控制>

2.selectOne(方法) 返回一个Object,适用于返回结果只是变量或一行数据时

3.selectMap(方法,列名)  以列名为key,以列名所在的行的对象为value存入map中,如:

Map<Object, Object> map = session.selectMap("cn.xiaohei.mapper.FlowerMapper.selAll", "name");

 

selectMap中的两个参数分别为:

  • 调用Mapper中的查询方法selAll
  • 以name列的值为key

表中数据如:

 查询结果如(Flower为实体对象):

{矮牵牛=Flower [id=1, name=矮牵牛, price=2.5, production=南美阿根廷], 的=Flower [id=5, name=的, price=33.0, production=天庭], 王母娘娘=Flower [id=6, name=王母娘娘, price=33.0, production=], 半枝莲=Flower [id=3, name=半枝莲, price=4.3, production=巴西], 百日草=Flower [id=2, name=百日草, price=5.0, production=墨西哥], ???????¨??¨?=Flower [id=4, name=???????¨??¨?, price=23.0, production=?¤????]}

返回类型

在Mapper中,resultType这一项的返回值可选如下(左列):

猜你喜欢

转载自www.cnblogs.com/Scorpicat/p/12408030.html
07