mybatis动态sql-新增与更新

记录一个简单的mybatis动态sql例子

新增



<mapper namespace="org.ruiskey.mapper.UserMapper">
    <insert id="save">
        insert into user
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="id != null and id != ''">
                    id,
                if>
                <if test="username != null and username != ''">
                    username,
                if>
                <if test="password != null and password != ''">
                    password,
                if>
                <if test="nickname != null and nickname != ''">
                    nickname,
                if>
                <if test="email != null and email != ''">
                    email,
                if>
                <if test="phone != null and phone != ''">
                    phone,
                if>
                <if test="address != null and address != ''">
                    address
                if>
            trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="id != null and id != ''">
                    #{id},
                if>
                <if test="username != null and username != ''">
                    #{username},
                if>
                <if test="password != null and password != ''">
                    #{password},
                if>
                <if test="nickname != null and nickname != ''">
                    #{nickname},
                if>
                <if test="email != null and email != ''">
                    #{email},
                if>
                <if test="phone != null and phone != ''">
                    #{phone},
                if>
                <if test="address != null and address != ''">
                    #{address}
                if>
            trim>
    insert>
mapper>

更新

<update id="update">
    update user
    <set>
        <if test="username != null and username != ''">
            username = #{username}
        if>
        <if test="password != null and password != ''">
            password = #{password}
        if>
        <if test="nickname != null and nickname != ''">
            nickname = #{nickname}
        if>
        <if test="email != null and email != ''">
            email = #{email}
        if>
        <if test="phone != null and phone != ''">
            phone = #{phone}
        if>
        <if test="address != null and address != ''">
            address = #{address}
        if>
    set>
    <where>
        id = #{id}
    where>
update>

你可能感兴趣的:(#,idea,web,java,intellij-idea,spring)