public class Student {
private int sno;
private String sname;
private String sex;
private String phone;
private String address;
private Date birthday;
private String email;
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", birthday=" + birthday +
", email='" + email + '\'' +
'}';
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
dao下的接口StudentDao
public interface StudentDao {
@Insert("insert into student(sno,sname,sex,phone,address,birthday,email) value(#{sno},#{sname},#{sex},#{phone},#{address},#{birthday},#{email})")
int add(Student student);
@Update("update student set sname=#{sname},address=#{address},birthday=#{birthday} where sno=#{sno}")
int up(Student student);
@Delete("delete from student where sno=#{sno}")
int del(int sno);
@Select("select * from student")
List<Student> findAll();
}
测试类TestStudent
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:spring.xml"} )
public class TestStudentDao {
@Autowired
StudentDao studentDao;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
@Test
public void testAdd() throws ParseException {
Student student = new Student();
student.setSex("男");
student.setPhone("123455655");
student.setEmail("123@qq.com");
student.setBirthday(format.parse("1999-9-9"));
student.setSname("张三");
student.setSno(40000);
student.setAddress(("河北廊坊"));
int i = studentDao.add(student);
if (i > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
}
@Test
public void testUpdate() throws ParseException {
Student student = new Student();
student.setBirthday(format.parse("1988-8-8"));
student.setSname("张三三");
student.setSno(40000);
student.setAddress(("河北廊坊西小区"));
int i = studentDao.up(student);
if (i > 0) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
}
@Test
public void testDel() throws ParseException {
int i = studentDao.del(40000);
if (i > 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
@Test
public void testFindAll() throws ParseException {
List<Student> all = studentDao.findAll();
for (Student s : all) {
System.out.println(s);
}
}
}
主要是对生日日期的表示方法,添加了新的代码段
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");