Mybatis框架Dao代理-02

3.3 封装 MyBatis 输出结果

    Mybatis自定义别名:

      Mybatis提供的对java类型的简定义简短,好几的名称

        定义别名步骤:

          1.在mybatiszhu配置文件中,使用typeAliases标签声明别名

          2.在mapper文件中,resultType=“别名”

                          示例:

          声明别名(在mybatis主配置文件中)

<typeAliases>
        <!--第一种方式:使用<typeAliases>标签
                type属性:java类型的全限定名称(自定义类型)
                alias属性:自定义的别名
            优点:别名可以自定义
            缺点:每个类型必须单独定义
                -->
        <typeAlias type="com.hrf.domain.Student" alias="stu"/>
        <typeAlias type="com.hrf.domain.QueryParam" alias="db"/>


        <!--第二种方式:使用<package>标签
                 name属性:包名,  Mybatis会把这个包中的所有类名作为别名使用(不区分大小写)
                    -->
        <package name="com.hrf.domain"/>
    </typeAliases>

    使用方式:

<!--方式一使用-->
<
select id="SelectStudentById" resultType="stu"> select id,name,email,age from student where id=#{StudenId} </select>
<!--方式二使用-->
<select id="SelectStudentById" resultType="Student">
  select * from student where id = #{StudentId}
</select>

 

  

  3.3.1 resultType

  resultType:执行sql得到ResultSet转换的类型,使用类型的完全限定名或别名。注意如果返回的是集合,拿应该设置为集合包含的类型,而不是集合本身。resultType和resultMap,不能同时使用

  

 

 

   

 

 

 

    resultType属性:在执行select时使用,作为<select>标签属性出现的

    resultType:表示结果类型,mysql执行sql语句。得到java对象的类型,它的值有两种

                1)java类型的全限定名称  2)使用别名

      resultType:使用java类型的全限定名称,表示的意思是Mybatis执行sql语句,把ResultSrt中的数据转为Student类型的对象。

            Mybatis会做以下操作:

              1.调用com.bjpowernode.domain.Student的无参构造方法,创建对象。

                Student student = new Student();    //使用的是反射机制创建对象

              2.同名的列赋值给同面馆的属性。

                 student.setId(rs.getInt("id"));

                 student.setName(re.getString("name"));

              3.得到java对象,如果dao接口返回值是List集合,Mybatis会把student对象放入List集合中。

    A.     简单类型

    接口创建方法:

    

 

 

     mapper文件写sql语句

    

 

 

     编写测试类方法:

    

 

 

     测试结果:   

 

 

 

 

  B.    对象类型

    接口方法:

    

 

 

    mapper文件:

    

 

 

     ..................等

    

 

 

   

 

 

     C.      Map

  sql的查询结果作为Map的Kay和Value。推荐使用

    Map<Object,Object>

    注意:Map作为接口返回值,sql语句的查询结果最多只能有一条记录。大于一条时是错误的。

    接口方法:

    

 

 

     Mapper文件:

    

 

 

     测试类方法:

    

 

 

     测试结果:

 

 

 ----------------------------------------------------------------------------------------------------------------

 

  3.3.2    resultMap

      resultMap可以自定义sql的结果和java对象属性的映射关系。更灵活的把列值赋值给指定的java对象属性

    (常用字列名和java对象属性名不一样的情况下)

    使用方式:

        1.在mapper文件中先定义resultMap,指定列名和属性的对应关系。

        2.在<select>中把resultType替换成为resultMap。

                              案例

            1.接口方法:

            

 

 

             实体类与数据库表单列表名不一致

            

 

 

             在mapper文件中使用resultMap标签定义:

<resultMap id="studentMap" type="com.hrf.domain.Student">
<!--     id属性是只能用来标记主键字段的   -->
        <id column="id" property="Id"/>
<!--      非主键字段使用result属性   要与返回值的类的属性进行对应-->
        <result column="name" property="cname"/>
        <result column="email" property="email"/>
        <result column="age" property="age"/>

    </resultMap>
    <select id="selectUseResultMap" resultMap="studentMap">
        select  * from student where id = #{stuId}
    </select>

        编写测试类方法:

          

 

 

         测试结果:      

 

 

 

 

        实体类属性名和列名不同的处理方式

          1)使用resultType

          只需要在sql语句中取别名即可             

select id as stuId, name as stuName,age as stuAge from student where name=#{queryName} or age=#{queryAge}

          2)使用resultMap

          使用resultMap标签与java对象类创建映射关系即可

   

    

 

posted @ 2022-02-26 08:45  Soleili  阅读(29)  评论(0编辑  收藏  举报