`MyBatis`各种查询功能
MyBatis
各种查询功能【非常重要】
1.查询一个实体类对象
若查询出的数据只有一条:
- 可以通过实体类对象接收
- 可以通过
List
集合接收 - 可以通过
Map
集合接收,在select
的resultType
或resultMap
要传入别名为map
【用于json
数据传输】
案例:
Mapper
接口
/**
*当查询的返回只有一条记录时,
* 1.可以用JavaBean类进行接收
* 2.可以用List集合进行接收
*/
// 根据ID查询用户信息 , 用User实体类进行接收
User getUserByID(@Param("id") Integer id);
// 根据ID查询用户信息 , 用List集合进行接收
List<User> getUserByID2(@Param("id") Integer id);
Mapper.xml
文件
<!--getUserByID根据ID查询用户信息-->
<select id="getUserByID" resultType="User">
select * from t_user where id = #{id};
</select>
<!--List<User> getUserByID2(@Param("id") Integer id);根据ID查询用户信息-->
<select id="getUserByID2" resultType="User">
select * from t_user where id = #{id};
</select>
Test
类
// 查询返回一条记录的情况
@Test
public void testGetUserByID(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
//查询返回一条记录用JavaBean类进行接收
// User user = mapper.getUserByID(1);
// System.out.println(user);
//查询返回一条记录用List集合进行接收
List<User> list = mapper.getUserByID2(1);
list.forEach(user -> System.out.println(user));
}
2.查询一个List
集合【非常重要】
若查询出的数据有多条:
- 可以通过实体类类型的
List
集合接收【就是泛型是JavaBean
】 - 可以通过
Map
类型的List
集合接收【就是泛型是Map
集合】在select
的resultType
或resultMap
要传入别名为map
【用于json
数据传输】 - 可以在
Mapper
接口的方法添加上@MapKey
注解,此时就可以将每条数据转换的Map
集合作为值,以某个字段的值作为键,放在同一个Map
集合中
注意是:一定不能通过实体类对象接收,此时会抛出异常TooManyResultException
Mapper
接口
/**
* 当查询返回是多条记录时,
* 1.可以用List集合进行接收
*
* 注意是:一定不能使用JavaBean接收,否则会报:TooManyResultsException
*/
List<User> getAllUser();
Mapper.xml
文件
<!--getAllUser查询所有用户信息-->
<select id="getAllUser" resultType="User">
select * from t_user;
</select>
test
类
// 查询所有用户信息
@Test
public void testAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
// 查询用户信息
List<User> allUser = mapper.getAllUser();
allUser.forEach(user -> System.out.println(user));
}
3.查询单个数据【就是返回单行单列】
/**
* 查询用户的总记录数
* @return
* 在MyBatis中,对于Java中常用的类型都设置了类型别名
* 例如:java.lang.Integer-->int|integer
* 例如:int-->_int|_integer
* 例如:String --> string
* 例如:Map-->map,List-->list
*/
int getCount();
案例:查询所以记录条数
Mapper
接口
// 查询单个数据【就是返回单行单列】
Integer getCount();
// 例如查询 id为1的email
String getEmailByID(@Param("id") Integer id);
Mapper.xml
文件
<!--Integer getCount();查询所有记录数-->
<select id="getCount" resultType="java.lang.Integer">
select count(*) from t_user;
</select>
<!--String getEmailByID(Integer id);根据ID查询Email-->
<select id="getEmailByID" resultType="java.lang.String">
select email from t_user where id = #{id};
</select>
test
类
// 查询所有记录数
@Test
public void testGetCount(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
// 查询所有记录条数
Integer count = mapper.getCount();
System.out.println(count);
// 根据ID查询 email
String email = mapper.getEmailByID(1);
System.out.println(email);
}
4.查询一条数据为Map
集合【非常重要在JSON
中使用】
本质是就是将查询到结果的属性作为Map
集合的键查询到的属性值作为Map
集合的值
Mapper
接口
/**
* 查询返回一条记录为Map集合
* 查询到的属性作为Map集合的键
* 查询到的值作为Map集合的值
*/
Map<String,Object> getUserByIdToMap(@Param("id")Integer id);
Mapper.xml
文件
<!--
Map<String,Object> getUserByIdToMap(@Param("id")Integer id);
查询到的结果为一条记录作为Map集合,查询到属性为Map键,查询到值为Map值
-->
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id = #{id};
</select>
resultType="map"
说明查询到数据封装Map
集合
test
类
// 查询到为一条记录封装为Map集合
@Test
public void testGetUserByIdToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> toMap = mapper.getUserByIdToMap(1);
System.out.println(toMap);
}
5.查询多条数据为Map
集合【非常重要】
方式一:查询到的多条放在List
集合中,泛型为Map
集合
例如:
/**
* 查询所有用户信息为map集合
* @return
* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,此
时可以将这些map放在一个list集合中获取
*/
List<Map<String, Object>> getAllUserToMap();
<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
案例
Mapper
接口
/**
* 查询返回多条记录
* 情况一:查询到的记录每条记录都封装成一个Map集合,再将每一个Map封装List集合
*/
//注意是:由于查询返回是多条记录不能使用一个Map集合接收,否则报错为:TooManyResultsException
// 如:Map<String,Object> getAllUserToMap();错误,返回多条记录只用一个Map接收
List<Map<String,Object>> getAllUserToMap();
mapper.xml
文件
<!--
List<Map<String,Object>> getAllUserToMap();
查询到多条记录,每条都封装成Map集合,再放在List集合中
-->
<select id="getAllUserToMap" resultType="Map">
select * from t_user;
</select>
test
类
// 情况一:每条记录封装为Map集合,再多条记录封装为List集合泛型为Map
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<Map<String, Object>> mapList = mapper.getAllUserToMap();
for (Map<String , Object> map:
mapList) {
System.out.println(map);
/**
* {password=123, sex=女, id=1, age=23, email=168@qq.com, username=明天}
* {password=123, sex=男, id=2, age=22, email=123@qq.com, username=海康}
* {password=123, sex=男, id=3, age=22, email=123@qq.com, username=湛江}
* {password=123, sex=男, id=4, age=22, email=123@qq.com, username=西安}
*/
}
}
方式二:查询到的多条记录作为Map
值,并且使用@MapKey
注解设置Map
集合的键【注意:@Mapkey
的值必须是唯一】
注意是:@MapKey
注解中的值必须是唯一,如果不是唯一可以将Map
集合中的其他值覆盖掉,所以必须是唯一【一般选择表的ID
】
例如:
/**
* 查询所有用户信息为map集合
* @return
* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,并
且最终要以一个map的方式返回数据,此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的
map集合
*/
@MapKey("id")
Map<String, Object> getAllUserToMap();
<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
结果:
<!--
{
1={password=123456, sex=男, id=1, age=23, username=admin},
2={password=123456, sex=男, id=2, age=23, username=张三},
3={password=123456, sex=男, id=3, age=23, username=张三}
}
-->
案例:
Mapper
接口
/**
* 查询每条记录都封装为Map集合,将作为Map集合的值,并且使用`@MapKey`
* 注意:`@MapKey``@MapKey`注解中的值必须是唯一,如果不是唯一可以将`Map`集合中的其他值覆盖掉,
* 所以必须是唯一【一般选择表的`ID`】
*/
@MapKey("id")
Map<Integer,Object> getAllUserToMap2();
Mapper.xml
文件
<!--
Map<String,Object> getAllUserToMap2();
查询每条记录都封装为Map集合,将作为Map集合的值,并且使用`@MapKey`
注意:`@MapKey``@MapKey`注解中的值必须是唯一,如果不是唯一可以将`Map`集合中的其他值覆盖掉,
所以必须是唯一【一般选择表的`ID`】
-->
<select id="getAllUserToMap2" resultType="map">
select * from t_user;
</select>
test
类
// 情况二:每条记录封装为Map集合,并且选取表中唯一标识作为`@MapKey`的值
@Test
public void testGetAllUserToMap2(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<Integer, Object> map = mapper.getAllUserToMap2();
System.out.println(map);
Set<Integer> keySet = map.keySet();
for (Integer key:
keySet) {
System.out.println(key+"="+map.get(key));
}
}
综合:
Mapper
接口
/**
* 用于各种查询操作
*/
public interface SelectMapper {
/**
*当查询的返回只有一条记录时,
* 1.可以用JavaBean类进行接收
* 2.可以用List集合进行接收
*/
// 根据ID查询用户信息 , 用User实体类进行接收
User getUserByID(@Param("id") Integer id);
// 根据ID查询用户信息 , 用List集合进行接收
List<User> getUserByID2(@Param("id") Integer id);
/**
* 当查询返回是多条记录时,
* 1.可以用List集合进行接收
*
* 注意是:一定不能使用JavaBean接收,否则会报:TooManyResultsException
*/
List<User> getAllUser();
// 查询单个数据【就是返回单行单列】
Integer getCount();
// 例如查询 id为1的email
String getEmailByID(@Param("id") Integer id);
/**
* 查询返回一条记录为Map集合
* 查询到的属性作为Map集合的键
* 查询到的值作为Map集合的值
*/
Map<String,Object> getUserByIdToMap(@Param("id")Integer id);
/**
* 查询返回多条记录
* 情况一:查询到的记录每条记录都封装成一个Map集合,再将每一个Map封装List集合
*/
//注意是:由于查询返回是多条记录不能使用一个Map集合接收,否则报错为:TooManyResultsException
// 如:Map<String,Object> getAllUserToMap();错误,返回多条记录只用一个Map接收
List<Map<String,Object>> getAllUserToMap();
/**
* 查询每条记录都封装为Map集合,将作为Map集合的值,并且使用`@MapKey`
* 注意:`@MapKey``@MapKey`注解中的值必须是唯一,如果不是唯一可以将`Map`集合中的其他值覆盖掉,
* 所以必须是唯一【一般选择表的`ID`】
*/
@MapKey("id")
Map<Integer,Object> getAllUserToMap2();
}
Mapper.xml
文件
<mapper namespace="com.haikang.mybatis.mapper.SelectMapper">
<!--getUserByID根据ID查询用户信息-->
<select id="getUserByID" resultType="User">
select * from t_user where id = #{id};
</select>
<!--List<User> getUserByID2(@Param("id") Integer id);根据ID查询用户信息-->
<select id="getUserByID2" resultType="User">
select * from t_user where id = #{id};
</select>
<!--getAllUser查询所有用户信息-->
<select id="getAllUser" resultType="User">
select * from t_user;
</select>
<!--Integer getCount();查询所有记录数-->
<select id="getCount" resultType="java.lang.Integer">
select count(*) from t_user;
</select>
<!--String getEmailByID(Integer id);根据ID查询Email-->
<select id="getEmailByID" resultType="java.lang.String">
select email from t_user where id = #{id};
</select>
<!--
Map<String,Object> getUserByIdToMap(@Param("id")Integer id);
查询到的结果为一条记录作为Map集合,查询到属性为Map键,查询到值为Map值
-->
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id = #{id};
</select>
<!--
List<Map<String,Object>> getAllUserToMap();
查询到多条记录,每条都封装成Map集合,再放在List集合中
-->
<select id="getAllUserToMap" resultType="Map">
select * from t_user;
</select>
<!--
Map<String,Object> getAllUserToMap2();
查询每条记录都封装为Map集合,将作为Map集合的值,并且使用`@MapKey`
注意:`@MapKey``@MapKey`注解中的值必须是唯一,如果不是唯一可以将`Map`集合中的其他值覆盖掉,
所以必须是唯一【一般选择表的`ID`】
-->
<select id="getAllUserToMap2" resultType="map">
select * from t_user;
</select>
</mapper>
test
类
/**
* @Author 海康
* @Version 1.0
*/
public class TestSelectMapper {
// 查询到多条记录封装Map集合的两种情况
// 情况一:每条记录封装为Map集合,再多条记录封装为List集合泛型为Map
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<Map<String, Object>> mapList = mapper.getAllUserToMap();
for (Map<String , Object> map:
mapList) {
System.out.println(map);
/**
* {password=123, sex=女, id=1, age=23, email=168@qq.com, username=明}
* {password=123, sex=男, id=2, age=22, email=123@qq.com, username=海康}
* {password=123, sex=男, id=3, age=22, email=123@qq.com, username=湛江}
* {password=123, sex=男, id=4, age=22, email=123@qq.com, username=西安}
*/
}
}
// 情况二:每条记录封装为Map集合,并且选取表中唯一标识作为`@MapKey`的值
@Test
public void testGetAllUserToMap2(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<Integer, Object> map = mapper.getAllUserToMap2();
System.out.println(map);
Set<Integer> keySet = map.keySet();
for (Integer key:
keySet) {
System.out.println(key+"="+map.get(key));
}
}
// 查询到为一条记录封装为Map集合
@Test
public void testGetUserByIdToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> toMap = mapper.getUserByIdToMap(1);
System.out.println(toMap);
}
// 查询所有记录数
@Test
public void testGetCount(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
// 查询所有记录条数
Integer count = mapper.getCount();
System.out.println(count);
// 根据ID查询 email
String email = mapper.getEmailByID(1);
System.out.println(email);
}
// 查询返回一条记录的情况
@Test
public void testGetUserByID(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
//查询返回一条记录用JavaBean类进行接收
// User user = mapper.getUserByID(1);
// System.out.println(user);
//查询返回一条记录用List集合进行接收
List<User> list = mapper.getUserByID2(1);
list.forEach(user -> System.out.println(user));
}
// 查询所有用户信息
@Test
public void testAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
// 查询用户信息
List<User> allUser = mapper.getAllUser();
allUser.forEach(user -> System.out.println(user));
}
}