1.mysql 更新操作误用and
出处:https://www.jianshu.com/p/b455962b68a1
今天一个开发反馈update某行但不生效,场景如下:
mysql> select * from test;
+------+------+
| c1 | c2 |
+------+------+
| 0 | a |
+------+------+
他想将c1列的值改成1、c2的值改成'b',然后用了如下sql:
update test set c1=1 and c2='b' where c1=0;
可以发现这个sql写法是错误的,正确写法应该是:
update test set c1=1,c2='b' where c1=0;
但第一个错误的sql运行没报错,因为被MySQL理解成:
update test set c1=(1 and c2='b') where c1=0;
=>
update test set c1=(1 and 0) where c1=0;
==>
update test set c1=0 where c1=0;
所以错误的sql相当啥都不做,但不仔细观察and应该改成逗号,还会觉得蛮诡异呢~
2.mybatis 批量删除
int delete(String[] usernos);
<delete id="delete" parameterType="java.lang.String | map">
DELETE FROM USER
WHERE
USERNO IN
<foreach collection="array" item="id" open="(" separator="," close=")">
#{userno,jdbcType.VARCHAR}
</foreach>
</delete>
3.mybatis 更新
1 <update id="update" parameterType="com.cib.bonds.query.vo.User"> 2 UPDATE USER 3 <trim prefix="SET" suffixOverrides=","> 4 <if test="loginName != null and loginName != ''">
LOGINNAME = #{loginName,jdbcType=VARCHAR},
</if> 5 <if test="password != null and password != ''">
PASSWORD = #{password,jdbcType=VARCHAR},
</if> 6 </trim> 7 WHERE 8 USERNO = #{userno,jdbcType=VARCHAR} 9 </update>
4.other
1 <!--<![CDATA[ ]]> --> 2 <select id="findAllInfos" parameterType="com.cib.bonds.query.vo.User" resultMap="userMap"> 3 SELECT 4 USERNO,IDTYPE,IDNUMBER,STATUS,BANKNO,LOGINNAME,PASSWORD,GENDER 5 FROM 6 USER 7 WHERE 8 1=1 9 <if test="status != null and status != ''">
AND STATUS = #{status,jdbcType=CHAR}
</if> 10 <if test="loginName != null and loginName != ''">
AND LOGINNAME = #{loginName,jdbcType=VARCHAR}
</if> 11 <if test="password != null and password != ''">
AND PASSWORD = #{password,jdbcType=VARCHAR}
</if> 12 </select> 13 14 <select id="findAllInfosCount" parameterType="com.cib.bonds.query.vo.User" resultType="int"> 15 SELECT 16 COUNT(*) 17 FROM 18 USER 19 WHERE 20 1=1 21 <if test="status != null and status != ''">
AND STATUS = #{status,jdbcType=CHAR}
</if> 22 <if test="loginName != null and loginName != ''">
AND LOGINNAME = #{loginName,jdbcType=VARCHAR}
</if> 23 <if test="password != null and password != ''">
AND PASSWORD = #{password,jdbcType=VARCHAR}
</if> 24 </select> 25 26 <select id="findByUserNo" parameterType="java.lang.String" resultMap="userMap"> 27 SELECT 28 USERNO,LOGINNAME,PASSWORD,GENDER 29 FROM 30 USER 31 WHERE 32 USERNO = #{userno,jdbcType=VARCHAR} 33 </select> 34 35 <insert id="insert" parameterType="com.cib.bonds.query.vo.User"> 36 INSERT INTO USER 37 (USERNO,IDTYPE,IDNUMBER,STATUS,BANKNO,LOGINNAME,PASSWORD,GENDER) 38 VALUES 39 (#{userno,jdbcType=VARCHAR}, 40 #{idType,jdbcType=CHAR}, 41 #{idNumber,jdbcType=VARCHAR}, 42 #{status,jdbcType=CHAR}, 43 #{bankNo,jdbcType=VARCHAR}, 44 #{loginName,jdbcType=VARCHAR}, 45 #{password,jdbcType=VARCHAR}, 46 #{gender,jdbcType=CHAR}) 47 </insert> 48