ssm框架,往数据库插入一条数据,返回插入数据的id

时间:2021-04-04 21:43:23


<insert id="preserveStudent" parameterType="com.model.Student" useGeneratedKeys="true">

insert into Student
(name,phone,grade,teacher,hobby,sex) 
values
(#{name},#{phone},#{grade},#{teacher},#{hobby},#{sex})
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID() AS id
    </selectKey>
</insert>


controller写法:
studentService.preserveStudent(student);//执行service方法
int id=student.getId();//返回的id是在pojo中去取。