<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中去取。
MyBatis插入学生数据
本文介绍了一个使用MyBatis框架实现的学生数据插入操作。通过配置XML映射文件中的SQL语句来完成学生信息的数据库插入,并利用<selectKey>元素获取自动生成的主键ID。在Controller层调用Service层的方法完成数据保存。
617

被折叠的 条评论
为什么被折叠?



