查询,在上两篇文章中已经讲完,本章主要是进行增删改。
StudentDao.java 定义方法
public interface StudentDao {
int add(Student stu);
int update(Student stu);
int delete(int id);
}
StudentDao.xml中使用MyBatis提供的一套语义化标签:、、
<insert id="add">
insert into student (name,gender,age) values(#{name},#{gender},#{age})
</insert>
<update id="update">
update student set name=#{name},gender=#{gender},age=#{age} where id=#{id}
</update>
<delete id="delete">
delete from student where id=#{id}
</delete>
测试时候记住要显示提交:
Student stu = new Student();
stu.setId(7);
stu.setName("小华");
stu.setGender("女");
stu.setAge(20);
int rs=studentDao.delete(7);
System.out.println(rs);
session.commit();
session.close();
|