// 备注 我用的是 sqlserver 数据库
public class SysUser {
private int id; // 在 数据库表中是 自增长 步长 为 1 int 类型
private String userName;
private String password;
private String salt;
private String roleIds;
private int locked;
// ...... 省略 get set 方法
}
// xml文件
<insert id="addUser">
<selectKey resultType="int" keyProperty="id" order="AFTER">
select @@IDENTITY as id
</selectKey>
insert into sys_user(
username,
password,
salt,
role_ids,
locked
)
values(
#{userName},
#{password},
#{salt},
#{roleIds},
#{locked}
)
</insert>
// 或者
<insert id="addUser" useGeneratedKeys="true" keyProperty="id" parameterType="com.zh.entity.SysUser">
insert into sys_user(
username,
password,
salt,
role_ids,
locked
)
values(
#{userName},
#{password},
#{salt},
#{roleIds},
#{locked}
)
</insert>
// 以上两种配置均可
// Controller
@RequestMapping(value="/add")
public String add(String userName,String password,
String salt,String roleIds,int locked){
try{
SysUser sysUser=new SysUser();
sysUser.setUserName(userName);
sysUser.setPassword(password);
sysUser.setSalt(salt);
sysUser.setRoleIds(roleIds);
sysUser.setLocked(locked);
int bol=services.addUser(sysUser);
if(bol>0){
System.out.println("新增成功 ");
System.out.println("获取新增之后对象的ID = "+sysUser.getId());
}
}catch(Exception e){
e.printStackTrace();
}
return "addUser";
}