今天spring mvc遇到的问题,之前一直没有注意的。
一、问题场景
数据库的id设置为primary key,如果尝试插入相同的id,直接报错违反唯一约束条件。
希望不报错影响程序完整性,如果违反唯一约束条件,直接返回插入失败。
二、解决办法
(1)不insert id,设置id为自增,可以确保id唯一
需要修改数据库配置。
但是这种方式限制了id字段为自增型,由需求限制。
(2)不对数据库本身做改动,直接在service中catch错误,返回错误提示即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@Override public int insert(shop record) { try { return shopmapper.insert(record); } catch (Exception e) { return 0; } } @Override public int insertSelective(shop record) { try { return shopmapper.insertSelective(record); } catch (Exception e) { return 0; } } |
这种方式要求开发者不能有遗漏。
(3)进行mybatis映射文件的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
<insert id="insert" parameterType="com.xt.pojo.users"> <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select sys_guid() from dual </selectKey> insert into USERS (ID, ACCOUNT, NAME, TYPE, PASSWORD, SFZH, SEX, HEAD_ICON, ADDRESS, BMID, PHONE, QQ, EMAIL, PRIVACY_CONFIG, STATUS) values (#{id,jdbcType=VARCHAR}, #{account,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{type,jdbcType=DECIMAL}, #{password,jdbcType=VARCHAR}, #{sfzh,jdbcType=VARCHAR}, #{sex,jdbcType=DECIMAL}, #{headIcon,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{bmid,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{privacyConfig,jdbcType=DECIMAL}, #{status,jdbcType=DECIMAL}) </insert> <insert id="insertSelective" parameterType="com.xt.pojo.users"> <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select sys_guid() from dual </selectKey> insert into USERS <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> ID, </if> <if test="account != null"> ACCOUNT, </if> <if test="name != null"> NAME, </if> <if test="type != null"> TYPE, </if> <if test="password != null"> PASSWORD, </if> <if test="sfzh != null"> SFZH, </if> <if test="sex != null"> SEX, </if> <if test="headIcon != null"> HEAD_ICON, </if> <if test="address != null"> ADDRESS, </if> <if test="bmid != null"> BMID, </if> <if test="phone != null"> PHONE, </if> <if test="qq != null"> QQ, </if> <if test="email != null"> EMAIL, </if> <if test="privacyConfig != null"> PRIVACY_CONFIG, </if> <if test="status != null"> STATUS, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=VARCHAR}, </if> <if test="account != null"> #{account,jdbcType=VARCHAR}, </if> <if test="name != null"> #{name,jdbcType=VARCHAR}, </if> <if test="type != null"> #{type,jdbcType=DECIMAL}, </if> <if test="password != null"> #{password,jdbcType=VARCHAR}, </if> <if test="sfzh != null"> #{sfzh,jdbcType=VARCHAR}, </if> <if test="sex != null"> #{sex,jdbcType=DECIMAL}, </if> <if test="headIcon != null"> #{headIcon,jdbcType=VARCHAR}, </if> <if test="address != null"> #{address,jdbcType=VARCHAR}, </if> <if test="bmid != null"> #{bmid,jdbcType=VARCHAR}, </if> <if test="phone != null"> #{phone,jdbcType=VARCHAR}, </if> <if test="qq != null"> #{qq,jdbcType=VARCHAR}, </if> <if test="email != null"> #{email,jdbcType=VARCHAR}, </if> <if test="privacyConfig != null"> #{privacyConfig,jdbcType=DECIMAL}, </if> <if test="status != null"> #{status,jdbcType=DECIMAL}, </if> </trim> </insert> |
关键就是:
1 2 3 |
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select sys_guid() from dual </selectKey> |
这段话保证插入的时候随机取id。
插入的id会变成一长串:1F8EB364DE194BCFB22FB5D502284971。
id变得很长很复杂,而且会覆盖插入时候的id,无法指定id进行插入。
三、总结
反正就是根据需求来,不要吞错误就可以了。