mybatis中insert语句获得自增id的两种实现

表结构

CREATE TABLE `person` (
  `person_id` int(11) NOT NULL AUTO_INCREMENT,
  `person_name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`person_id`)
)

pojo文件

package com.xjj.pojo;

public class Person {
    private Integer personId;
    private String personName;
	/*省略了getter和setter方法*/
}

mapper接口

package com.xjj.mapper;

import com.xjj.pojo.Person;

public interface PersonMapper {
    /**
     * 插入一个对象同时获取它自增的id,使用useGeneratedKeys
     * @param person
     */
    void useGeneratedKeys(Person person);

    /**
     * 插入一个对象的同时获取它自增的id,使用selectKey
     * @param person
     */
    void selectKey(Person person);
}

mapper实现类

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xjj.mapper.PersonMapper">
    <!--加上useGeneratedKeys这个属性,将person_id的值赋给personId这个属性-->
    <insert id="useGeneratedKeys" useGeneratedKeys="true" keyColumn="person_id" keyProperty="personId">
        insert into person(person_name) value(#{personName})
    </insert>
    <insert id="selectKey">
        insert into person(person_name) value(#{personName});
        /*和useGeneratedKeys的道理差不多,order是指是在sql执行前执行还是sql执行后执行,resultType必写。*/
        <selectKey order="AFTER" resultType="java.lang.Integer" keyColumn="person_id" keyProperty="personId">
            select LAST_INSERT_ID();
        </selectKey>
    </insert>
</mapper>

service接口

package com.xjj.service;

import com.xjj.pojo.Person;

public interface PersonService {
    void useGeneratedKeys(Person person);
    void selectKey(Person person);
}

service实现类

package com.xjj.service.impl;

import com.xjj.mapper.PersonMapper;
import com.xjj.pojo.Person;
import com.xjj.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("/personService")
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonMapper personMapper;

    @Override
    public void useGeneratedKeys(Person person) {
        personMapper.useGeneratedKeys(person);
    }

    @Override
    public void selectKey(Person person) {
        personMapper.selectKey(person);
    }
}

controller层

package com.xjj.controller;

import com.xjj.pojo.Person;
import com.xjj.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonService personService;

    @RequestMapping("useGeneratedKeys")
    public String useGeneratedKeys(Model model,Person person){
        personService.useGeneratedKeys(person);
        model.addAttribute("useGeneratedKeys",person);
        return "showPersonId";
    }
    @RequestMapping("selectKey")
    public String selectKey(Model model,Person person){
        personService.selectKey(person);
        model.addAttribute("selectKey",person);
        return "showPersonId";
    }
}

前台表单页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    使用useGeneratedKeys
    <form method="post" action="${pageContext.request.contextPath}/person/useGeneratedKeys">
        <input type="text" name="personName">
        <input type="submit" value="提交">
    </form>
    使用selectKey
    <form method="post" action="${pageContext.request.contextPath}/person/selectKey">
        <input type="text" name="personName">
        <input type="submit" value="提交">
    </form>
</body>
</html>

显示页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
使用useGeneratedKeys自增的id:${useGeneratedKeys.personId}<br>
使用selectKey自增的id:${selectKey.personId}
</body>
</html>

最终显示结果

head>
Title

使用useGeneratedKeys自增的id:${useGeneratedKeys.personId}
使用selectKey自增的id:${selectKey.personId} ```

最终显示结果
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值