mybatis的各种查询情况3 |
您所在的位置:网站首页 › 怎么看qq记录条数 › mybatis的各种查询情况3 |
前言 上一章我们介绍了mybatis的各种查询情况2 - 查询所有信息 目录 1.接口部分 2.mapper映射文件部分 2.1大纲碎碎念 2.2 resultType返回类型 3.测试类 4.结果 4.1测试类结果 4.2数据库验证 5.结论 1.接口部分接口部分用Integer或者int类型都是可以的,返回一个整数。 /** * 查询用户信息总记录数 */ Integer getCount(); 2.mapper映射文件部分 2.1大纲碎碎念mapper映射文件部分可以用两种语句,在这里用count(*)和 count(1)是等价的。但注意不要使用count(字段),count(字段)时候如果某个字段是null值(没设置值)则不会被统计进去结果,那会影响总量。 2.2 resultType返回类型这里返回类型的话不再是之前用过的user对象,这里是返回一个int/Integer类型,那我们则需要设置resultType = "java.lang.Integer" resultType这里,设置Integer, Int, integer, int都是可以的,此处是mybatis的类型别名,不区分大小写。但是严谨起见,推荐Integer和 int 常见类型。 注意哦,这里是新知识点。 select count(*) from t_user 3.测试类 @Test public void testGetCount() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); Integer count = mapper.getCount(); System.out.println("数据库总量是: "+count); } 4.结果 4.1测试类结果测试类结果和数据库查询结果是一致的,测试类正确。 返回count(*)要记得返回resultType是Integer 下一章我们将介绍 mybatis各种查询功能4 - 查询以及返回单个map集合 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |