获取自增的主键
全局配置
yml
mybatis:
configuration:
use-generated-keys: true
xml
<settings>
<setting name="useGeneratedKeys" value="true" />
</settings>
selectKey
keyProperty 主键名 和返回值对应
keyColumn 匹配属性的返回结果集中的列名称
order
BEFORE 首先选择主键,设置keyProperty然后执行插入语句
AFTER 先执行插入语句,然后是selectKey元素
mysql
<insert id="insertBlogMysql" parameterType="Blog">
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO `blog` ( )
VALUES ( )
</insert>
Oracle
<insert id="insertBlogOracle" parameterType="Blog">
<selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">
select seq.nextval as id from dual
</selectKey>
INSERT INTO `blog` ( )
VALUES ( )
</insert>
resultType
<select id="findAll" resultType="com.itheima.domain.User">
select userAddress from user
</select>