6、MyBatis的各种查询功能
6.1、查询一个实体类对象
SelectMapper.java接口
/**
* 根据用户id查询用户信息
* @param id
* @return
*/
User getUserById(@Param("id") int id);
SelectMapper.xml
<!--User getUserById(@Param("id") int id)-->
<select id="getUserById" resultType="User">
select * from t_user where id = #{id}
</select>
SelectMapperTest.java
@Test
public void testGetUserById(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
}
运行测试
DEBUG 02-25 02:33:02,731==> Preparing: select * from t_user where id = ? (BaseJdbcLogger.java:137)
DEBUG 02-25 02:33:02,757==> Parameters: 1(Integer) (BaseJdbcLogger.java:137)
DEBUG 02-25 02:33:02,777<== Total: 1 (BaseJdbcLogger.java:137)
User{id=1, username=‘admin’, password=‘123456’, age=23, gender=‘男’, email=‘12345@qq.com’}
6.2、查询一个list集合
若sql语句查询的结果为多条时,一定不能以实体类类型作为方法的返回值,
否则会抛出异常 TooManyResultsException
若sql语句查询的结果为1条时,此时可以使用实体类类型或list集合类型作为方法的返回值
SelectMapper.java接口
/**
* 查询所有的用户信息
* @return
*/
List<User> getAllUser();
SelectMapper.xml
<select id="getAllUser" resultType="User">
select * from t_user
</select>
当查询的数据为多条时,不能使用实体类作为返回值,否则会抛出异常TooManyResultsException;
但是若sql语句查询的结果为1条时,此时可以使用实体类类型或list集合类型作为方法的返回值。
运行测试:
DEBUG 02-25 02:33:28,173==> Preparing: select * from t_user (BaseJdbcLogger.java:137)
DEBUG 02-25 02:33:28,198==> Parameters: (BaseJdbcLogger.java:137)
DEBUG 02-25 02:33:28,215<== Total: 4 (BaseJdbcLogger.java:137)
User{id=1, username=‘admin’, password=‘123456’, age=23, gender=‘男’, email=‘12345@qq.com’}
User{id=2, username=‘root’, password=‘123456’, age=33, gender=‘女’, email=‘123@qq.com’}
User{id=3, username=‘zhangsan’, password=‘123’, age=23, gender=‘男’, email=‘123@qq.com’}
User{id=4, username=‘lisi’, password=‘123’, age=null, gender=‘null’, email=‘null’}
6.3、查询单个数据
SelectMapper.java接口
查询用户总数
/**
* 查询用户的总数量
* @return
*/
Integer getCount();
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fan.mybatis.mapper.SelectMapper">
<!--
MyBatis中为Java中常用的类型设置了类型别名
Integer: Integer,int
int: _int,_integer
Map: map
String: string
-->
<select id="getCount" resultType="int">
select count(*) from t_user
</select>
</mapper>
SelectMapperTest.java
@Test
public void testGetCount(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Integer count = mapper.getCount();
System.out.println(count);
}
运行测试:
DEBUG 02-25 02:41:42,727==> Preparing: select count(*) from t_user (BaseJdbcLogger.java:137)
DEBUG 02-25 02:41:42,749==> Parameters: (BaseJdbcLogger.java:137)
DEBUG 02-25 02:41:42,766<== Total: 1 (BaseJdbcLogger.java:137)
4
count()中的根据某个字段查询数据数量,若查询出来的结果中的某个字段为null,字段为null的数据不会放入map集合中。
6.4、查询一条数据为map集合
SelectMapper.java接口
/**
* 根据id查询用户信息为map集合
* @param id
* @return
*/
Map<String,Object> getUserByIdToMap(@Param("id") Integer id);
SelectMapper.xml
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id = #{id}
</select>
SelectMapperTest.java
@Test
public void testGetUserByIdToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> map = mapper.getUserByIdToMap(1);
//查询一条数据转换为map集合
//{password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin}
System.out.println(map);//map集合没有固定的键,如果查询出来的结果集里某个字段为Null,这个字段是不会放在map集合中的。
}
运行测试:
6.5、查询多条数据为map集合
若查询的数据有多条时,并且要将每条数据转换为map集合。
有两种解决方案:
1、将mapper接口方法的返回值设置为泛型是map的list集合。
List<Map<String,Object>> getAllUserToMap();
结果:
[
{password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},
{password=123456, gender=女, id=2, age=33, email=123@qq.com, username=root},
{password=123, gender=男, id=3, age=23, email=123@qq.com, username=zhangsan},
{password=123, id=4, username=lisi}
]
2、可以将每条数据转换的map集合放在一个大的map中,但是必须要通过 @MapKey 注解,将查询的某个字段的值作为map的键。
@MapKey(“id”)
Map<String,Object> getAllUserToMap();
结果:
{
1={password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},
2={password=123456, gender=女, id=2, age=33, email=123@qq.com, username=root},
3={password=123, gender=男, id=3, age=23, email=123@qq.com, username=zhangsan},
4={password=123, id=4, username=lisi}
}
方式一:使用能存储map集合的List集合,作为方法的返回值。
SelectMapper.java 接口
/**
* 查询所有的用户信息为map集合
* @return
*/
List<Map<String,Object>> getAllUserToMap();
SelectMapper.xml
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
SelectMapperTest.java
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<Map<String, Object>> list = mapper.getAllUserToMap();
System.out.println(list);
}
运行测试
DEBUG 02-25 14:34:25,385==> Preparing: select * from t_user (BaseJdbcLogger.java:137)
DEBUG 02-25 14:34:25,402==> Parameters: (BaseJdbcLogger.java:137)
DEBUG 02-25 14:34:25,421<== Total: 4 (BaseJdbcLogger.java:137)
[{password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin}, {password=123456, gender=女, id=2, age=33, email=123@qq.com, username=root}, {password=123, gender=男, id=3, age=23, email=123@qq.com, username=zhangsan}, {password=123, id=4, username=lisi}]
方式二:使用@MapKey()注解设置Map集合的键
Map集合中也可以存储多条数据,可以直接把每一条数据转换为Map集合放入List集合中,不能把每条转换为Map集合的数据放入Map集合中。map集合是键值对。查询出来的数据可以作为值。
注解:@MapKey() 把当前查询的数据所转换的Map集合,放入到一个大的Map集合中,通过@MapKey注解设置Map集合的键。
比如:@MapKey(“id”) 括号中id是以查询出来数据的字段id作为键,它的值就是每一条数据转换的Map集合。
SelectMapper.java接口
/**
* 方式二:查询所有的用户信息为map集合
* @return
*/
@MapKey("id")
Map<String,Object> getAllUserToMap2();
SelectMapper.xml
<select id="getAllUserToMap2" resultType="map">
select * from t_user
</select>
SelectMapperTest.java
@Test
public void testGetAllUserToMap2(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
/**
* {
* 1={password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},
* 2={password=123456, gender=女, id=2, age=33, email=123@qq.com, username=root},
* 3={password=123, gender=男, id=3, age=23, email=123@qq.com, username=zhangsan},
* 4={password=123, id=4, username=lisi}
* }
*/
Map<String,Object> map = mapper.getAllUserToMap2();
System.out.println(map);
}
运行测试:
DEBUG 02-25 15:33:21,599==> Preparing: select * from t_user (BaseJdbcLogger.java:137)
DEBUG 02-25 15:33:21,622==> Parameters: (BaseJdbcLogger.java:137)
DEBUG 02-25 15:33:21,638<== Total: 4 (BaseJdbcLogger.java:137)
{1={password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin}, 2={password=123456, gender=女, id=2, age=33, email=123@qq.com, username=root}, 3={password=123, gender=男, id=3, age=23, email=123@qq.com, username=zhangsan}, 4={password=123, id=4, username=lisi}}