mysql中sql语句的冷门应用

 

1:提取公共的sql语句:

2:动态添加----sql语句:

代码:

  <insert id="test1" parameterType="com.floor.shop.model.Product">
        INSERT INTO product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productName!=null and productName!=''">
                product_name,
            </if>
            <if test="stockNum!=null and stockNum!=''">
                stock_num,
            </if>
            <if test="salePrice!=null and salePrice!=''">
                sale_price,
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="productName!=null and productName!=''">
                #{productName},
            </if>
            <if test="stockNum!=null and stockNum!=''">
                #{stockNum},
            </if>
            <if test="salePrice!=null and salePrice!=''">
                #{salePrice},
            </if>

        </trim>
    </insert>
View Code

3:动态修改----sql语句:

4:增加前避免重复:

(当userName在数据库中不存在的情况下,增加到数据库)

5:修改或者新增获取受影响的行数:

6:新增自动获取主键(新增id):

7:dao多参数处理,不封装的情况下传递参数:

mapper中的sql语句不用添加参数类型(paramater=" ")

8:传入多个值的数组处理(foreach):

 接口:

映射文件:

测试:

猜你喜欢

转载自www.cnblogs.com/dw3306/p/9289891.html