MyBatis入门第二课insert update delete

1.insert

id="insertStudent" parameterType="com.yanfang.PO.Student" useGeneratedKeys="true" keyProperty="id">
    insert into student(s_id,s_name,s_birth,s_sex) values(#{sId},#{sName},#{sBirth},#{sSex})

加入

useGeneratedKeys="true" keyProperty="id"

插入对象时,对象自动增长的id会自动装配到对象的id中

也可用

id="insertStudent" parameterType="com.yanfang.PO.Student" >
    keyProperty="id" keyColumn="id" order="AFTER" resultType="java.lang.Integer">
        select LAST_INSERT_ID()
    
    insert into student(s_id,s_name,s_birth,s_sex) values(#{sId},#{sName},#{sBirth},#{sSex})

2.update

id="updateNameById" parameterType="com.yanfang.PO.Student">
    update student set s_name=#{sName} where id=#{id}
openSession.update("com.yanfang.Dao.updateNameById",student);

3.delete

id="deleteStudentById" parameterType="com.yanfang.PO.Student">
    delete from student where id=#{id}


Student student = new Student();
student.setId(10);
openSession.delete("com.yanfang.Dao.deleteStudentById",student);


你可能感兴趣的:(MyBatis入门第二课insert update delete)