Mybatis动态标签

2021-04-13  本文已影响0人  felixfeijs

Mybatis动态标签

汇总

标签名称 示意
select 定义查询语句
insert 定义增加语句
update 定义修改语句
delete 定义删除语句
resultMap 绑定结果集和对象属性
foreach 用于遍历
if 用于判断属性
choose 用于多条件判断属性
where 进行语句where拼接
set 格式化update语句
trim 去除属性的空格
collection 多对多关联关系
association 一对一关联关系
sql 定义sql常量片段

SQL定义标签

select
    <select id="listMap" resultType="java.util.Map">
        select * from bss_user
    </select>
insert
    <insert id="insert" parameterType="com.example.demo.model.User" keyProperty="userId" keyColumn="user_id">
        INSERT INTO
        `bss_user`
        (`nick_name`, `real_name`, `avatar`, `sex`, `phone`, `password`, `create_time`, `update_time`)
        VALUES (#{nickName}, #{realName}, #{avatar}, #{sex}, #{phone}, #{password}, now(), NULL);
    </insert>
update
    <update id="update" parameterType="com.example.demo.model.User">
        update `bss_user` 
        set `nick_name` = #{nickName} 
        where `user_id` = #{userId}
    </update>
delete
    <delete id="deleteById" parameterType="java.lang.String">
        delete from `bss_user` where `user_id` = #{userId}
    </delete>
sql标签
    <sql id="Base_Column" lang="" databaseId="">
       `user_id`,`nick_name`, `real_name`, `avatar`, `sex`, `phone`, `password`, `create_time`, `update_time`
    </sql>
include标签
    <select id="listMap" resultType="java.util.Map">
        select
        <include refid="Base_Column"/>
        from bss_user
    </select>

配置对象属性与查询结果集标签

resultMap
<resultMap id="userMap" type="com.example.demo.model.User" extends="com.example.demo.mapper.WorkMapper.workMap">
        <id column="user_id" property="userId"/>
        <result column="nick_name" property="nickName"/>
        <result column="real_name" property="realName"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="phone" property="phone"/>
        <result column="password" property="password"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>
    <select id="listUser" resultMap="userMap">
        select
        `user_id`,`nick_name`, `real_name`, `avatar`, `sex`, `phone`, `password`, `create_time`, `update_time`
        from bss_user
    </select>

动态拼接SQL

if标签
    <update id="update" parameterType="com.example.demo.model.User">
        update `bss_user`
        set `nick_name` = #{nickName}
        <if test="real_name!=null and real_name!=''">`real_name` = #{real_name}</if>
        where `user_id` = #{userId}
    </update>
foreach标签
    <select id="listUser" resultMap="userMap">
        select
        `user_id`,`nick_name`, `real_name`, `avatar`, `sex`, `phone`, `password`, `create_time`, `update_time`
        from bss_user where `user_id` in
        <foreach collection="list" index="i" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
choose标签
    <select id="listUser" resultMap="userMap">
        select
        `user_id`,`nick_name`, `real_name`,`type`,`avatar`, `sex`, `phone`, `password`, `create_time`, `update_time`
        from bss_user where `user_id` = #{userId}
        <choose>
            <when test="type == 1">
                and `sex` = 1
            </when>
            <when test="type == 2">
                and `sex` = 2
            </when>
            <otherwise>
                
            </otherwise>
        </choose>
    </select>

格式化输出

where标签
     <select id="listUser" resultMap="userMap">
        select
        `user_id`,`nick_name`, `real_name`,`type`,`avatar`, `sex`, `phone`, `password`, `create_time`, `update_time`
        from bss_user
        where
        <if test=" userId != null">
            `user_id` = #{userId}
        </if>
        <if test=" phone != null and phone !=''">
            and `phone` = #{phone}
        </if>
    </select>
    <select id="listUser" resultMap="userMap">
        select
        `user_id`,`nick_name`, `real_name`,`type`,`avatar`, `sex`, `phone`, `password`, `create_time`, `update_time`
        from bss_user
        <where>
            <if test=" userId != null">
                `user_id` = #{userId}
            </if>
            <if test=" phone != null and phone !=''">
                and `phone` = #{phone}
            </if>
        </where>
    </select>
set标签
        <update id="update" parameterType="com.example.demo.model.User">
        update `bss_user`
        <set>
            <if test="nickName !=null and nickName !=''">`nick_name` = #{nickName},</if>
            <if test="realName !=null and realName !=''">`real_name` = #{realName},</if>
            <if test="avatar !=null and avatar !=''">`avatar` = #{avatar},</if>
            `update_time` = now()
        </set>
        where `user_id` = #{userId}
    </update>
trim标签
    <select id="getUser" resultMap="userMap">
        select
        `user_id`,`nick_name`, `real_name`,`type`,`avatar`, `sex`, `phone`, `password`, `create_time`, `update_time`
        from bss_user
        <trim prefix="where" prefixOverrides="AND|OR">
            <if test="phone!=null and phone!=''">`phone` = #{phone}</if>
            <if test="password!=null and password!=''">and `password` = #{password}</if>
        </trim>
    </select>

关联关系配置

collection
  1. 定义父类接受vo
package com.example.demo.vo;

import com.example.demo.model.Article;
import lombok.Data;

import java.util.List;

@Data
public class UserArticleVO {

    private String userId;
    private String nickName;
    private String realName;
    private String avatar;
    private Integer sex;
    private Integer type;
    private String phone;
    private List<Article> articleList;
}
  1. 定义子类
package com.example.demo.model;

import lombok.Data;

@Data
public class Article {

    private String articleId;
    private String userId;
    private String title;
    private String content;
    private String createTime;
    private String updateTime;
}
  1. 定义父类sql片段
    <sql id="Base_Column">
       bu.`user_id`,bu.`nick_name`, bu.`real_name`, bu.`avatar`, bu.`sex`, bu.`phone`, bu.`password`, bu.`create_time`, bu.`update_time`
    </sql>
  1. 定义子类sql片段
    <sql id="Base_Column">
       ba.`article_id`, ba.`user_id`, ba.`title`, ba.`content`, ba.`create_time`, ba.`update_time`
    </sql>
collection(方式一)
  1. 定义主表查询语句和resultMap
    <resultMap id="userArticleMap" type="com.example.demo.vo.UserArticleVO">
        <id column="user_id" property="userId"/>
        <result column="nick_name" property="nickName"/>
        <result column="real_name" property="realName"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="phone" property="phone"/>
        <result column="type" property="type"/>
        <collection property="articleList" column="{user_id=user_id}" select="listArticleByUser"/>
    </resultMap>

    <sql id="Base_Column">
       ba.`article_id`, ba.`user_id`, ba.`title`, ba.`content`, ba.`create_time`, ba.`update_time`
    </sql>

    <select id="listUserArticle" resultMap="userArticleMap">
        select
        <include refid="com.example.demo.mapper.DemoMapper.Base_Column"/>
        from bss_user bu
    </select>
  1. 定义子表的查询语句
    <select id="listArticleByUser" resultType="com.example.demo.model.Article">
        select
        <include refid="Base_Column"/>
        from bss_article ba
        where ba.user_id = #{userId}
    </select>
collection(方式二)
  1. 定义查询语句和resultMap

    <resultMap id="userArticleMap" type="com.example.demo.vo.UserArticleVO">
        <id column="user_id" property="userId"/>
        <result column="nick_name" property="nickName"/>
        <result column="real_name" property="realName"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="phone" property="phone"/>
        <collection property="articleList" ofType="com.example.demo.model.Article">
            <id column="article_id" property="articleId"/>
            <result column="title" property="title"/>
            <result column="content" property="content"/>
            <result column="create_time" property="createTime"/>
        </collection>
    </resultMap>
    
    <select id="listUserArticle" resultMap="userArticleMap">
        select
        bu.`user_id`,bu.`nick_name`, bu.`real_name`, bu.`avatar`, bu.`sex`, bu.`phone`,
        ba.`article_id`,ba.`title`, ba.`content`, ba.`create_time`
        from bss_user bu left join bss_article ba
        on bu.user_id = ba.user_id
    </select>
association
  1. 定义UserVo
package com.example.demo.vo;

import com.example.demo.model.Account;
import lombok.Data;

import java.util.Date;

@Data
public class UserVo {

    private String userId;
    private String nickName;
    private String realName;
    private String avatar;
    private Integer sex;
    private Integer type;
    private String phone;
    private String createTime;
    private Account account;

}
  1. 定义Account
package com.example.demo.model;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.math.BigDecimal;

@Data
public class Account {

    private String accountId;
    private String userId;
    private BigDecimal availableAmount = new BigDecimal(0.00);
    private BigDecimal frozenAmount = new BigDecimal(0.00);
    private int points;
    private String createTime;
    private String updateTime;
}

association(方式一)
  1. 定义主表查询语句和resultMap

    <resultMap id="userMap" type="com.example.demo.vo.UserVo">
        <id column="user_id" property="userId"/>
        <result column="nick_name" property="nickName"/>
        <result column="real_name" property="realName"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="phone" property="phone"/>
        <result column="type" property="type"/>
        <result column="create_time" property="createTime"/>
        <association property="account" select="com.example.demo.mapper.AccountMapper.getAccountByUserId" column="user_id"/>
    </resultMap>

    <sql id="Base_Column">
       bu.`user_id`,bu.`nick_name`, bu.`real_name`, bu.`avatar`, bu.`sex`, bu.`phone`, bu.`password`, bu.`create_time`, bu.`update_time`
    </sql>

    <select id="getUserAccountByUserId" resultMap="userMap">
        select
        <include refid="Base_Column"/>
        from bss_user bu
        where bu.`user_id` = #{userId}
    </select>
  1. 定义子表查询语句
    <sql id="Base_Column">
       bua.`account_id`,bua.`user_id`,bua.`available_amount`,bua.`frozen_amount`,bua.`points`,bua.`create_time`,bua.`update_time`
    </sql>

    <select id="getAccountByUserId" resultType="com.example.demo.model.Account">
        select
        <include refid="Base_Column"/>
        from bss_account bua
        where bua.`user_id` = #{userId}
    </select>
association(方式二)
  1. 定义查询语句和resultMap
    <resultMap id="userMap" type="com.example.demo.vo.UserVo">
        <id column="user_id" property="userId"/>
        <result column="nick_name" property="nickName"/>
        <result column="real_name" property="realName"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="phone" property="phone"/>
        <result column="type" property="type"/>
        <result column="create_time" property="createTime"/>
        <association property="account" javaType="com.example.demo.model.Account">
            <id column="account_id" property="accountId"/>
            <result column="user_id" property="userId"/>
            <result column="available_amount" property="availableAmount"/>
            <result column="frozen_amount" property="frozenAmount"/>
            <result column="points" property="points"/>
            <result column="create_time" property="createTime"/>
            <result column="update_time" property="updateTime"/>
        </association>
    </resultMap>

    <select id="getUserAccountByUserId" resultMap="userMap">
        select
       bu.`user_id`,bu.`nick_name`, bu.`real_name`, bu.`avatar`, bu.`sex`, bu.`phone`,
       bua.`account_id`,bua.`available_amount`,bua.`frozen_amount`,bua.`points`,bua.`create_time`,bua.`update_time`
        from bss_user bu left join bss_account bua on bu.user_id = bua.user_id
        where bu.`user_id` = #{userId}
    </select>
上一篇下一篇

猜你喜欢

热点阅读