【总结】
1.查询出的数据只有一条:
(1)可以通过实体类对象接收
(2)可以通过List集合接收(Eg1)
(3)可以通过map接收(Eg4)
2.若查询的数据有多条:
(1)可以通过List集合接收(Eg2)
(2)可以通过map类型的List集合接收(Eg5(1))
(3)可以在mapper接口的方法上添加
@MapKey
注解,此时就可以将每条数据转为map集合作为值,以某个字段的值作为键,放在同一个map集合里面。(Eg5(2))(注意)|(一定不能通过实体类对象接收,否则会抛出异常TooManyResultsException)
准备
(1)创建接口
(2)创建对应的映射文件
查询一个实体类对象
//SelectMapper 接口 public interface SelectMapper { /** * 查询用户信息 * @param id 用户id * @return 用户对象 */ User getUserById(Integer id); }
<!-- User getUserById(Integer id);--> <select id="getUserById" resultType="User"> select * from t_users where id= #{id} </select>
@Test public void getUserById(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); User result = mapper.getUserById(4); System.out.println(result); }
cc.mllt.sky.utils.SqlSessionUtils.java
package cc.mllt.sky.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class SqlSessionUtils { public static SqlSession getSqlSession(){ SqlSession sqlSession = null; try{ InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); sqlSession = sqlSessionFactory.openSession(true); }catch (IOException e){ e.printStackTrace(); } return sqlSession; } }
查询返回一个List集合
Eg1:查询出的数据只有一条
//SelectMapper 接口 public interface SelectMapper { /** * 查询用户信息 * @param id 用户id * @return 用户对象 */ List<User> getUserById(Integer id); }
<!-- User getUserById(Integer id);--> <select id="getUserById" resultType="User"> select * from t_users where id= #{id} </select>
@Test public void getUserById(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); List<User> result = mapper.getUserById(4); System.out.println(result); }
Eg2:查询出的数据有多条
//SelectMapper 接口 /** * 查询所有用户 * @return List<User> */ List<User> getUsers();
<!-- List<User> getUsers();--> <select id="getUsers" resultType="User"> select * from t_users </select>
@Test public void getUsers(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); List<User> result = mapper.getUsers(); System.out.println(result); }
其他返回数据类型(补充:MyBatis类型别名)
Eg3:返回数据为一行一列
//SelectMapper 接口 /** * 查询用户表行数 * @return Integer */ Integer getCount();
<!-- Integer getCount();--> <select id="getCount" resultType="java.lang.Integer"> select count(*) from t_users </select>
resultType中填写
Integer
、integer
、int
、Int
、_int
都不会报错原因在 官方手册
Configuration XML
>typeAliases
(类型别名)因为MyBatis中设置默认的类型别名
Alias | Mapped Type |
_int | int |
int | Integer |
integer | Integer |
Alias:别名
Mapped Type:映射地址
java.lang.Integer --> int , Integer
int --> _int , _integer
Map --> map
String --> string
@Test public void getCount(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); Integer result = mapper.getCount(); System.out.println(result); }
Eg4:返回一条数据 返回数据类型为Map集合
//SelectMapper 接口 /** * 根据用户id获取Map类型的信息 * @param id 用户id * @return Map类型用户信息 Map<String,Object> */ Map<String,Object> getUserByIdToMap(@Param("id") Integer id);
<!-- Map<String,Object> getUserByIdToMap(@Param("id") Integer id);--> <select id="getUserByIdToMap" resultType="map"> select * from t_users where userId= #{id} </select>
@Test public void getUserByIdToMap(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); Map<String, Object> result = mapper.getUserByIdToMap(4); System.out.println(result); }
获得的map集合结果以字段为键
输出结果如下
{userPassword=123456改了密码, userCount=test001, userGrade=1, userRegDate=2022-03-01T00:00, userName=测试用户改了名, userId=4, userBlance=0}
Eg5:返回多条数据 返回数据类型为map集合
(1)方式1: 将多个Map放在List中
//SelectMapper 接口 /** * 获取所有用户信息 * @return Map类型用户信息 Map<String,Object> */ List<Map<String,Object>> getusersToMap(); }
<!-- Map<String,Object> getusersToMap();--> <select id="getusersToMap" resultType="map"> select * from t_users </select>
@Test public void getUsersToMap(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); List<Map<String, Object>> result = mapper.getusersToMap(); System.out.println(result); }
(2)方式2: @MapKey()注解
//SelectMapper 接口 /** * 获取所有用户信息 * @return Map类型用户信息 Map<String,Object> */ @MapKey("userId")//MySQl中字段名,最好为key Map<String,Object> getusersToMap();
<!-- Map<String,Object> getusersToMap();--> <select id="getusersToMap" resultType="map"> select * from t_users </select>
@Test public void getUsersToMap(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); // List<Map<String, Object>> result = mapper.getusersToMap(); Map<String, Object> result = mapper.getusersToMap(); System.out.println(result); }
输出结果如下
{1={userPassword=qq2686485465, userCount=mllt9920, userGrade=999, userRegDate=2022-03-20T00:00, userName=萌狼蓝天, userId=1, userBlance=100000000}, 2={userPassword=134679852, userCount=xrilang, userGrade=888, userRegDate=2022-03-25T00:00, userName=向日狼, userId=2, userBlance=10240000}, 4={userPassword=123456改了密码, userCount=test001, userGrade=1, userRegDate=2022-03-01T00:00, userName=测试用户改了名, userId=4, userBlance=0}, 5={userPassword=123, userCount=mllt@xrilang.com, userGrade=0, userName=张三, userId=5, userBlance=0}}