Yoshi.dev

技術系の趣味、またはやった仕事やそこから学んだことを忘れないために

MyBatis

MyBatis generatorでインサート時のidを取得する

ちょっとタイトルが変だけどMyBatis generatorで作成したMapperクラスで、insert時にauto_incrementで付与されたidを取得できますgenerator_config.xml内で <table tableName="table_name"> <generatedKey column="table_name_id" sqlStatement="MySql" identity="true" /> </table> こんな感じにgeneratedKeyタグを追加するだけ。え、楽すぎてヤバイ