Mybatis三种查询方式
1.selectList()返回值为List<resultType属性控制>
适用于查询结果都需要遍历的需求:
List<Flower> list = session.selectList(com.mapper.FlowerMapper.selAll); for(Flower flower :list) { System.out.println(flower.toString()); }
2.selectOne()返回值Object
适用于返回结果只是变量或一行数据时
int count = session.selectOne("com.mapper.FlowerMapper.selById"); System.out.println(count);
3.selectMap()返回值Map
适用于需要在查询结果中通过某列的值取到这行数据的需求
Map<key,resultType控制>
Map<Object, Object> map = session.selectMap("com.mapper.FlowerMapper.selMap", "name");
System.out.println(map);
xml配置文档中对应代码
<select id="selAll" resultType="com.pojo.Flower"> select * from flower </select> <select id="selById" resultType="int"> select count(*) from flower </select> <select id="selMap" resultType="com.pojo.Flower"> select * from flower </select>