SpringBoot实现学生在线交作业(二)—— 整合mybatis
1.首先设计数据库,这个项目数据库设计非常简单。
在application.properties中配置:
spring.datasource.url=jdbc:mysql://xxxxxxxx:3306/rjclass?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username =xxxxxx
spring.datasource.password =xxxxxx
2.编写po学生类
四个变量,与数据库一一对应:
private String name;
private String number;
private String course;
private String times;
编写set,get,tostring方法
重写一下equals类,用于判断是否数据重复
代码:
public class Student {
private String name;
private String number;
private String course;
private String times;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getTimes() {
return times;
}
public void setTimes(String times) {
this.times = times;
}
@Override
public String toString() {
return "Student [name=" + name + ", number=" + number + ", course=" + course + ", times=" + times + "]";
}
public boolean equals(Student stu) {
if (stu.getCourse().equals(course) && stu.getName().equals(name) && stu.getNumber().equals(number)
&& stu.getTimes().equals(times))
return true;
return false;
}
}
3.编写mapper接口
该接口让student类中的变量与数据库中的成员实现了映射,这里我写了三个方法,
分别是获得数据库中的所有学生作业信息——List<Student> getStudents();
插入学生作业信息——void insert(Student student);
更新学生作业信息——void updata(Student student);
代码如下:
@Mapper
public interface CourseMapper {
@Select("SELECT * FROM course")
List<Student> getStudents();
@Insert("INSERT INTO course(name,number,course,times) VALUES (#{name},#{number},#{course},#{times})")
void insert(Student student);
@Update("UPDATE course SET times=#{times} WHERE name =#{name}")
void updata(Student student);
}
4.编写service层,从mapper层获取到数据库中的数据并进行加工处理,或是将数据通过mapper层写入到数据库。
首先是获取数据库信息的类
(1)GetService,该类中有两个方法,分别是返回所有数据的学生类集合——public List<Student> getAll();
然后是输出所有信息的方法——public void inputInfo();
代码:
@Service
public class GetService {
@Autowired
CourseMapper courseMapper;
public List<Student> getAll() {
return courseMapper.getStudents();
}
public void inputInfo() {
List<Student> list = new ArrayList<Student>();
list = courseMapper.getStudents();
for (Student student : list) {
System.out.println(list.toString());
}
}
}
(2)SubmitService,该类用于提交数据到数据库中,在提交的时候进行判断,若提交的学号,姓名,实验课以及试验次数一样,则更新作业内容,不一样则添加此次数据。
代码:
@Service
public class SubmitService {
@Autowired
CourseMapper courseMapper;
private boolean key = true;
public void submit(Student student) {
List<Student> temp = new ArrayList<Student>();
temp = courseMapper.getStudents();
for (Student student2 : temp) {
if (student2.equals(student)) {
courseMapper.updata(student);
System.out.println("已更新数据");
key = false;
break;
}
}
if (key) {
courseMapper.insert(student);
System.out.println("已添加数据");
}
}
}
5.编写测试类,使用springboot的测i是类来测试刚刚的方法
代码:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ClassDesignApplicationTests {
@Autowired
private CourseMapper courseMapper;
@Autowired
GetService getService = new GetService();
@Autowired
SubmitService submitService = new SubmitService();
/*
* 通过service层获取到数据库的所有信息并打印在屏幕上
*/
@Test
public void test01() {
List<Student> arrayList = new ArrayList<Student>();
arrayList = getService.getAll();
for (Student student : arrayList) {
System.out.println(arrayList.toString());
}
System.out.println("test22测试成功");
}
/*
* 提交数据
*/
@Test
public void test02() {
Student student = new Student();
student.setCourse("C语言");
student.setName("ywh");
student.setNumber("111");
student.setTimes("2");
submitService.submit(student);
}
}
注:service层中使用了mapper,所以service层的对象也需要@Autowired才能完成注入。