[简单]mybatis insert语句添加动态字段

      mysql数据库,id自增,配置如下:

     

<insert id="saveTestTb" useGeneratedKeys="true" keyProperty="id"
		parameterType="TestTb">
		insert into
		db_ap_fgm_TestTb(id,traffic_type
		<include refid="getTestTbField" />
		)
		values(#{id},#{trafficType}
		<include refid="getTestTbValue" />
		)
	</insert>

	<sql id="getTestTbField">
		<trim prefix="," suffixOverrides=",">
			<if
				test="testA!=null and testA!='null' and testA!=''">
				TESTA,
			</if>
			<if test="level!=null and level!='null' and level!=''">
				LEVEL,
			</if>
			<if test="state!=null and state!='null' and state!=''">
				STATE,
			</if>
			<if test="description!=null and description!='null' and description!=''">
				DESCRIPTION
			</if>
		</trim>
	</sql>

	<sql id="getTestTbValue">
		<trim prefix="," suffixOverrides=",">
			<if
				test="testA!=null and testA!='null' and testA!=''">
				#{testA},
			</if>
			<if test="level!=null and level!='null' and level!=''">
				#{level},
			</if>
			<if test="state!=null and state!='null' and state!=''">
				#{state},
			</if>
			<if test="description!=null and description!='null' and description!=''">
				#{description}
			</if>
		</trim>
	</sql>

猜你喜欢

转载自53873039oycg.iteye.com/blog/2215984