实体类entity下的表course
public class Course {
private int cno;
private String cname;
private int classHour;
public int getCno() {
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
@Override
public String toString() {
return "Course{" +
"cno=" + cno +
", cname='" + cname + '\'' +
", classHour=" + classHour +
'}';
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getClassHour() {
return classHour;
}
public void setClassHour(int classHour) {
this.classHour = classHour;
}
}
对课程表下学号(cno)、姓名(cname)、课时(classHour) 进行定义。
dao下的接口CoursDao
public interface CourseDao {
@Insert("insert into course(cname,classhour) value (#{cname},#{classHour})")
int add(Course course);
@Update("update course set cname=#{cname},classhour=#{classHour} where cno=#{cno}")
int up(Course course);
@Delete("delete from course where cno=#{cno}")
int del(int cno);
@Select("select * from course")
List<Course>findAll();
}
测试类TestCourseDao
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:spring.xml"} )
public class TestCouseDao {
@Autowired
CourseDao courseDao;
@Test
public void testAdd(){
Course course=new Course();
course.setCname("jsp");
course.setClassHour(64);
int i=courseDao.add(course);
if(i>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
@Test
public void testUp(){
Course course=new Course();
course.setCname("css");
course.setClassHour(48);
course.setCno(3);
int i=courseDao.up(course);
if(i>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
}
@Test
public void testDel(){
int i=courseDao.del(3);
if(i>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
@Test
public void findAll(){
List<Course> all=courseDao.findAll();
for(Course c:all){
System.out.println(c);
}
}
}