In MyBatis+SqlServer, the primary key field should not be generated in the insert statement
<table tableName="Customer"> <columnOverride column="RowId" isGeneratedAlways="true"></columnOverride> </table>
In MyBatis+SqlServer, the primary key value is returned after insertion
The original way of writing the code generator:
<table tableName="Customer"> <generatedKey column="RowId" sqlStatement="JDBC" identity="true"/> </table>
Generate Insert statement in Mapper:
<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>
According to the relevant books and materials, this writing method should be feasible, but it is not practical.
After testing, the primary key is returned after successful insertion using the following writing method
Code generator writing:
<table tableName="Customer"> <generatedKey column="RowId" sqlStatement="JDBC" identity="true"/> </table>
Generate Insert statement in Mapper:
<insert id="insert" keyColumn="RowId" keyProperty="rowid" parameterType="com.LTSolution.ShopApp.Model.Customer" useGeneratedKeys="true"> insert into Customer (...) values (...) </insert>