insert 动态添加

<insert id="addBook" useGeneratedKeys="true" keyProperty="id"  
        parameterType="Book">  
        insert into  
        book(name,author,publish,publishdate
        <include refid="getTestTbField" />  
        )  
        values(#{name},#{author},#{publish},#{publishdate}  
        <include refid="getTestTbValue" />  
        )  
    </insert>  
  
    <sql id="getTestTbField">  
        <trim prefix="," suffixOverrides=",">  
            <if  
                test="page!= 0">  
                page,  
            </if>  
            <if test="price!= 0">  
                price,  
            </if>  
            <if test="content!=null and content!='null' and content!=''">  
                content,  
            </if>  
        </trim>  
    </sql>  
  
    <sql id="getTestTbValue">  
        <trim prefix="," suffixOverrides=",">  
            <if  
                test="page!= 0">  
                #{page},  
            </if>  
            <if test="price!= 0">  
                #{price},  
            </if>  
            <if test="content!=null and content!='null' and content!=''">  
                #{content},  
            </if>  
        </trim>  
    </sql> 

根据传入的值动态添加

猜你喜欢

转载自blog.csdn.net/a116385895/article/details/88640171