zoukankan      html  css  js  c++  java
  • 课时6 输出参数为简单类型、对象类型、HashMap及resultMap使用

    .1)resultType

      (1)简单类型(8个基本类型加String)

      (2)输出类型为实体对象类型

      (3) 输出参数为实体对象类型的集合 :虽然输出类型为集合,但是resultType依然写 集合的元素类型  (resultType="student")

      (4)输出参数为HashMap

    <!--    别名作为Map的key-->
        <select id="queryStudentOutHashMap" resultType="HashMap" parameterType="int">
            select stuName "name",stuAge "age" from student where stuno=#{xx}
        </select>

        --HashMap本身就是一个集合,可以存放多个元素,

        但是根据提示发现 返回值为HashMap的时候,查询的结果只能是一个学生(no,name);

        结论---》:一个HashMap对应一个学生的多个元素(多个属性) (一个map一个学生)

        如果非要改成输出为多个学生 改进如下:

    <!--    查询返回值为集合map-->
        <select id="queryAllStudentOutHashMap" resultType="HashMap">
            select stuName "name",stuAge "age" from student
        </select>
     /**
         * 查询所有学生并且返回值为Map集合
         * @return
         */
        List<Map<String ,Object>> queryAllStudentOutHashMap();
      public static void queryAllStudentOutHashMap() throws IOException {
            Reader reader = Resources.getResourceAsReader("config.xml");
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
            SqlSession session=sessionFactory.openSession();
            IStudentDao iStudentDao=session.getMapper(IStudentDao.class);
            List<Map<String, Object>> map = iStudentDao.queryAllStudentOutHashMap();
            System.out.println(map);
            session.close();
            reader.close();
        }

    .2)resultMap

      (1)实体类的属性表中的字段:类型,名字不同时(stuno,id)  

       注意:当属姓名和字段名不一致时,除了resultMap以外,还可以使用resultType+HashMap

         1.resultMap处理属性和字段不一致

    <!--    使用resultMap返回-->
        <select id="queryAllStudentByidMap" resultMap="queryAllStudentByidMaps" parameterType="int">
            select id,name from student where id=#{xx}
        </select>
    
        <resultMap id="queryAllStudentByidMaps" type="student">
            <id property="stuNo" column="id"></id>
            <result property="stuName" column="name"></result>
        </resultMap>

         2.resultType处理属性和字段名不一致

    <!--使用resultType返回-->
        <select id="queryAllStudentByidWithType" resultType="student" parameterType="int">
            select id "stuNo",name "stuName" from student where id=#{xx}
        </select>

           select 表中的字段名 “类中的属性名” from ...来指定属性名和字段名的关系

           注意:如果10个字段 ,但发现 某一个属性结果始终为默认值(0,0.0,null) ,则可能是表的字段和类的属性 名字写错了

  • 相关阅读:
    1347: Last Digit (周期函数)
    1363: Count 101 (经典数位dp)
    1360: Good Serial Inc.(不知道是什么类型的题)
    C#winForm调用WebService的远程接口
    Web Service 的创建简单编码、发布和部署
    极致精简的webservice集成例子
    SVN使用教程总结
    C# int.Parse()与int.TryParse()
    C# 函数1 (函数的定义)
    C#中的委托和事件
  • 原文地址:https://www.cnblogs.com/thisHBZ/p/12455241.html
Copyright © 2011-2022 走看看