?????????????????????????????????????????????????????????????????????????????????е?java?????????????????DAO?????????漰?????????????????????????????????????????????????п????????????????????????????????????????????????DAO??????????????????????????????????????????????????????duplicate key????????????????????????????е???????????try-catch-finally????????????????????????????????????????????????????spring?????????spring??????????????????????????DAO??????????????????????
??????????
?????????????Student??????????StudentService????в??????ò????????Mybatis?????????????????????£?
????Student?????:
public class Student {
private Integer id;
public Student(String name?? String sex?? Byte age?? String tel) {
this.name = name;
this.sex = sex;
this.age = age;
this.tel = tel;
}
public Student() {
}
private String name;
private String sex;
private Byte age;
private String tel;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public Byte getAge() {
return age;
}
public void setAge(Byte age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel == null ? null : tel.trim();
}
}
????StudentMapper????
public interface StudentMapper {
int insert(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKey(Student record);
}
????StudentService???????
public interface StudentService {
public Student getStudentsById(int StudentsId);
public int insertStudent(Student s);
public void updateStudent(Student s);
}
????StudentServiceImpl???????
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
public Student getStudentsById(int StudentsId) {
return studentMapper.selectByPrimaryKey(StudentsId);
}
public int insertStudent(Student s) {
return studentMapper.insert(s);
}
public void updateStudent(Student s) {
studentMapper.updateByPrimaryKey(s);
}
}
????????????
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
@Transactional
@Rollback(true)
public class StudentServiceTest {
@Autowired
private StudentService studentService;
@Test
public void testInsertStudent() {
Student s = new Student("test"?? "male"?? (byte) 23?? "110");
studentService.insertStudent(s);
Assert.assertEquals(studentService.getStudentsById(s.getId()).getName()??"test");
Assert.assertEquals(studentService.getStudentsById(s.getId()).getAge().intValue()?? 23);
}
@Test
public void testUpdateStudent() {
Student s = new Student("test"?? "male"?? (byte) 23?? "110");
studentService.insertStudent(s);
Assert.assertEquals(studentService.getStudentsById(s.getId()).getName()??"test");
Assert.assertEquals(studentService.getStudentsById(s.getId()).getAge().intValue()?? 23);
s.setAge((byte)25);
s.setName("test2");
studentService.updateStudent(s);
Assert.assertEquals(studentService.getStudentsById(s.getId()).getName()??"test2");
Assert.assertEquals(studentService.getStudentsById(s.getId()).getAge().intValue()?? 25);
}
}
????@Transactional????????????????????????????????????е????????????????????????????????в???????????????????????????κζ????????????
????@Rollback(true)??????????????????@Transactional???defaultRollback??????true????????????????
??????н?????????? ??