Mybatis 返回主键操作

1.xml文件编写useGeneratedKeys和keyProperty


    
    
        INSERT INTO car
        VALUES
        (
                NULL,
                #{car.carNumber},
                #{car.driver},
                #{car.phone},
                #{car.color},
        );
    
    

2.mapper.java类

    /**
      * 添加一条数据 
     * @param car
     * @return
     */
    int addCarOne(@Param("car") Car car);

3.测试类

    @Test
    public void Text01() {
        Car car=new Car();
        car.setCarNumber("广202233");
        car.setDriver("张珊");
        car.setPhone("111");
        int addCarOne = carmapper.addCarOne(car);
        System.out.println("addCarOne     "+addCarOne);
        System.out.println("getId     "+car.getId());
    }

4.打印的结果

addCarOne     1
getId     30

你可能感兴趣的:(Mybatis 返回主键操作)