常见的数据库函数,关键字整理

ParameterType与resultType与resultMap的区别

ParameterType(是对传入参数的设置)

传入的参数设置,单个参数直接指明类型即可

多个参数mapper文件用@Param(“name”)指明 parameterType属性可以省略

 

resultType和resultMap(对结果集的映射)

resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用(可以自己定义返回类型的字段数量)

  1. 提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当提供的返回类型属性是resultType的时候,MyBatis对自动的给把对应的值赋给resultType所指定对象的属性。
  2. 当提供的返回类型是resultMap时,因为Map不能很好表示领域模型,就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。

 

当ResultMap中需要引入对象,或要进行子查询时

<resultMap type="Comment" id="CommentResult">
<association property="blog" select="selectBlog" column="blog" javaType="Blog"/>
</resultMap>

<select id="selectComment" parameterType="int" resultMap="CommentResult">
select * from t_Comment where id = #{id}
</select>

<select id="selectBlog" parameterType="int" resultType="Blog">
select * from t_Blog where id = #{id}
</select>

 

collection和association的使用区别

association是用于一对一和多对一,而collection是用于一对多的关系

1. 关联-association
2. 集合-collection

一对一代码如下:

<?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.glj.mapper.PersonMapper">
    <resultMap type="com.glj.poji.Person" id="personMapper">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <association property="card" column="card_id" 
            select="com.glj.mapper.CardMapper.selectCardById"
            javaType="com.glj.poji.Card">
        </association>
    </resultMap>
    <select id="selectPersonById" parameterType="int" resultMap="personMapper">
        select * from tb_person where id = #{id}
    </select>
</mapper>

一对多用collection多对一关系,所以使用了关联-association

<?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.glj.mapper.StudentMapper">
    <select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
        select * from tb_clazz c,tb_student s where c.id = s.id and s.id = #{id}
    </select>
    <select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">
        select * from tb_student where clazz_id = #{id}
    </select>
    <resultMap type="com.glj.pojo.Student" id="studentResultMap">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <association property="clazz" javaType="com.glj.pojo.Clazz">
            <id property="id" column="id"/>
            <result property="code" column="code"/>
            <result property="name" column="name"/>
        </association>
    </resultMap>
</mapper>

 

javaType和ofType的区别

JavaTypeofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型

 

<resultMap type="User" id="resultUserMap">
         <result property="id" javaType="int" column="user_id" />
         <result property="username" javaType="string" column="username" />
         <result property="mobile"  column="mobile" />
                        <!--javatype指定的是user对象的属性的类型(例如id,posts),而oftype指定的是映射到list集合属性中pojo的类型(本例指的是post类型)-->
         <collection property="posts"  ofType="com.spenglu.Post"  javaType="java.util.ArrayList" column="userid">
             <id property="id" column="post_id" javaType="int" jdbcType="INTEGER"/>   
            <result property="title" column="title" javaType="string" jdbcType="VARCHAR"/>
            <result property="content" column="content" javaType="string" jdbcType="VARCHAR"/>
         </collection>
    </resultMap>

 

 

choose (when, otherwise)标签(相当于switch的用法)

有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为 true,就会执行 if 标签中的条件。MyBatis 提供了 choose 元素。if标签是与(and)的关系,而 choose 是或(or)的关系。

choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

when元素表示当 when 中的条件满足的时候就输出其中的内容,跟 JAVA 中的 switch 效果差不多的是按照条件的顺序,当 when 中有条件满足的时候,就会跳出 choose,即所有的 when 和 otherwise 条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出 otherwise 中的内容。

 

posted @ 2019-04-22 15:53  彳亍风  阅读(485)  评论(0编辑  收藏  举报