01_4_插入一个实体对象
01_4_插入一个实体对象
1. 配置映射配置
<insert id="insertStudent" parameterClass="Student">
insert into Student(sid, name, major, birth, score) values(#sid#, #name#, #major#, #birth#, #score#)
</insert>
2. 实现方法类
public void addStudent(Student student) {
try {
sqlMapClient.insert("insertStudent", student);
} catch (SQLException e) {
e.printStackTrace();
}
}
3. 调试及其结果
public static void main(String[] args) {
IStudentDAO dao = new IStudentDAOImpl();
Student student = new Student();
student.setSid(6);
student.setName("fengge");
student.setScore(100);
dao.addStudent(student);
for(Student stu : dao.queryAllStudent()) {
System.out.println(stu);
}
}
[sid:6, name:fengge, major:null, birth:null, score:100.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 Mar 22 00:00:00 CST 2018, score:80.0]