MyBatis动态插入的实现


mybatis通过定义前缀后缀和分割字符来拼接sql语句,实现动态插入的功能

<insert id="addNewsTypeByNewsId">
        insert into news_newstype

        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="newsId != null">
                newsid,
            </if>
            <if test="newsTypeId != null and newsTypeId !=0">
                newstypeid,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="newsId != null">
                #{newsId},
            </if>
            <if test="newsTypeId != null and newsTypeId !=0">
                #{newsTypeId},
            </if>
        </trim>
    </insert>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM