1. 程式人生 > >mybatis mysql 插入物件返回資料主鍵id

mybatis mysql 插入物件返回資料主鍵id

RuleTypeMapper.xml  檔案原始碼
<insert id="insertRuleType" useGeneratedKeys="true" keyProperty="id"  parameterType="com.haier.gsp.core.model.rule.RuleType" >
  insert into rule_type (type_name, create_user, create_date)
  values ( #{typeName,jdbcType=VARCHAR}, #{createUser,jdbcType=BIGINT},NOW())
</
insert>
RuleTypeServiceImpl.java  檔案原始碼
/**
 * 儲存規則類別
 * @param ruleType,uid
* @return
*/
@Override
@Transactional
public Response<String> insertRuleType(RuleType ruleType,Long uid) {
    Response<String> response = new Response<>();
    ruleType.setCreateUser(uid);
    System.out.println("前:"+ ruleType.getId());
    Integer id =  ruleTypeDao
.insertRuleType(ruleType);
    System.out.println("後:"+ ruleType.getId());
    //結果   前:0    後:18
List<ComBoxObj> users = ruleType.getUsers(); List<RuleType> records = new ArrayList<RuleType>(); for (ComBoxObj user:users){ RuleType types = new RuleType(); types.setId(ruleType.getId()); types.setUserId(Long.valueOf
(user.getKey())); types.setFlag(1);//1維護 2審批records.add(types); } Integer count = ruleTypeDao.saveRuleReation(records); if(id>0&&count>0){ response.setResult("操作成功!"); }else{ response.setError("操作失敗!"); } return response;}