MyBatis+SqlServer中,主键字段不要生成到插入语句中
<table tableName="Customer"> <columnOverride column="RowId" isGeneratedAlways="true"></columnOverride> </table>
MyBatis+SqlServer中,插入后返回主键值
代码生成器原来写法:
<table tableName="Customer"> <generatedKey column="RowId" sqlStatement="JDBC" identity="true"/> </table>
生成Mapper中的Insert语句:
<insert id="insert" parameterType="com.LTSolution.ShopApp.Model.Customer"> <selectKey keyProperty="rowid" order="AFTER" resultType="java.lang.Integer"> SELECT SCOPE_IDENTITY() </selectKey> insert into Company (...) values (...) </insert>
按照查看相关书籍和资料,这个写法应该是可行的,但是实际不行。
经过测试使用下面的写法成功插入后返回主键
代码生成器写法:
<table tableName="Customer"> <generatedKey column="RowId" sqlStatement="JDBC" identity="true"/> </table>
生成Mapper中的Insert语句:
<insert id="insert" keyColumn="RowId" keyProperty="rowid" parameterType="com.LTSolution.ShopApp.Model.Customer" useGeneratedKeys="true"> insert into Customer (...) values (...) </insert>