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;
    }

 

上一篇:IDEA生成JavaDoc


下一篇:flask图书练习