赞
踩
//根据id查询用户信息
User getUserById(@Param("id") Integer id);
<!--User getUserById(@Param("id") Integer id);-->
<select id="getUserById" resultType="User">
select * from t_user where id=#{id}
</select>
@Test
public void testGrtUserById(){
SqlSession sqlSession= SqlSessionUtil.getSqlSession();
SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
User user=mapper.getUserById(1);
System.out.println(user);
}
//查询所有用户信息
List<User> getAllUser();
<!--List<User> getAllUser();-->
<select id="getAllUser" resultType="User">
select * from t_user;
</select>
@Test
public void testGrtAllUser(){
SqlSession sqlSession= SqlSessionUtil.getSqlSession();
SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
List<User> list=mapper.getAllUser();
list.forEach(System.out::println);
}
当查询的数据为多条时,不能使用实体类作为返回值,否则会抛出异常TooManyResultsException;但是若查询的数据只有一条,可以使用实体类或集合作为返回值
/**
* 查询用户的总数量
* @return
* 在MyBatis中,对于Java中常用的类型都设置了类型别名
* 例如: java.lang.Integer-->int|integer
* 例如: int-->_int|_integer
* 例如: Map-->map,List-->list
*/
Integer getCount();
<!--Integer getCount();-->
<select id="getCount" resultType="integer">
select count(*) from t_user
</select>
@Test
public void testGetCount(){
SqlSession sqlSession= SqlSessionUtil.getSqlSession();
SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
Integer count=mapper.getCount();
System.out.println(count);
}
//根据用户id查询用户信息为map集合
Map<String ,Object> getUserByIdToMap(@Param("id") int id);
<!--Map<String ,Object> getUserByIdToMap(@Param("id") int id);-->
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id=#{id};
</select>
@Test
public void testGetUserByIdToMap(){
SqlSession sqlSession= SqlSessionUtil.getSqlSession();
SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
Map<String ,Object> map=mapper.getUserByIdToMap(1);
System.out.println(map);
}
注意:当查询的数据中,有字段为空,则不会放到Map集合中
将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,此时可以将这些map放在一个list集合中获取
//查询所有用户信息为map集合
List<Map<String ,Object>> getAllUserToMap();
<!--Map<String ,Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user;
</select>
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession= SqlSessionUtil.getSqlSession();
SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
List<Map<String ,Object>> list= mapper.getAllUserToMap();
System.out.println(list);
结果:
[{password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},
{password=123456, gender=女, id=3, age=20, email=12345@qq.com, username=bbss},
{password=123456, gender=女, id=4, age=33, email=456789@qq.com, username=root}]
将表中的数据以map集合的方式查询,一条数据对应一map;若有多条数据,就会产生多个map集合,并且最终要以一个map的方式返回数据,此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的map集合
//查询所有用户信息为map集合
@MapKey("id")
Map<String ,Object> getAllUserToMap();
<!--Map<String ,Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user;
</select>
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession= SqlSessionUtil.getSqlSession();
SelectMapper mapper=sqlSession.getMapper(SelectMapper.class);
Map<String ,Object> map= mapper.getAllUserToMap();
System.out.println(map);
结果:
{1={password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},
3={password=123456, gender=女, id=3, age=20, email=12345@qq.com, username=bbss},
4={password=123456, gender=女, id=4, age=33, email=456789@qq.com, username=root}}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。