MyBatis中常见标签的使用

1. <collection>标签

        例如有两张表:user表

role表

 

那么我们再写实体类User 、Role 的对应关系是,一个用户有多个角色,因此,在 User 的实体中加入一个 Role 的属性。

private List<Role> roles //1对多关系

然而在Mybatis的XML文件中我们要使用 <collection>标签来映射这一关系

<select id="queryUserRoleList" resultMap="queryForListMap">  
        SELECT  
          u.user_id,  
          u.user_name,  
          u.password,  
          r.role_id
          r.role_name,  
        FROM  
          user u  
        LEFT JOIN  
          role r  
        ON  
          u.user_id = r.user_id
    </select>  
<resultMap id="queryUserRoleList" type="com.rh.domain.User">  
        <id column="user_id" property="userId" jdbcType="VARCHAR"/>  
        <result column="user_name" property="userName" jdbcType="VARCHAR"/>  
        <result column="password" property="password" jdbcType="VARCHAR"/>  
        <collection property="roles" javaType="java.util.List" ofType="com.rh.domain.Role">  
            <id column="role_id" property="roleId" jdbcType="VARCHAR" />  
            <result column="role_name" property="roleName" jdbcType="VARCHAR" />  
        </collection>  
    </resultMap>  

2. <association>标签

        既然有1对多标签就有1对1标签,<association>标签就是1对1标签, 嵌套查询是先查一个表,根据这个表里面的结果的外键id,去再另外一个表里面查询数据,也是通过association配置,但另外一个表的查询通过select属性配置,如下:

<resultMap id=”queryList” type=”Blog”>  
<association property="author" column="blog_author_id" javaType="Author"  
select=”selectAuthor”/>  
</resultMap>  
   
<select id=”selectBlog” parameterType=”int” resultMap=”queryList”>  
SELECT * FROM BLOG WHERE ID = #{id}  
</select>  
   
<select id=”selectAuthor” parameterType=”int” resultType="Author">  
SELECT * FROM AUTHOR WHERE ID = #{id}  
</select> 

3. 使用resultType、ResultMap处理返回结果

resultType: 指定返回值结果的完全限定名,处理多表查询的结果。多表查询需要定义vo封装查询的结果。只能用在单表查询或者定义vo的情况,如果是定义vo不能将关联的数据封装为需要获得某个类的对象

resultMap:不需要定义vo类,将关联数据对应的类,作为另外一个类的属性。

      第一步:定义resultMap

      第二步:引用resultMap

      使用resultMap:用来多表关联的复杂查询中,通过需要将关联查询的数据封装为某个实体类对象,如果有特殊业务需要或者说明需要将管理数据封装为实体类的对象,使用resultMap

4.<if>、<choose>、<when>、<otherwise>

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

        例子:

<select id="queryList" parameterType="Blog" resultType="Blog">
        select * from t_blog where 1 = 1 
        <choose>
            <when test="name!= null">
                and name= #{name}
            </when>
            <when test="password!= null">
                and password= #{password}
            </when>
            <otherwise>
                and sign= "sign"
            </otherwise>
        </choose>
    </select>

5.<sql>和<include>

        <sql>标签相当于一段SQL语句片段,引用它的时候使用<include>标签来引用

<sql id="testSql>
    <if test="sex!=null">
        and sex=#{sex}
    </if>
    <if test="email!=null>
        and email=#{email}
    </if>
</sql>

<select id="findUser" parameeterType="User" resultType="User">
    select * from user
    <where>
    <include refid="testSql"></include>
    </where>
</select>

6. <foreach>

        一般使用它来做语句的拼接,在向sql传递 集合或者数组的时候 mybatis使用<foreach>来解析

例如:SQL语句SELECT * FROM user WHERE id=1 OR id=10 OR id=16 

mybatis:

<if test="ids != null">     
        <foreach collection="ids"item="user_id"open="AND ("close=")" separator="or" >
    每次遍历需要拼接的串
                id= #{user_id}
        </foreach>
 </if>

7. <set>、<if>

         这两个标签一般用于更新语句中,表限定条件

<update id="updateStudent" parameterType="StudentEntity">     
    UPDATE student      
    <set>    
        <if test="studentName!=null and studentName!='' ">     
            student_name = #{studentName},      
        </if>     
        <if test="studentSex!=null and studentSex!='' ">     
            student_sex = #{studentSex},      
        </if>     
        <if test="studentBirthday!=null ">     
            student_birthday = #{studentBirthday},      
        </if>       
    </set>     
    WHERE student_id = #{studentID};      
</update>  

8. <trim>

        <trim>标签作用一般是用于去除多余关键字

<!-- 查询学生list,like姓名,=性别 -->     
<select id="getStudentList" parameterType="StudentEntity" resultMap="studentResultMap">     
    SELECT * from student ST      
    <trim prefix="WHERE" prefixOverrides="AND|OR">     
        <if test="studentName!=null and studentName!='' ">     
            ST.student_name LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
        </if>     
        <if test="studentSex!= null and studentSex!= '' ">     
            AND ST.student_sex = #{studentSex}      
        </if>     
    </trim>     
</select>  

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值