PersonMapper.xml

    <!-- 插入数据 -->
    <insert id="insert" parameterType="com.itbto.mode1.Person">
        <!-- 
            selectKey:作主键返回
            keyProperty:接收主键的属性
            order:insert语句和生成主键的sql执行顺序,mysql是after,oracle是before
            resultType:返回主键的数据类型
            mysql生成主键的sql:select last_insert_id()
         -->
        <selectKey keyProperty="personId" order="AFTER" resultType="java.lang.Integer">
            select last_insert_id()
        </selectKey>
        insert into person(person_id,name,gender,person_addr,birthday) 
            values(#{personId},#{name},#{gender},#{personAddr},#{birthday})
    </insert>

测试

    @Test
    public void testInsert(){
        SqlSession session = sessionFactory.openSession();

        Person p = new Person();
        p.setName("李四");
        p.setGender(2);
        p.setPersonAddr("上海");
        p.setBirthday(new Date());

        try {
            int count = session.insert("com.itbto.mapper.PersonMapper.insert", p);
            session.commit();
            System.out.println(count);
            System.out.println(p.getPersonId());
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally{
            session.close();
        }
    }