mybatis generator生成的插入方法执行后,返回自增主键

This commit is contained in:
shuzheng 2016-10-17 15:16:16 +08:00
parent de057c6b74
commit f821cfe687
11 changed files with 97 additions and 194 deletions

View File

@ -106,17 +106,18 @@
</if> </if>
</delete> </delete>
<insert id="insert" parameterType="com.zheng.cms.model.Book" > <insert id="insert" parameterType="com.zheng.cms.model.Book" >
insert into book (id, userid, name <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
) SELECT LAST_INSERT_ID()
values (#{id,jdbcType=INTEGER}, #{userid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR} </selectKey>
) insert into book (userid, name)
values (#{userid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
</insert> </insert>
<insert id="insertSelective" parameterType="com.zheng.cms.model.Book" > <insert id="insertSelective" parameterType="com.zheng.cms.model.Book" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into book insert into book
<trim prefix="(" suffix=")" suffixOverrides="," > <trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="userid != null" > <if test="userid != null" >
userid, userid,
</if> </if>
@ -125,9 +126,6 @@
</if> </if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides="," > <trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="userid != null" > <if test="userid != null" >
#{userid,jdbcType=INTEGER}, #{userid,jdbcType=INTEGER},
</if> </if>

View File

@ -144,25 +144,28 @@
</if> </if>
</delete> </delete>
<insert id="insert" parameterType="com.zheng.cms.model.CmsArticle" > <insert id="insert" parameterType="com.zheng.cms.model.CmsArticle" >
insert into cms_article (article_id, title, author, <selectKey resultType="java.lang.Integer" keyProperty="articleId" order="AFTER" >
fromurl, image, keywords, SELECT LAST_INSERT_ID()
description, type, allowcomments, </selectKey>
status, user_id, up, insert into cms_article (title, author, fromurl,
down, readnumber, ctime, image, keywords, description,
orders, content) type, allowcomments, status,
values (#{articleId,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, user_id, up, down, readnumber,
#{fromurl,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR}, #{keywords,jdbcType=VARCHAR}, ctime, orders, content
#{description,jdbcType=VARCHAR}, #{type,jdbcType=TINYINT}, #{allowcomments,jdbcType=TINYINT}, )
#{status,jdbcType=TINYINT}, #{userId,jdbcType=INTEGER}, #{up,jdbcType=INTEGER}, values (#{title,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, #{fromurl,jdbcType=VARCHAR},
#{down,jdbcType=INTEGER}, #{readnumber,jdbcType=INTEGER}, #{ctime,jdbcType=BIGINT}, #{image,jdbcType=VARCHAR}, #{keywords,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR},
#{orders,jdbcType=BIGINT}, #{content,jdbcType=LONGVARCHAR}) #{type,jdbcType=TINYINT}, #{allowcomments,jdbcType=TINYINT}, #{status,jdbcType=TINYINT},
#{userId,jdbcType=INTEGER}, #{up,jdbcType=INTEGER}, #{down,jdbcType=INTEGER}, #{readnumber,jdbcType=INTEGER},
#{ctime,jdbcType=BIGINT}, #{orders,jdbcType=BIGINT}, #{content,jdbcType=LONGVARCHAR}
)
</insert> </insert>
<insert id="insertSelective" parameterType="com.zheng.cms.model.CmsArticle" > <insert id="insertSelective" parameterType="com.zheng.cms.model.CmsArticle" >
<selectKey resultType="java.lang.Integer" keyProperty="articleId" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into cms_article insert into cms_article
<trim prefix="(" suffix=")" suffixOverrides="," > <trim prefix="(" suffix=")" suffixOverrides="," >
<if test="articleId != null" >
article_id,
</if>
<if test="title != null" > <if test="title != null" >
title, title,
</if> </if>
@ -213,9 +216,6 @@
</if> </if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides="," > <trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="articleId != null" >
#{articleId,jdbcType=INTEGER},
</if>
<if test="title != null" > <if test="title != null" >
#{title,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR},
</if> </if>

View File

@ -113,21 +113,22 @@
</if> </if>
</delete> </delete>
<insert id="insert" parameterType="com.zheng.cms.model.CmsCategory" > <insert id="insert" parameterType="com.zheng.cms.model.CmsCategory" >
insert into cms_category (category_id, pid, level, <selectKey resultType="java.lang.Integer" keyProperty="categoryId" order="AFTER" >
name, description, icon, SELECT LAST_INSERT_ID()
type, alias, ctime, </selectKey>
orders) insert into cms_category (pid, level, name,
values (#{categoryId,jdbcType=INTEGER}, #{pid,jdbcType=INTEGER}, #{level,jdbcType=TINYINT}, description, icon, type,
#{name,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR}, alias, ctime, orders)
#{type,jdbcType=TINYINT}, #{alias,jdbcType=VARCHAR}, #{ctime,jdbcType=BIGINT}, values (#{pid,jdbcType=INTEGER}, #{level,jdbcType=TINYINT}, #{name,jdbcType=VARCHAR},
#{orders,jdbcType=BIGINT}) #{description,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR}, #{type,jdbcType=TINYINT},
#{alias,jdbcType=VARCHAR}, #{ctime,jdbcType=BIGINT}, #{orders,jdbcType=BIGINT})
</insert> </insert>
<insert id="insertSelective" parameterType="com.zheng.cms.model.CmsCategory" > <insert id="insertSelective" parameterType="com.zheng.cms.model.CmsCategory" >
<selectKey resultType="java.lang.Integer" keyProperty="categoryId" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into cms_category insert into cms_category
<trim prefix="(" suffix=")" suffixOverrides="," > <trim prefix="(" suffix=")" suffixOverrides="," >
<if test="categoryId != null" >
category_id,
</if>
<if test="pid != null" > <if test="pid != null" >
pid, pid,
</if> </if>
@ -157,9 +158,6 @@
</if> </if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides="," > <trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="categoryId != null" >
#{categoryId,jdbcType=INTEGER},
</if>
<if test="pid != null" > <if test="pid != null" >
#{pid,jdbcType=INTEGER}, #{pid,jdbcType=INTEGER},
</if> </if>

View File

@ -111,19 +111,22 @@
</if> </if>
</delete> </delete>
<insert id="insert" parameterType="com.zheng.cms.model.CmsTag" > <insert id="insert" parameterType="com.zheng.cms.model.CmsTag" >
insert into cms_tag (tag_id, name, description, <selectKey resultType="java.lang.Integer" keyProperty="tagId" order="AFTER" >
icon, type, alias, SELECT LAST_INSERT_ID()
ctime, orders) </selectKey>
values (#{tagId,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, insert into cms_tag (name, description, icon,
#{icon,jdbcType=VARCHAR}, #{type,jdbcType=TINYINT}, #{alias,jdbcType=VARCHAR}, type, alias, ctime,
#{ctime,jdbcType=BIGINT}, #{orders,jdbcType=BIGINT}) orders)
values (#{name,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR},
#{type,jdbcType=TINYINT}, #{alias,jdbcType=VARCHAR}, #{ctime,jdbcType=BIGINT},
#{orders,jdbcType=BIGINT})
</insert> </insert>
<insert id="insertSelective" parameterType="com.zheng.cms.model.CmsTag" > <insert id="insertSelective" parameterType="com.zheng.cms.model.CmsTag" >
<selectKey resultType="java.lang.Integer" keyProperty="tagId" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into cms_tag insert into cms_tag
<trim prefix="(" suffix=")" suffixOverrides="," > <trim prefix="(" suffix=")" suffixOverrides="," >
<if test="tagId != null" >
tag_id,
</if>
<if test="name != null" > <if test="name != null" >
name, name,
</if> </if>
@ -147,9 +150,6 @@
</if> </if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides="," > <trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="tagId != null" >
#{tagId,jdbcType=INTEGER},
</if>
<if test="name != null" > <if test="name != null" >
#{name,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
</if> </if>

View File

@ -133,19 +133,22 @@
</if> </if>
</delete> </delete>
<insert id="insert" parameterType="com.zheng.cms.model.User" > <insert id="insert" parameterType="com.zheng.cms.model.User" >
insert into user (id, username, password, <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
nickname, sex, ctime, SELECT LAST_INSERT_ID()
content) </selectKey>
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, insert into user (username, password, nickname,
#{nickname,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, #{ctime,jdbcType=BIGINT}, sex, ctime, content
#{content,jdbcType=LONGVARCHAR}) )
values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{nickname,jdbcType=VARCHAR},
#{sex,jdbcType=INTEGER}, #{ctime,jdbcType=BIGINT}, #{content,jdbcType=LONGVARCHAR}
)
</insert> </insert>
<insert id="insertSelective" parameterType="com.zheng.cms.model.User" > <insert id="insertSelective" parameterType="com.zheng.cms.model.User" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into user insert into user
<trim prefix="(" suffix=")" suffixOverrides="," > <trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="username != null" > <if test="username != null" >
username, username,
</if> </if>
@ -166,9 +169,6 @@
</if> </if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides="," > <trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="username != null" > <if test="username != null" >
#{username,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR},
</if> </if>

View File

@ -48,15 +48,26 @@
<javaClientGenerator targetPackage="com.zheng.cms.mapper" targetProject="src/main/java" type="XMLMAPPER" /> <javaClientGenerator targetPackage="com.zheng.cms.mapper" targetProject="src/main/java" type="XMLMAPPER" />
<!-- 需要映射的表 --> <!-- 需要映射的表 -->
<table tableName="user" domainObjectName="User"></table> <table tableName="user" domainObjectName="User">
<table tableName="book" domainObjectName="Book"></table> <generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
<table tableName="cms_article" domainObjectName="CmsArticle"></table> <table tableName="book" domainObjectName="Book">
<table tableName="cms_category" domainObjectName="CmsCategory"></table> <generatedKey column="id" sqlStatement="MySql" identity="true" />
<table tableName="cms_tag" domainObjectName="CmsTag"></table> </table>
<table tableName="cms_article" domainObjectName="CmsArticle">
<generatedKey column="article_id" sqlStatement="MySql" identity="true" />
</table>
<table tableName="cms_category" domainObjectName="CmsCategory">
<generatedKey column="category_id" sqlStatement="MySql" identity="true" />
</table>
<table tableName="cms_tag" domainObjectName="CmsTag">
<generatedKey column="tag_id" sqlStatement="MySql" identity="true" />
</table>
<table tableName="cms_article_category" domainObjectName="CmsArticleCategory"></table> <table tableName="cms_article_category" domainObjectName="CmsArticleCategory"></table>
<table tableName="cms_article_tag" domainObjectName="CmsArticleTag"></table> <table tableName="cms_article_tag" domainObjectName="CmsArticleTag"></table>
<table tableName="cms_category_tag" domainObjectName="CmsCategoryTag"></table> <table tableName="cms_category_tag" domainObjectName="CmsCategoryTag"></table>
<table tableName="cms_comment" domainObjectName="CmsComment"></table> <table tableName="cms_comment" domainObjectName="CmsComment">
<generatedKey column="case_id" sqlStatement="MySql" identity="true" />
</table>
</context> </context>
</generatorConfiguration> </generatorConfiguration>

View File

@ -18,8 +18,4 @@ public interface UserVOMapper extends UserMapper {
UserVO selectUserWithBook(int id); UserVO selectUserWithBook(int id);
List<User> selectAll(Map<String, Object> map);
void insertAutoKey(User user);
} }

View File

@ -36,48 +36,4 @@
u.id=#{id,jdbcType=INTEGER} u.id=#{id,jdbcType=INTEGER}
</select> </select>
<!-- 查询所有记录 -->
<select id="selectAll" resultType="user" parameterType="java.util.Map">
select
<choose>
<when test="clumns != null">${clumns}</when>
<otherwise>
*
</otherwise>
</choose>
from
user
<where>
<if test="condition != null and condition != ''">${condition}</if>
</where>
order by
<choose>
<when test="order != null and order != ''">${order}</when>
<otherwise>id asc</otherwise>
</choose>
</select>
<!-- 插入单条记录插入前建好id索引 -->
<insert id="insertAutoKey" useGeneratedKeys="true" keyProperty="id" parameterType="user">
insert into
user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="username != null">username,</if>
<if test="password != null">password,</if>
<if test="nickname != null">nickname,</if>
<if test="sex != null">sex,</if>
<if test="ctime != null">ctime,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id,jdbcType=INTEGER},</if>
<if test="username != null">#{username,jdbcType=VARCHAR},</if>
<if test="password != null">#{password,jdbcType=VARCHAR},</if>
<if test="nickname != null">#{nickname,jdbcType=VARCHAR},</if>
<if test="sex != null">#{sex,jdbcType=INTEGER},</if>
<if test="ctime != null">#{ctime,jdbcType=BIGINT},</if>
</trim>
</insert>
</mapper> </mapper>

View File

@ -1,10 +1,6 @@
package com.zheng.cms.service; package com.zheng.cms.service;
import java.util.List;
import java.util.Map;
import com.zheng.cms.mapper.UserMapper; import com.zheng.cms.mapper.UserMapper;
import com.zheng.cms.model.User;
import com.zheng.cms.model.UserVO; import com.zheng.cms.model.UserVO;
/** /**
@ -21,17 +17,4 @@ public interface UserService extends BaseService<UserMapper> {
*/ */
UserVO selectUserWithBook(int id); UserVO selectUserWithBook(int id);
/**
* 根据条件获取用户列表
* @param map
* @return
*/
List<User> selectAll(Map<String, Object> map);
/**
* 插入用户并返回主键
* @param user
*/
void insertAutoKey(User user);
} }

View File

@ -1,18 +1,12 @@
package com.zheng.cms.service.impl; package com.zheng.cms.service.impl;
import java.util.List;
import java.util.Map;
import com.zheng.cms.mapper.UserMapper; import com.zheng.cms.mapper.UserMapper;
import com.zheng.cms.model.User;
import com.zheng.cms.model.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zheng.cms.mapper.UserVOMapper; import com.zheng.cms.mapper.UserVOMapper;
import com.zheng.cms.model.UserVO; import com.zheng.cms.model.UserVO;
import com.zheng.cms.service.UserService; import com.zheng.cms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/** /**
* 用户service实现 * 用户service实现
@ -48,24 +42,4 @@ public class UserServiceImpl implements UserService {
return userVOMapper.selectUserWithBook(id); return userVOMapper.selectUserWithBook(id);
} }
/**
* 根据条件获取用户列表
* @param map
* @return
*/
@Override
public List<User> selectAll(Map<String, Object> map) {
return userVOMapper.selectAll(map);
}
/**
* 插入用户并返回主键
* @param user
*/
@Override
public void insertAutoKey(User user) {
userVOMapper.insertAutoKey(user);
}
} }

View File

@ -31,7 +31,7 @@ import java.util.Map;
@RequestMapping("/user") @RequestMapping("/user")
public class UserController extends BaseController { public class UserController extends BaseController {
private static Logger logger = LoggerFactory.getLogger(UserController.class); private static Logger _log = LoggerFactory.getLogger(UserController.class);
@Autowired @Autowired
private UserService userService; private UserService userService;
@ -59,8 +59,8 @@ public class UserController extends BaseController {
*/ */
@RequestMapping("/list") @RequestMapping("/list")
public String list( public String list(
@RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "1", value = "page") int page,
@RequestParam(required = false, defaultValue = "20") int rows, @RequestParam(required = false, defaultValue = "20", value = "rows") int rows,
HttpServletRequest request, Model model) { HttpServletRequest request, Model model) {
UserExample userExample = new UserExample(); UserExample userExample = new UserExample();
@ -69,7 +69,7 @@ public class UserController extends BaseController {
userExample.setOffset((page -1) * rows); userExample.setOffset((page -1) * rows);
userExample.setLimit(rows); userExample.setLimit(rows);
userExample.setDistinct(false); userExample.setDistinct(false);
userExample.setOrderByClause(" id desc "); userExample.setOrderByClause(" id asc ");
List<User> users = userService.getMapper().selectByExample(userExample); List<User> users = userService.getMapper().selectByExample(userExample);
model.addAttribute("users", users); model.addAttribute("users", users);
@ -106,29 +106,16 @@ public class UserController extends BaseController {
public String add(@Valid User user, BindingResult binding) { public String add(@Valid User user, BindingResult binding) {
if (binding.hasErrors()) { if (binding.hasErrors()) {
for (ObjectError error : binding.getAllErrors()) { for (ObjectError error : binding.getAllErrors()) {
logger.error(error.getDefaultMessage()); _log.error(error.getDefaultMessage());
} }
return "/user/add"; return "/user/add";
} }
user.setCtime(System.currentTimeMillis()); user.setCtime(System.currentTimeMillis());
userService.getMapper().insertSelective(user);
return "redirect:/user/list";
}
/** userService.getMapper().insertSelective(user);
* 新增post2,返回自增主键值
* @param user _log.info("新增记录id为{}", user.getId());
* @param binding
* @return
*/
@RequestMapping(value = "/add2", method = RequestMethod.POST)
public String add2(@Valid User user, BindingResult binding) {
if (binding.hasErrors()) {
return "user/add";
}
user.setCtime(System.currentTimeMillis());
userService.insertAutoKey(user);
System.out.println(user.getId());
return "redirect:/user/list"; return "redirect:/user/list";
} }
@ -138,7 +125,7 @@ public class UserController extends BaseController {
* @return * @return
*/ */
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET) @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
public String delete(@PathVariable int id) { public String delete(@PathVariable("id") int id) {
userService.getMapper().deleteByPrimaryKey(id); userService.getMapper().deleteByPrimaryKey(id);
return "redirect:/user/list"; return "redirect:/user/list";
} }
@ -150,7 +137,7 @@ public class UserController extends BaseController {
* @return * @return
*/ */
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET) @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
public String update(@PathVariable int id, Model model) { public String update(@PathVariable("id") int id, Model model) {
model.addAttribute("user", userService.getMapper().selectByPrimaryKey(id)); model.addAttribute("user", userService.getMapper().selectByPrimaryKey(id));
return "/user/update"; return "/user/update";
} }
@ -164,7 +151,7 @@ public class UserController extends BaseController {
* @return * @return
*/ */
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST) @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
public String update(@PathVariable int id, @Valid User user, BindingResult binding, Model model) { public String update(@PathVariable("id") int id, @Valid User user, BindingResult binding, Model model) {
if (binding.hasErrors()) { if (binding.hasErrors()) {
model.addAttribute("errors", binding.getAllErrors()); model.addAttribute("errors", binding.getAllErrors());
return "user/update/" + id; return "user/update/" + id;