Mybatis:注解方式插入获取自增主键

1.tab_user

 


Create Table

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

2.UserDao 

package com.zengjx.miaosha.dao;

import com.zengjx.miaosha.domain.User;
import org.apache.ibatis.annotations.*;

/**
 * @ClassName HelloController
 * @Description TODO
 * @Author zengjx
 * @Company zengjx
 * @Date 2020/1/13  19:33
 * @Version V1.0
 */
@Mapper
public interface UserDao {
    @Select("SELECT  id,name  FROM   USER   WHERE    id =#{id}")
    public User    getUserById( @Param("id") Integer  id);

    @Insert("insert into user (name) values  (#{name} )")
    @Options(useGeneratedKeys = true,keyProperty ="id",keyColumn = "id")

    public  int  inseartUser(User  user);
 }

3.UseService

package com.zengjx.miaosha.service.impl;

import com.zengjx.miaosha.dao.UserDao;
import com.zengjx.miaosha.domain.User;
import com.zengjx.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName HelloController
 * @Description TODO
 * @Author zengjx
 * @Company zengjx
 * @Date 2020/1/13  19:58
 * @Version V1.0
 */
@Service
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User getUserById(Integer id) {

        if(id<=0){

          //错误处理
        }

        return userDao.getUserById(id);
    }

    @Override
    public int inseartUser(User user) {

        int ret= userDao.inseartUser(user);
        System.out.println("ret ="+ret+"  userid"+user.getId());
        return ret;
    }
}

4.UserController

package com.zengjx.miaosha.controller;

import com.zengjx.miaosha.domain.User;
import com.zengjx.miaosha.result.Result;
import com.zengjx.miaosha.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;

/**
 * @ClassName HelloController
 * @Description TODO
 * @Author zengjx
 * @Company zengjx
 * @Date 2020/1/13  20:31
 * @Version V1.0
 */
@RestController
public class UserController {
   @Autowired
   private UserService   userService;
    @RequestMapping("/getUserById/{id}")
    public Result   getUserById( @PathVariable Integer  id)

    {
        Result   result=new Result();
        User user = userService.getUserById(id);
          result.setCode(1);
          result.setData(user);
          result.setMsg("查询成功");
        return  result;
    }
    @RequestMapping(value = "/inseartUser",method = RequestMethod.POST)
    public Result   inseartUser(@RequestBody User  user){
      int   ret=   userService.inseartUser(user);
      Result   result =new Result<>();
      result.setMsg("插入成功");
      result.setData(ret);
      return   result;
    }
}

 5.PostMan测试

Mybatis:注解方式插入获取自增主键_第1张图片

 Mybatis:注解方式插入获取自增主键_第2张图片

 自增饿主键:

你可能感兴趣的:(mybatis)