Home > Back-end >  MYSQL insert a data and returns on the id
MYSQL insert a data and returns on the id

Time:11-27

mybits in the writing of mapper
Written as follows: (dry)

Close test can use


method one:
The SELECT LAST_INSERT_ID ()

 

Insert into ` edo_trip ` (Title, LineTypeId, LineType, ClassTypeId ClassType, RouteId, SeatCount, Price, StartDate, CarNo, Driver, DriverHelper, DriverHelperMobile,
Data1, Data2, Data3, respectively, Data4, Data5, CDate) values
(# # {Title}, {LineTypeId}, # {LineType}, # {ClassTypeId}, # {ClassType}, # {RouteId}, # {SeatCount}, # {Price}, # {StartDate}, # {CarNo}, # {the Driver}, # {DriverHelper}, # {DriverHelperMobile},
# # {Data1}, {Data2}, # {Data3}, # {Data4}, # # {Data5}, {CDate})


The SELECT LAST_INSERT_ID () AS ID







Note: 1, the label is written inside the
2. Don't forget to write down the order attribute
3. The dao layer don't receive, my dao writing is as follows:
Void insertBug (Map Params);
4. The business layer receives the returned id parameter:
The Integer id=(Integer) params. Get (" id ");
Is, returns the id of the params in the request parameters, so that you can,
5. SELECT LAST_INSERT_ID () AS the use of the id can also use other, such AS:
@ @ identity

SelectKey will SELECT LAST_INSERT_ID () the results into the incoming parameters (the parameterType) inside the primary key:
(1) the primary key of the corresponding model keyProperty property names, here is the id of the user, because it goes with the primary key of the corresponding database
(2) the order="AFTER" said SELECT LAST_INSERT_ID () in the insert executed AFTER, multi-purpose and the primary key,
Said BEFORE the SELECT LAST_INSERT_ID () in the insert perform BEFORE, so we won't be able to get a primary key,
This is suitable for the type of the primary key is not on the
ResultType primary key type


method 2:
# insert


 

KeyProperty="ID" parameterType="com. Hsedo. Onepiece. Pojo. Edo_Tips" & gt;

Insert into ` edo_trip ` (Title, LineTypeId, LineType, ClassTypeId ClassType, RouteId, SeatCount, Price, StartDate, CarNo, Driver, DriverHelper, DriverHelperMobile,
Data1, Data2, Data3, respectively, Data4, Data5, CDate) values
(# # {Title}, {LineTypeId}, # {LineType}, # {ClassTypeId}, # {ClassType}, # {RouteId}, # {SeatCount}, # {Price}, # {StartDate}, # {CarNo}, # {the Driver}, # {DriverHelper}, # {DriverHelperMobile},
# # {Data1}, {Data2}, # {Data3}, # {Data4}, # # {Data5}, {CDate})







# # key:
KeyColumn corresponding user_id is the database fields, keyProperty userId corresponding entity class SysUserEntity fields, can be the same, can also be different, here he will automatically generated user_id in entity classes, Java to get inside,
This method can also be, he will automatically generate the id into SysUserEntity inside the corresponding id



note:
The two methods both choose, do not use

CodePudding user response:

Url reference others forget
  • Related