01_6_修改实体对象

01_6_修改实体对象

1. 配置相应的映射文件

 <update id="updateStudentById" parameterClass="Student">

  update student set name=#name#, major=#major#, score=#score#, birth=#birth#

 where sid=#sid#  </update>

2. 实现类的方法

public void updateStudentById(Student student) {

try {

sqlMapClient.update("updateStudentById", student);

} catch (SQLException e) {

e.printStackTrace();

}

}

3. 调用测试

public static void main(String[] args) {

IStudentDAO dao = new IStudentDAOImpl();

for(Student stu : dao.queryAllStudent()) {

System.out.println(stu);

}

//更新sid3的数据

Student student = new Student();

student.setName("子浩");

student.setBirth(Date.valueOf("2018-01-25"));

student.setMajor("数学");

student.setScore(100);

student.setSid(3);

dao.updateStudentById(student);

System.out.println("###########################");

for(Student stu : dao.queryAllStudent()) {

System.out.println(stu);

}

}

[sid:1, name:李明, major:语文, birth:Tue Mar 27 00:00:00 CST 2018, score:100.0]

[sid:2, name:可可, major:英语, birth:Mon Mar 12 00:00:00 CST 2018, score:120.0]

[sid:3, name:天天, major:数学, birth:Thu Mar 22 00:00:00 CST 2018, score:80.0]

###########################

[sid:1, name:李明, major:语文, birth:Tue Mar 27 00:00:00 CST 2018, score:100.0]

[sid:2, name:可可, major:英语, birth:Mon Mar 12 00:00:00 CST 2018, score:120.0]

[sid:3, name:子浩, major:数学, birth:Thu Jan 25 00:00:00 CST 2018, score:100.0]

原文地址:https://www.cnblogs.com/flyback/p/8661553.html