更新操作
插入
xml方式
<insert id="insertOne" parameterType="com.Student">
insert into students(stu_name,stu_sex) value (#{stuName},#{stuSex})
<selectKey resultType="Integer" keyProperty="stuId" order="AFTER">
<!-- order="AFTER" 标识在当前插入语句执行结束后再执行下面查询语句回填到实体对象中,因为通常主键是自动生成的-->
<!--resultType表示下面SQL执行后返回对象数据类型-->
<!--keyProperty表示回填对象中的属性-->
select last_insert_id() <!--该函数是MySQL中自带的函数,用于查询当前会话中最后一次插入的主键字段-->
</selectKey>
</insert>
用于测试关键代码片段,在创建sqlSession
对象时需要将自动事务提交关闭,并手动提交和回滚,其他代码省略
try {
sqlSession = sessionFactory.openSession(false); //创建sqlSession对象,并关闭自动事务提交
/*创建实体对象*/
Student student = new Student();
student.setStuName("张三");
student.setStuSex(false);
/*执行插入方法*/
int number = sqlSession.insert("student.insertOne", student); //返回影响条数
sqlSession.commit(); //手动事务提交
System.out.println(number); //查看影响条数
System.out.println(student); //查看数据是否回填
} catch (Exception e) {
if (sqlSession != null) {
sqlSession.rollback(); //出现异常手动回滚
}
e.printStackTrace();
} finally { //释放资源
if (sqlSession != null) {
sqlSession.close();
}
}
注解方式
package com;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.SelectKey;
public interface StudentMapper {
@Insert("insert into students(stu_name,stu_sex) value (#{stuName},#{stuSex})")
@SelectKey(
keyProperty = "stuId", //keyProperty表示回填对象中的属性
resultType = Integer.class, //表示下面SQL执行后返回对象数据类型,需要使用Class对象标识
before = false, //在当前插入语句执行结束后再执行下面查询语句回填到实体对象中,因为通常主键是自动生成的
statement = "select last_insert_id()" //该函数是MySQL中自带的函数,用于查询当前会话中最后一次插入的主键字段
)
int insertOne(Student student);
}
用于测试关键代码片段,在创建sqlSession
对象时需要将自动事务提交关闭,并手动提交和回滚,上面xml方式中已经展示过,其他代码省略
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
/*创建实体对象*/
Student student = new Student();
student.setStuName("张三");
student.setStuSex(false);
/*执行插入方法*/
int number = studentMapper.insertOne(student);
sqlSession.commit(); //手动事务提交
System.out.println(number); //查看影响条数
System.out.println(student); //查看数据是否回填
修改
xml方式
<update id="updateOne" parameterType="com.Student">
update students set stu_name = #{stuName},stu_sex = #{stuSex} where stu_id = #{stuId}
</update>
用于测试关键代码片段,在创建sqlSession
对象时需要将自动事务提交关闭,并手动提交和回滚,上面插入代码中已经展示过,其他代码省略
/*创建实体对象,一般会先查询再修改,为了示例所以手动创建*/
Student student = new Student();
student.setStuId(1);
student.setStuName("张三修改");
student.setStuSex(true);
/*执行修改方法*/
int number = sqlSession.update("student.updateOne", student); //返回影响条数
sqlSession.commit(); //手动事务提交
System.out.println(number); //查看影响条数
注解方式
package com;
import org.apache.ibatis.annotations.Update;
public interface StudentMapper {
@Update("update students set stu_name = #{stuName},stu_sex = #{stuSex} where stu_id = #{stuId}")
int updateOne(Student student);
}
用于测试关键代码片段,在创建sqlSession
对象时需要将自动事务提交关闭,并手动提交和回滚,上面插入代码中已经展示过,其他代码省略
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
/*创建实体对象,一般会先查询再修改,为了示例所以手动创建*/
Student student = new Student();
student.setStuId(1);
student.setStuName("张三修改");
student.setStuSex(true);
/*执行修改方法*/
int number = studentMapper.updateOne(student); //返回影响条数
sqlSession.commit(); //手动事务提交
System.out.println(number); //查看影响条数
删除
xml方式
<delete id="deleteOne" parameterType="Integer"> <!--一般删除只需要查询主键进行删除-->
delete from students where stu_id = #{value}
</delete>
用于测试关键代码片段,在创建sqlSession
对象时需要将自动事务提交关闭,并手动提交和回滚,上面插入代码中已经展示过,其他代码省略
/*执行删除方法*/
int number = sqlSession.update("student.deleteOne", 1); //返回影响条数
sqlSession.commit(); //手动事务提交
System.out.println(number); //查看影响条数
注解方式
package com;
import org.apache.ibatis.annotations.Delete;
public interface StudentMapper {
@Delete("delete from students where stu_id = #{value}")
int deleteOne(int stuId);
}
用于测试关键代码片段,在创建sqlSession
对象时需要将自动事务提交关闭,并手动提交和回滚,上面插入代码中已经展示过,其他代码省略
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
/*执行删除方法*/
int number = studentMapper.deleteOne(1); //返回影响条数
sqlSession.commit(); //手动事务提交
System.out.println(number); //查看影响条数
Comments NOTHING