欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第4讲 --插入一个实体对象

程序员文章站 2022-03-03 16:09:00
...

步骤:

1.增加 Student.xml中 对插入单个对象的支持 ,注意标签是 insert

 

 
 <insert id="insertStudent"  parameterClass="Student">
    insert into student values (#sid#,#sname#,#major#,#birth#,#score#) 
  </insert>

2.在实现类中

public void add(Student student)
	{
		
		try {
			sqlMapClient.insert("insertStudent",student);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
 

 3.测试

StudentDAO studentDAO=new StudentDAOImpl();
		Student student=new Student();
		student.setSid(123);
		student.setSname("胡晓亮");
		student.setMajor("哈哈");
		student.setScore(98);
		student.setBirth(new Date(System.currentTimeMillis()));
		studentDAO.add(student);

 

 4.结果
 
 
mysql> select * from student;
+-----+--------+-------+------------+-------+
| sid | sname  | major | birth      | score |
+-----+--------+-------+------------+-------+
|   1 | 1      | 1     | 1985-12-11 |     1 |
|   2 | 2      | 2     | 1985-12-10 |     2 |
| 324 | 你好   | 1     | 1985-12-14 |    56 |
| 123 | 胡晓亮 | 哈哈  | 2009-06-16 |    98 |
+-----+--------+-------+------------+-------+
4 rows in set (0.06 sec)
 
相关标签: MySQL XML