ssm框架实现添加时动态sql时点击添加没反应,把sql语句写死了能正常添加
xml:sql:
insert into t_base_order ( user_id,account,address_id,create_time ) VALUES (#{baseOrder.userId},#{baseOrder.account},#{baseOrder.userAddress.addressId},#{baseOrder.createTime})
insert into t_base_order
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="baseOrder.userId!=null and baseOrder.userId!=''">
user_id,
</if>
<if test="baseOrder.account!=null and baseOrder.account!=''">
account,
</if>
<if test="baseOrder.userAddress!=null and baseOrder.userAddress.addressId!=null and baseOrder.userAddress.addressId!=-1 ">
address_id,
</if>
<if test="baseOrder.createTime!=null and baseOrder.createTime!=''">
create_time,
</if>
</trim>
<trim prefix=" values (" suffix=")" suffixOverrides=",">
<if test="baseOrder.userId!=null and baseOrder.userId!=''">
#{baseOrder.userId},
</if>
<if test="baseOrder.account!=null and baseOrder.account!=''">
#{baseOrder.account},
</if>
<if test="baseOrder.userAddress!=null and baseOrder.userAddress.addressId!=null and baseOrder.userAddress.addressId!=-1">
#{baseOrder.userAddress},
</if>
<if test="baseOrder.createTime!=null and baseOrder.createTime!=''">
#{baseOrder.createTime},
</if>
</trim>
控制台:
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1acff2b7]
JDBC Connection [jdbc:mysql://localhost:3306/student_ssm_db?useUnicode=true&characterEncoding=UTF-8, UserName=root@, MySQL-AB JDBC Driver] will be managed by Spring
==> Preparing: insert into t_base_order( user_id, account, address_id, create_time ) values ( ?, ?, ?, ? )
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1acff2b7]
Transaction synchronization rolling back SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1acff2b7]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1acff2b7]
insert 动态sql,参考如下试试:
<!-- 对应的插入字段的名字 -->
<sql id="key">
<trim suffixOverrides=",">
<if test="userCode!=null and userCode!=''">
userCode,
</if>
<if test="userName!=null and userName!=''">
userName,
</if>
<if test="userPassword!=null and userPassword!=''">
userPassword,
</if>
<if test="gender!=null and gender!=''">
gender,
</if>
<if test="address!=null and address!=''">
address,
</if>
<if test="phone!=null and phone!=''">
phone,
</if>
</trim>
</sql>
<!-- 对应的插入字段的值 -->
<sql id="values">
<trim suffixOverrides=",">
<if test="userCode!=null and userCode!=''">
#{userCode},
</if>
<if test="userName!=null and userName!=''">
#{userName},
</if>
<if test="userPassword!=null and userPassword!=''">
#{userPassword},
</if>
<if test="gender!=null and gender!=''">
#{gender},
</if>
<if test="address!=null and address!=''">
#{address},
</if>
<if test="phone!=null and phone!=''">
#{phone},
</if>
</trim>
</sql>
<insert id="addUser2" parameterType="user">
insert into smbms_user(<include refid="key"/>)
values(<include refid="values"/>)
</insert>
最后一个If test里面即create_time不要加逗号
不知道你这个问题是否已经解决, 如果还没有解决的话: