mybatis generator生成的插入方法执行后,返回自增主键
This commit is contained in:
parent
de057c6b74
commit
f821cfe687
|
@ -106,17 +106,18 @@
|
|||
</if>
|
||||
</delete>
|
||||
<insert id="insert" parameterType="com.zheng.cms.model.Book" >
|
||||
insert into book (id, userid, name
|
||||
)
|
||||
values (#{id,jdbcType=INTEGER}, #{userid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}
|
||||
)
|
||||
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
|
||||
SELECT LAST_INSERT_ID()
|
||||
</selectKey>
|
||||
insert into book (userid, name)
|
||||
values (#{userid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
|
||||
</insert>
|
||||
<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
|
||||
<trim prefix="(" suffix=")" suffixOverrides="," >
|
||||
<if test="id != null" >
|
||||
id,
|
||||
</if>
|
||||
<if test="userid != null" >
|
||||
userid,
|
||||
</if>
|
||||
|
@ -125,9 +126,6 @@
|
|||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides="," >
|
||||
<if test="id != null" >
|
||||
#{id,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="userid != null" >
|
||||
#{userid,jdbcType=INTEGER},
|
||||
</if>
|
||||
|
|
|
@ -144,25 +144,28 @@
|
|||
</if>
|
||||
</delete>
|
||||
<insert id="insert" parameterType="com.zheng.cms.model.CmsArticle" >
|
||||
insert into cms_article (article_id, title, author,
|
||||
fromurl, image, keywords,
|
||||
description, type, allowcomments,
|
||||
status, user_id, up,
|
||||
down, readnumber, ctime,
|
||||
orders, content)
|
||||
values (#{articleId,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR},
|
||||
#{fromurl,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR}, #{keywords,jdbcType=VARCHAR},
|
||||
#{description,jdbcType=VARCHAR}, #{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})
|
||||
<selectKey resultType="java.lang.Integer" keyProperty="articleId" order="AFTER" >
|
||||
SELECT LAST_INSERT_ID()
|
||||
</selectKey>
|
||||
insert into cms_article (title, author, fromurl,
|
||||
image, keywords, description,
|
||||
type, allowcomments, status,
|
||||
user_id, up, down, readnumber,
|
||||
ctime, orders, content
|
||||
)
|
||||
values (#{title,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, #{fromurl,jdbcType=VARCHAR},
|
||||
#{image,jdbcType=VARCHAR}, #{keywords,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR},
|
||||
#{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 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
|
||||
<trim prefix="(" suffix=")" suffixOverrides="," >
|
||||
<if test="articleId != null" >
|
||||
article_id,
|
||||
</if>
|
||||
<if test="title != null" >
|
||||
title,
|
||||
</if>
|
||||
|
@ -213,9 +216,6 @@
|
|||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides="," >
|
||||
<if test="articleId != null" >
|
||||
#{articleId,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="title != null" >
|
||||
#{title,jdbcType=VARCHAR},
|
||||
</if>
|
||||
|
|
|
@ -113,21 +113,22 @@
|
|||
</if>
|
||||
</delete>
|
||||
<insert id="insert" parameterType="com.zheng.cms.model.CmsCategory" >
|
||||
insert into cms_category (category_id, pid, level,
|
||||
name, description, icon,
|
||||
type, alias, ctime,
|
||||
orders)
|
||||
values (#{categoryId,jdbcType=INTEGER}, #{pid,jdbcType=INTEGER}, #{level,jdbcType=TINYINT},
|
||||
#{name,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR},
|
||||
#{type,jdbcType=TINYINT}, #{alias,jdbcType=VARCHAR}, #{ctime,jdbcType=BIGINT},
|
||||
#{orders,jdbcType=BIGINT})
|
||||
<selectKey resultType="java.lang.Integer" keyProperty="categoryId" order="AFTER" >
|
||||
SELECT LAST_INSERT_ID()
|
||||
</selectKey>
|
||||
insert into cms_category (pid, level, name,
|
||||
description, icon, type,
|
||||
alias, ctime, orders)
|
||||
values (#{pid,jdbcType=INTEGER}, #{level,jdbcType=TINYINT}, #{name,jdbcType=VARCHAR},
|
||||
#{description,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR}, #{type,jdbcType=TINYINT},
|
||||
#{alias,jdbcType=VARCHAR}, #{ctime,jdbcType=BIGINT}, #{orders,jdbcType=BIGINT})
|
||||
</insert>
|
||||
<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
|
||||
<trim prefix="(" suffix=")" suffixOverrides="," >
|
||||
<if test="categoryId != null" >
|
||||
category_id,
|
||||
</if>
|
||||
<if test="pid != null" >
|
||||
pid,
|
||||
</if>
|
||||
|
@ -157,9 +158,6 @@
|
|||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides="," >
|
||||
<if test="categoryId != null" >
|
||||
#{categoryId,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="pid != null" >
|
||||
#{pid,jdbcType=INTEGER},
|
||||
</if>
|
||||
|
|
|
@ -111,19 +111,22 @@
|
|||
</if>
|
||||
</delete>
|
||||
<insert id="insert" parameterType="com.zheng.cms.model.CmsTag" >
|
||||
insert into cms_tag (tag_id, name, description,
|
||||
icon, type, alias,
|
||||
ctime, orders)
|
||||
values (#{tagId,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR},
|
||||
#{icon,jdbcType=VARCHAR}, #{type,jdbcType=TINYINT}, #{alias,jdbcType=VARCHAR},
|
||||
#{ctime,jdbcType=BIGINT}, #{orders,jdbcType=BIGINT})
|
||||
<selectKey resultType="java.lang.Integer" keyProperty="tagId" order="AFTER" >
|
||||
SELECT LAST_INSERT_ID()
|
||||
</selectKey>
|
||||
insert into cms_tag (name, description, icon,
|
||||
type, alias, ctime,
|
||||
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 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
|
||||
<trim prefix="(" suffix=")" suffixOverrides="," >
|
||||
<if test="tagId != null" >
|
||||
tag_id,
|
||||
</if>
|
||||
<if test="name != null" >
|
||||
name,
|
||||
</if>
|
||||
|
@ -147,9 +150,6 @@
|
|||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides="," >
|
||||
<if test="tagId != null" >
|
||||
#{tagId,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="name != null" >
|
||||
#{name,jdbcType=VARCHAR},
|
||||
</if>
|
||||
|
|
|
@ -133,19 +133,22 @@
|
|||
</if>
|
||||
</delete>
|
||||
<insert id="insert" parameterType="com.zheng.cms.model.User" >
|
||||
insert into user (id, username, password,
|
||||
nickname, sex, ctime,
|
||||
content)
|
||||
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
|
||||
#{nickname,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, #{ctime,jdbcType=BIGINT},
|
||||
#{content,jdbcType=LONGVARCHAR})
|
||||
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
|
||||
SELECT LAST_INSERT_ID()
|
||||
</selectKey>
|
||||
insert into user (username, password, nickname,
|
||||
sex, ctime, content
|
||||
)
|
||||
values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{nickname,jdbcType=VARCHAR},
|
||||
#{sex,jdbcType=INTEGER}, #{ctime,jdbcType=BIGINT}, #{content,jdbcType=LONGVARCHAR}
|
||||
)
|
||||
</insert>
|
||||
<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
|
||||
<trim prefix="(" suffix=")" suffixOverrides="," >
|
||||
<if test="id != null" >
|
||||
id,
|
||||
</if>
|
||||
<if test="username != null" >
|
||||
username,
|
||||
</if>
|
||||
|
@ -166,9 +169,6 @@
|
|||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides="," >
|
||||
<if test="id != null" >
|
||||
#{id,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="username != null" >
|
||||
#{username,jdbcType=VARCHAR},
|
||||
</if>
|
||||
|
|
|
@ -48,15 +48,26 @@
|
|||
<javaClientGenerator targetPackage="com.zheng.cms.mapper" targetProject="src/main/java" type="XMLMAPPER" />
|
||||
|
||||
<!-- 需要映射的表 -->
|
||||
<table tableName="user" domainObjectName="User"></table>
|
||||
<table tableName="book" domainObjectName="Book"></table>
|
||||
|
||||
<table tableName="cms_article" domainObjectName="CmsArticle"></table>
|
||||
<table tableName="cms_category" domainObjectName="CmsCategory"></table>
|
||||
<table tableName="cms_tag" domainObjectName="CmsTag"></table>
|
||||
<table tableName="user" domainObjectName="User">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true" />
|
||||
</table>
|
||||
<table tableName="book" domainObjectName="Book">
|
||||
<generatedKey column="id" sqlStatement="MySql" identity="true" />
|
||||
</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_tag" domainObjectName="CmsArticleTag"></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>
|
||||
</generatorConfiguration>
|
|
@ -18,8 +18,4 @@ public interface UserVOMapper extends UserMapper {
|
|||
|
||||
UserVO selectUserWithBook(int id);
|
||||
|
||||
List<User> selectAll(Map<String, Object> map);
|
||||
|
||||
void insertAutoKey(User user);
|
||||
|
||||
}
|
|
@ -35,49 +35,5 @@
|
|||
where
|
||||
u.id=#{id,jdbcType=INTEGER}
|
||||
</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>
|
|
@ -1,10 +1,6 @@
|
|||
package com.zheng.cms.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.zheng.cms.mapper.UserMapper;
|
||||
import com.zheng.cms.model.User;
|
||||
import com.zheng.cms.model.UserVO;
|
||||
|
||||
/**
|
||||
|
@ -20,18 +16,5 @@ public interface UserService extends BaseService<UserMapper> {
|
|||
* @return
|
||||
*/
|
||||
UserVO selectUserWithBook(int id);
|
||||
|
||||
/**
|
||||
* 根据条件获取用户列表
|
||||
* @param map
|
||||
* @return
|
||||
*/
|
||||
List<User> selectAll(Map<String, Object> map);
|
||||
|
||||
/**
|
||||
* 插入用户并返回主键
|
||||
* @param user
|
||||
*/
|
||||
void insertAutoKey(User user);
|
||||
|
||||
}
|
|
@ -1,18 +1,12 @@
|
|||
package com.zheng.cms.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.model.UserVO;
|
||||
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实现
|
||||
|
@ -48,24 +42,4 @@ public class UserServiceImpl implements UserService {
|
|||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -30,8 +30,8 @@ import java.util.Map;
|
|||
@Controller
|
||||
@RequestMapping("/user")
|
||||
public class UserController extends BaseController {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
private static Logger _log = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
@ -59,8 +59,8 @@ public class UserController extends BaseController {
|
|||
*/
|
||||
@RequestMapping("/list")
|
||||
public String list(
|
||||
@RequestParam(required = false, defaultValue = "1") int page,
|
||||
@RequestParam(required = false, defaultValue = "20") int rows,
|
||||
@RequestParam(required = false, defaultValue = "1", value = "page") int page,
|
||||
@RequestParam(required = false, defaultValue = "20", value = "rows") int rows,
|
||||
HttpServletRequest request, Model model) {
|
||||
|
||||
UserExample userExample = new UserExample();
|
||||
|
@ -69,7 +69,7 @@ public class UserController extends BaseController {
|
|||
userExample.setOffset((page -1) * rows);
|
||||
userExample.setLimit(rows);
|
||||
userExample.setDistinct(false);
|
||||
userExample.setOrderByClause(" id desc ");
|
||||
userExample.setOrderByClause(" id asc ");
|
||||
List<User> users = userService.getMapper().selectByExample(userExample);
|
||||
model.addAttribute("users", users);
|
||||
|
||||
|
@ -106,39 +106,26 @@ public class UserController extends BaseController {
|
|||
public String add(@Valid User user, BindingResult binding) {
|
||||
if (binding.hasErrors()) {
|
||||
for (ObjectError error : binding.getAllErrors()) {
|
||||
logger.error(error.getDefaultMessage());
|
||||
_log.error(error.getDefaultMessage());
|
||||
}
|
||||
return "/user/add";
|
||||
}
|
||||
user.setCtime(System.currentTimeMillis());
|
||||
|
||||
userService.getMapper().insertSelective(user);
|
||||
|
||||
_log.info("新增记录id为:{}", user.getId());
|
||||
|
||||
return "redirect:/user/list";
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增post2,返回自增主键值
|
||||
* @param user
|
||||
* @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";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
|
||||
public String delete(@PathVariable int id) {
|
||||
public String delete(@PathVariable("id") int id) {
|
||||
userService.getMapper().deleteByPrimaryKey(id);
|
||||
return "redirect:/user/list";
|
||||
}
|
||||
|
@ -150,7 +137,7 @@ public class UserController extends BaseController {
|
|||
* @return
|
||||
*/
|
||||
@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));
|
||||
return "/user/update";
|
||||
}
|
||||
|
@ -164,7 +151,7 @@ public class UserController extends BaseController {
|
|||
* @return
|
||||
*/
|
||||
@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()) {
|
||||
model.addAttribute("errors", binding.getAllErrors());
|
||||
return "user/update/" + id;
|
||||
|
|
Loading…
Reference in New Issue