获取数据库表中自增长最新的id

时间:2024-07-16 09:35:50

mybatis

<insert id="InsertCourse">
insert into
training_course(type_id,course_title,course_summary,coverimg_url,course_content,message_type,
create_date,create_by,creater_role_id,creater_party_id,corresponding_party_id,attach_url,corresponding_region_id,corresponding_country_id,state,levelone_type_id,leveltwo_type_id,levelthree_type_id)
values(#{typeId},#{courseTitle},#{courseSummary},#{coverImgUrl},#{courseContent},#{messageType},
#{createDate},#{createBy},#{createrRoleId},#{createrPartyId},#{correspondingPartyId},#{attachUrl},#{correspondingRegionId},#{correspondingCountryId},#{state},#{levelOneTypeId},#{levelTwoTypeId},#{levelThreeTypeId})
<selectKey keyProperty="courseId" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>

action层

  1. public void CreateCourse()
  2. {
  3. JSONObject result=new JSONObject();
  4. response.setHeader("Content-Type", "application/json");
  5. try{
  6. request.setCharacterEncoding("UTF-8");
  7. int typeId=Integer.parseInt(request.getParameter("typeId"));
  8. int levelOneTypeId = Integer.parseInt(request.getParameter("levelOneTypeId"));
  1. int levelTwoTypeId = Integer.parseInt(request.getParameter("levelTwoTypeId"));
  2. int levelThreeTypeId = Integer.parseInt(request.getParameter("levelThreeTypeId"));
  3. char messageType=request.getParameter("messageType").charAt(0);
  4. String title=request.getParameter("courseTitle");
  5. String coverImgUrl=request.getParameter("coverImgUrl");
  6. coverImgUrl=coverImgUrl.replace("/var/www/topsale/topsale/", "http://obctop.tcl.com.cn/topsale/");
  7. // String attamentUrl=request.getParameter("attamentUrl");
  8. // if(attamentUrl!=""){
  9. // attamentUrl=attamentUrl.replace("/var/www/topsale/topsale/", "http://obctop.tcl.com.cn/topsale/");
  10. // }
  11. String summary=request.getParameter("courseSummary");
  12. String content=request.getParameter("courseContent");
  13. String partyId=request.getParameter("partyId");
  14. String regionId=request.getParameter("regionId");
  15. String countryId=request.getParameter("countryId");
  16. content=content.replace("/var/www/topsale/topsale/","http://obctop.tcl.com.cn/topsale/");
  17. TrainingCourse course=new TrainingCourse();
  18. course.setTypeId(typeId);
  19. course.setLevelOneTypeId(levelOneTypeId);
  20. course.setLevelTwoTypeId(levelTwoTypeId);
  21. course.setLevelThreeTypeId(levelThreeTypeId);
  1. course.setMessageType(messageType);
  2. course.setCourseTitle(title);
  3. course.setCourseSummary(summary);
  4. course.setCourseContent(content);
  5. course.setCoverImgUrl(coverImgUrl);
  6. // course.setAttachUrl(attamentUrl);
  7. course.setCreateDate(new Date());
  8. course.setCreateBy(WebPageUtil.getLoginedUserId());
  9. course.setCreaterRoleId(WebPageUtil.getLoginedUser().getRoleId());
  10. course.setCreaterPartyId(WebPageUtil.getLoginedUser().getPartyId());
  11. course.setCorrespondingPartyId(partyId);
  12. course.setCorrespondingRegionId(regionId);
  13. course.setCorrespondingCountryId(countryId);
  14. course.setState("0");
  15. result.accumulate("msg","success");
  16. courseService.InsertCourse(course);
  1. String allUserStr = request.getParameter("allUserStr");
  2. String roleType = request.getParameter("roleTypeName");
  3. String roleId =UUID.randomUUID().toString().replace("-", "").toLowerCase();
  4. roleId = roleType + "_" + roleId;
  1. System.out.println(allUserStr+"------------------------");
  2. //创建消息
  3. Message msg = new Message();
  1. msg.setMsgType("1");
  2. msg.setMsgTitle(title);
  3. msg.setMsgComment(content);
  4. msg.setMsgTitleUrl("<a href=coursedetail.jsp?courseId="+course.getCourseId()+">"); //获取对象中的courseId
  5. msg.setCreateBy(WebPageUtil.getLoginedUserId());
  6. msg.setCreateTime(new Date());
  7. msg.setCreateCountryId(countryId);
  8. msg.setCreatePartyId(WebPageUtil.getLoginedUser().getPartyId());
  9. msg.setMsgRoleId(roleId);
  10. msg.setMsgSummary(summary);
  11. msg.setMsgCoverUrl(coverImgUrl);
  12. msg.setCourseId(String.valueOf(course.getCourseId()));
  13. messageService.insertMessage(msg,allUserStr);
  14. } catch (Exception e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. log.error(e.getMessage(), e);
  18. String msg = e.getCause()==null?e.getMessage():e.getCause().getMessage().replaceAll("\"", "").replaceAll("\n", "");
  19. result.accumulate("success", true);
  20. result.accumulate("msg", msg);
  21. }
  22. WebPageUtil.writeBack(result.toString());
  23. }