问题说明:建表语句中deleted字段的默认值为1
CREATE TABLE `sys_permission` (
`id` varchar(64) NOT NULL COMMENT '主键',
`code` varchar(64) DEFAULT NULL COMMENT '菜单权限编码',
`name` varchar(300) DEFAULT NULL COMMENT '菜单权限名称',
`perms` varchar(500) DEFAULT NULL COMMENT '授权(如:sys:user:add)',
`url` varchar(100) DEFAULT NULL COMMENT '访问地址URL',
`method` varchar(10) DEFAULT NULL COMMENT '资源请求类型',
`pid` varchar(64) DEFAULT NULL COMMENT '父级菜单权限名称',
`order_num` int(11) DEFAULT '0' COMMENT '排序',
`type` tinyint(4) DEFAULT NULL COMMENT '菜单权限类型(1:目录;2:菜单;3:按钮)',
`status` tinyint(4) DEFAULT '1' COMMENT '状态1:正常 0:禁用',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`deleted` tinyint(4) DEFAULT '1' COMMENT '是否删除(1未删除;0已删除)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
当我们插入一条数据时:
@Override
public SysPermission addPermission(PermissionAddReqVO permissionAddReqVO) {
SysPermission sysPermission = new SysPermission();
BeanUtils.copyProperties(permissionAddReqVO,sysPermission);
sysPermission.setId(UUID.randomUUID().toString());
sysPermission.setCreateTime(new Date());
sysPermissionDao.insert(sysPermission);
return sysPermission;
}
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into sys_permission(id, code, name, perms, url, method, pid, order_num, type, status, create_time, update_time, deleted)
values (#{id}, #{code}, #{name}, #{perms}, #{url}, #{method}, #{pid}, #{orderNum}, #{type}, #{status}, #{createTime}, #{updateTime}, #{deleted})
</insert>
结果发现数据库中这条新增数据的deleted字段为null,不是建表的时候设置了默认值为1吗?如何解决呢?
① 采用mybatis 的insert操作,但是不指定deleted字段 :
该方案可以让你在插入操作时取默认值,但是插入时只能取默认值,取不了其他值,只有在后续的update操作中才能取到其他值,不适合在插入时还有其他值的应用场景。
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into sys_permission(id, code, name, perms, url, method, pid, order_num, type, status, create_time, update_time)
values (#{id}, #{code}, #{name}, #{perms}, #{url}, #{method}, #{pid}, #{orderNum}, #{type}, #{status}, #{createTime}, #{updateTime})
</insert>
② 采用mybatis 的insertSelective操作:
insertSelective操作生成的insert语句中字段是动态生成的,如果你传入的实体对象中的属性值为null时,生成的insert语句就不会指定该字段,从而插入的时候,如果字段设置了默认值会直接取默认值。
<insert id="insertSelective">
insert into sys_permission
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="code != null">
code,
</if>
<if test="name != null">
`name`,
</if>
<if test="perms != null">
perms,
</if>
<if test="url != null">
url,
</if>
<if test="method != null">
`method`,
</if>
<if test="pid != null">
pid,
</if>
<if test="orderNum != null">
order_num,
</if>
<if test="type != null">
`type`,
</if>
<if test="status != null">
`status`,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
<if test="deleted != null">
deleted,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="code != null">
#{code,jdbcType=VARCHAR},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="perms != null">
#{perms,jdbcType=VARCHAR},
</if>
<if test="url != null">
#{url,jdbcType=VARCHAR},
</if>
<if test="method != null">
#{method,jdbcType=VARCHAR},
</if>
<if test="pid != null">
#{pid,jdbcType=VARCHAR},
</if>
<if test="orderNum != null">
#{orderNum,jdbcType=INTEGER},
</if>
<if test="type != null">
#{type,jdbcType=TINYINT},
</if>
<if test="status != null">
#{status,jdbcType=TINYINT},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
<if test="deleted != null">
#{deleted,jdbcType=TINYINT},
</if>
</trim>
</insert>
|