返回User类型的list
<select id="queryAllUser" resultType="org.example.entity.User"> select * from t_user </select>
public interface UserMapper { public void addUser(); public List<User> queryAllUser(); public User queryUserByid(@Param("name") String name); }
@Test public void queryAllUser(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.queryAllUser(); System.out.println(users); }
查询所有用户信息为map集合(方法1)
<select id="queryAllUser" resultType="map"> select * from t_user </select>
public interface UserMapper { public void addUser(); public User queryUserByid(@Param("name") String name); public List<Map<String, Object>> queryAllUser(); }
@Test public void queryAllUser(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<Map<String, Object>> maps = mapper.queryAllUser(); System.out.println(maps); }
[{gender=男, name=tom, id=1, age=20, [email protected]}, {gender=女, name=luccy, id=2, age=10, [email protected]}]
查询所有用户信息为map集合(方法2)
<select id="queryAllUser" resultType="map"> select * from t_user </select>
public interface UserMapper { public void addUser(); public User queryUserByid(@Param("name") String name); @MapKey("id")//设置id为map集合的key public Map<String, Object> queryAllUser(); }
@Test public void queryAllUser(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Map<String, Object> maps = mapper.queryAllUser(); System.out.println(maps); }
{1={gender=男, name=tom, id=1, age=20, [email protected]}, 2={gender=女, name=luccy, id=2, age=10, [email protected]}}
标签:name,id,UserMapper,多条,查询,queryAllUser,void,mybatis,public From: https://www.cnblogs.com/ixtao/p/17161941.html