四、MyBatis的各种查询功能
1、查询一个实体类对象
若查询出的数据只有一条,则可以通过实体类对象或者集合接收。如果使用实体类接收多条数据,就会报异常TooManyResultsException。
1)和第三章一样创建一个模块,名为MyBatis-查询功能(可以直接复制第三章的模块然后导入)。
2)在mapper包下新建mapper接口SelectMapper.java
3)在mapper目录下新建mapper映射文件SelectMapper.xml
4)使用实体类或者list集合接收一条数据
-
使用实体类接收:
编写SelectMapper.java文件:
//根据id查询用户信息 User getUserById(@Param("id") Integer id);
编写SelectMapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <!--mybatis配置文件中的约束--> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--将该mapper映射文件与UserMapper接口关联--> <mapper namespace="com.atguigu.mybatis.mapper.SelectMapper"> <!--根据id获取用户信息,关联SelectMapper接口中的getUserById(@Param("id") Integer id)方法--> <select id="getUserById" resultType="User"> select * from t_user where id = #{id} </select> </mapper>
在test包下创建TestSelectMapper测试类并编写:
/** * MyBatis的各种查询功能: * 1.若查询出的数据只有一条 * a>可以通过实体类对象接收 * b>可以通过list集合接收 * c>可以通过map集合接收 * 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, username=admin} * 2.若查询处出的数据有多条 * a>可以通过list集合来接收 * b>可以通过map集合接收 * c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为value, * 以某个字段的的值作为key,放在同一个map集合中 * 注意:一定不能通过实体类对象接收,否则会抛出异常TooManyResultsException * * MyBatis中设置了默认的类型别名: * java.lang.Integer --> int, Integer * int --> _int, _integer * Map --> map * String --> string */ //测试根据id查询用户信息的功能 @Test public void testGetUserById(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); System.out.println(mapper.getUserById(3)); }
-
使用map集合接收:
编写SelectMapper.java文件:
//根据id查询用户信息 //User getUserById(@Param("id") Integer id); List<User> getUserById(@Param("id") Integer id);
编写SelectMapper.xml文件:
<!--根据id获取用户信息,关联SelectMapper接口中的getUserById(@Param("id") Integer id)方法--> <select id="getUserById" resultType="User"> select * from t_user where id = #{id} </select>
在test包下创建TestSelectMapper测试类并编写:
/** * MyBatis的各种查询功能: * 1.若查询出的数据只有一条 * a>可以通过实体类对象接收 * b>可以通过list集合接收 * c>可以通过map集合接收 * 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, username=admin} * 2.若查询处出的数据有多条 * a>可以通过list集合来接收 * b>可以通过map集合接收 * c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为value, * 以某个字段的的值作为key,放在同一个map集合中 * 注意:一定不能通过实体类对象接收,否则会抛出异常TooManyResultsException * * MyBatis中设置了默认的类型别名: * java.lang.Integer --> int, Integer * int --> _int, _integer * Map --> map * String --> string */ //测试根据id查询用户信息的功能 @Test public void testGetUserById(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); System.out.println(mapper.getUserById(3)); }
2、查询一个list集合
编写SelectMapper.java文件:
//根据id查询用户信息(使用list集合接收) List<User> getAllUser();
编写SelectMapper.xml文件:
<!--获取所有用户信息,关联SelectMapper接口中的getAllUser()方法--> <select id="getAllUser" resultType="User"> select * from t_user </select>
在test包下创建TestSelectMapper测试类并编写:
//测试获取所有用户信息的功能 @Test public void testGetAllUser(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); System.out.println(mapper.getAllUser()); }
3、查询单个数据
-
查询单行单列时:
编写SelectMapper.java文件:
//查询用户信息的总记录数 Integer getCount();
编写SelectMapper.xml文件:
<!--查询用户信息的总记录数,关联SelectMapper接口中的getCount()方法--> <!-- 因为getCount()方法的返回值为Integer,所以resultType应该为java.lang.Integer 或者Integer或者int或者Int,而非User --> <select id="getCount" resultType="java.lang.Integer"> select count(*) from t_user </select>
在test包下创建TestSelectMapper测试类并编写:
/** * MyBatis的各种查询功能: * 1.若查询出的数据只有一条 * a>可以通过实体类对象接收 * b>可以通过list集合接收 * c>可以通过map集合接收 * 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, username=admin} * 2.若查询处出的数据有多条 * a>可以通过list集合来接收 * b>可以通过map集合接收 * c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为value, * 以某个字段的的值作为key,放在同一个map集合中 * 注意:一定不能通过实体类对象接收,否则会抛出异常TooManyResultsException * * MyBatis中设置了默认的类型别名: * java.lang.Integer --> int, Integer * int --> _int, _integer * Map --> map * String --> string */ //测试查询用户信息的总记录数的功能 @Test public void testGetCount(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); System.out.println(mapper.getCount()); }
4、查询一条数据为map集合
编写SelectMapper.java文件:
//根据id查询用户信息为一个map集合 Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
编写SelectMapper.xml文件:
<!--根据id查询用户信息为一个map集合,关联SelectMapper接口中的getUserByIdToMap(@Param("id") Integer id)方法--> <!--由于查询结果返回给getUserByIdToMap()方法是一个Map集合,所以resultType为map--> <select id="getUserByIdToMap" resultType="map"> select * from t_user where id = #{id} </select>
在test包下创建TestSelectMapper测试类并编写:
/** * MyBatis的各种查询功能: * 1.若查询出的数据只有一条 * a>可以通过实体类对象接收 * b>可以通过list集合接收 * c>可以通过map集合接收 * 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, username=admin} * 2.若查询处出的数据有多条 * a>可以通过list集合来接收 * b>可以通过map集合接收 * c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为value, * 以某个字段的的值作为key,放在同一个map集合中 * 注意:一定不能通过实体类对象接收,否则会抛出异常TooManyResultsException * * MyBatis中设置了默认的类型别名: * java.lang.Integer --> int, Integer * int --> _int, _integer * Map --> map * String --> string */ //测试根据id查询用户信息为一个map集合的功能 @Test public void testUserByIdToMap(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); System.out.println(mapper.getUserByIdToMap(3)); }
5、查询多条数据为map集合
-
通过map类型的list集合来接收数据:
编写SelectMapper.java文件:
//查询所有用户信息为map集合 //将多条Map集合放到一个list中返回 List<Map<String, Object>> getAllUserToMap();
编写SelectMapper.xml文件:
<!--查询所有用户信息为map集合,关联SelectMapper接口中的getAllUserToMap()方法--> <select id="getAllUserToMap" resultType="map"> select * from t_user </select>
在test包下创建TestSelectMapper测试类并编写:
//测试根据id查询用户信息为一个map集合的功能 @Test public void testGetAllUserToMap(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); System.out.println(mapper.getAllUserToMap()); }
-
通过@MapKey注解来设置map集合键,并直接使用map集合来接收数据:
编写SelectMapper.java文件:
//查询所有用户信息为map集合 //将多条Map集合放到一个list中返回 //List<Map<String, Object>> getAllUserToMap(); //@MapKey("id"):设置当前map集合的键为id @MapKey("id") Map<String, Object> getAllUserToMap();
编写SelectMapper.xml文件:
<!--查询所有用户信息为map集合,关联SelectMapper接口中的getAllUserToMap()方法--> <select id="getAllUserToMap" resultType="map"> select * from t_user </select>
在test包下创建TestSelectMapper测试类并编写:
/** * MyBatis的各种查询功能: * 1.若查询出的数据只有一条 * a>可以通过实体类对象接收 * b>可以通过list集合接收 * c>可以通过map集合接收 * 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, username=admin} * 2.若查询处出的数据有多条 * a>可以通过list集合来接收 * b>可以通过map集合接收 * c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为value, * 以某个字段的的值作为key,放在同一个map集合中 * 注意:一定不能通过实体类对象接收,否则会抛出异常TooManyResultsException * * MyBatis中设置了默认的类型别名: * java.lang.Integer --> int, Integer * int --> _int, _integer * Map --> map * String --> string */ //测试根据id查询用户信息为一个map集合的功能 @Test public void testGetAllUserToMap(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); System.out.println(mapper.getAllUserToMap()); }