首页 > 其他分享 >MyBatis-各种查询功能

MyBatis-各种查询功能

时间:2022-08-27 17:50:02浏览次数:44  
标签:map 功能 SelectMapper mapper getAllUserToMap 查询 public MyBatis id

1. 获取总记录数count

SelectMapper.java

public interface SelectMapper {
    /**
     * 查询用户的总数量
     * @return
     */
    Integer getCount();
}

 SelectMapper.xml

        MyBatis中为Java中常用的类型设置了类型别名
        Integer:Integer,int
        int:_int,_integer
        Map:map
        String:string

<?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.mybatis.mapper.SelectMapper">
    <!--Integer getCount();-->
    <select id="getCount" resultType="int">
        select count(*) from t_user
    </select>
</mapper>

测试文件:

    @Test
    public void testGetCount(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Integer count = mapper.getCount();
        System.out.println(count);
    }

2. 查询一条数据为Map集合

若查询的结果没有对应的实体类,可将其转换为map

SelectMapper.java

public interface SelectMapper {
   /**
     * 根据id查询用户信息为map集合
     * @param id
     * @return
     */
    Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
}

 SelectMapper.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.mybatis.mapper.SelectMapper">
    <!--Map<String, Object> getUserByIdToMap(@Param("id") Integer id);-->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>
</mapper>

测试文件:

    @Test
    public void testGetUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> map = mapper.getUserByIdToMap(4);
        //{password=123456, gender=男, id=1, age=23, [email protected], username=admin}
        System.out.println(map);
    }

 3. 查询所有的信息为map集合

若查询的数据有多条时,并且要将每条数据转换为map集合

此时有两种解决方案:

    1、将mapper接口方法的返回值设置为泛型是map的list集合

       List<Map<String, Object>> getAllUserToMap();

      结果:{password=123456, gender=男, id=1, age=23, [email protected], username=admin},{password=123456, gender=男, id=2, age=27, [email protected], username=张三}

SelectMapper.java

public interface SelectMapper {
  List<Map<String, Object>> getAllUserToMap();
}

 SelectMapper.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.mybatis.mapper.SelectMapper">
    <!--Map<String, Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
</mapper>

测试文件:

     @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<Map<String, Object>> list = mapper.getAllUserToMap();
        System.out.println(list);
    }

       2、可以将每条数据转换的map集合放在一个大的map中,但是必须要通过@MapKey注解,将查询的某个字段的值作为大的map的键

     @MapKey("id")

     Map<String, Object> getAllUserToMap();

  结果:

   {  1={password=123456, gender=男, id=1, age=23, [email protected], username=admin},
          2={password=123, gender=男, id=2, age=23, [email protected], username=zhangsan},
          3={password=123456, gender=女, id=3, age=33, [email protected], username=root},
               4={password=123, id=4, username=lisi}
         }

SelectMapper.java

public interface SelectMapper {
    @MapKey("id")
    Map<String, Object> getAllUserToMap();
}

 SelectMapper.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.mybatis.mapper.SelectMapper">
    <!--Map<String, Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
</mapper>

测试文件:

@Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> map = mapper.getAllUserToMap();
        System.out.println(map);
    }

标签:map,功能,SelectMapper,mapper,getAllUserToMap,查询,public,MyBatis,id
From: https://www.cnblogs.com/Joyce-mi7/p/16630987.html

相关文章

  • ElasticSearch高级用法之滚动查询
    由于es的限制,普通查询最多查询10000条数据,那么需要查询数据量大的情况怎么办呢?这个时候就可以使用滚动查询。代码如下://设置查询超时时间Scrollscroll=newS......
  • 功能强大的 PDF 处理器
    PDFPlusMac是一款功能强大的PDF处理器,只需三个简单的步骤即可帮助您合并、拆分、添加水印和裁剪PDF文档。例如,如果您想为一组带有公司徽标的PDF文档添加水印,PDFPl......
  • MyBatis保姆级理解与使用,动态SQL(核心)
    1. 动态SQL(核心)1.1 简介Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的难点问题。比如:我们在多条......
  • 路由器功能----组播
    1.IGMP监听(IGMPSnooping)能够帮助交换机了解哪些主机请求接受特定多播应用的流量以及这些主机都连接在哪些交换机端口上2.多播路由协议:PIM-SM(ProtocolIndependent......
  • mysql-开启日志记录功能
    开启日志记录功能--开启功能SETGLOBALgeneral_log=ON;--保存到文件SETGLOBALlog_output='file';查看日志内容--查看日志保存位置及开启状态showvariab......
  • 服务器查询访问IP指令
    通过netstat查看当前访问服务器最多的IP,非常实用,现在分享给大家:netstat-ntu|tail-n+3|awk'{print$5}'|cut-d:-f1|sort|uniq-c|sort-n-r|head......
  • CCF 202112-2 序列查询新解(C++)
    该题关键点在于:分段计算先对f分段:for(inti=1;i<=n+1;i++)//以f(i)为区域划分计算在此区域内f的取值相同,值为:i-1。再对每个f值相同的区域按照g值进行分段:for(int......
  • mybatis面试题
    mybatis中#{}和${}的区别是什么?#{}是预编译处理,会将sql中的#{}替换为?号防止sql注入${}是字符串替换。当实体类中的属性名和表中的字段名不一样,怎么办?......
  • 10个快速入门Query函数使用的Pandas的查询示例
    转载:https://mp.weixin.qq.com/s/TJStQDtUfOOXtb__cpivDgpandas.的query函数为我们提供了一种编写查询过滤条件更简单的方法,特别是在的查询条件很多的时候,在本文中整理了1......
  • Mysql---多表查询
    《需求》  比如说:我们要显示一系列信息,但是这些信息并不是在同一个表上的,可能在多个表上这个时候就要展示多张表的内容如:    如果我直接这样会显示什么呢......