MyBatis插入返回主键id

 

 

<!--
    <insert id="addBook" parameterType="com.example.pojo.Book">
        INSERT INTO book(name,author) VALUES (#{name},#{author})
    </insert>
    <insert id="addBook" keyProperty="id" useGeneratedKeys="true"
 parameterType="com.example.pojo.Book">
        INSERT INTO book(name,author) VALUES (#{name},#{author})

     </insert>
     -->
    <insert id="addBook" parameterType="com.example.pojo.Book">
        <selectKey keyProperty="id" resultType="int" order="BEFORE">
            <!--
            select idseq.nextVal from dual
            -->
            SELECT
            auto_increment
            FROM
            information_schema.`TABLES`
            WHERE
            table_name = 'book'
            AND TABLE_SCHEMA = 'vhr'
        </selectKey>
        INSERT INTO book(id,name,author) VALUES (#{id},#{name},#{author})
    </insert>

 

 @Override
    public int addBook(Book book) {
//        return bookMapper.addBook(book);
//        int insertNum = Integer.parseInt(bookMapper.addBook(book) + "");
        int insertNum = bookMapper.addBook(book);
        Integer id = book.getId();
        logger.info(insertNum+"------BookServiceImpl.addBook()-----------"+id);
        return id;
    }

 

 

<insert id="insertEnterprise" useGeneratedKeys="true" keyProperty="id"  parameterType="com.entity.Enterprise">
        <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into enterprise
        (id,name,logo,province_code,city_code,area_code,
        location,
        address,create_time,update_time,deleted)
        values
        (#{id},#{name},#{logo},#{provinceCode},#{cityCode},#{areaCode},
        ST_GeomFromText(#{location}),
        #{address},#{createTime},#{updateTime},0)
    </insert>

 

posted @ 2021-04-03 14:27  残星  阅读(292)  评论(0编辑  收藏  举报