【Mybatis】使用mybatis添加时进行条件判断

一、代码


DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo06.mapper.UserMapper">
    <insert id="addUser" >
        INSERT INTO users
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null and username !=''">
                username,
            if>
            <if test="password != null and password !=''">
                password,
            if>
        trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="username != null and username !=''">
                #{username},
            if>
            <if test="password != null and password !=''">
                #{password},
            if>
        trim>
    insert>
mapper>

二、详情

trim 标签用来截取拼接的sql前后内容,相当于java和js中的trim
prefix:在trim标签内sql语句加上前缀
suffix:在trim标签内sql语句加上后缀
suffixOverrides:指定去除多余的后缀内容
比如:suffixOverrides=”,”,去除trim标签内sql语句多余的后缀”,”
prefixOverrides:指定去除多余的前缀内容

你可能感兴趣的:(Java,笔记,java)