springboot-简单的学⽣选课系统(含mybatis部署和使⽤)-2
上⼀篇在这⾥
mapper的修改
现在mybatis已经帮我们⽣成了简单的model和操作数据库的mapper,那么,如果需要对数据库做其他操作呢?
之前我写过⼀篇复杂⼀点的:resource和autowired注解的区别
不过本⽂只写⼀个简单的例⼦,如下图的getCurId。(其他函数都是mybatis⾃动⽣成的)
在l中只要填写红圈中的就好了。其他都是mybatis⾃动⽣成的,不需要关⼼。
需要注意的是,如果返回的是Course对象,那么第⼀句(例如18⾏)应该写resultMap=“BaseResultMap”,就是第4~9⾏说明的、包含这些列的对象;如果返回的不是⼀个对象,⽐如是int,string,List< string>这种,要写resultType=“java.lang.Integer” 或者resultType=“java.lang.String”
service层
service层调⽤mapper的函数,实现种种功能。StudentService.java
package;
import List;
import Resource;
import Service;
import StudentDao;
import Student;
// 这个注解不要忘了
@Service("StudentService")
public class StudentService {
/
/ Resource⾃动组装Dao
@Resource
private StudentDao studentDao;
public int addStudent(String name){
Student student =new Student();
student.setName(name);
this.studentDao.insert(student);
return CurId();
}
}
CourseService.java
package;
import Resource;
import Service;
import CourseDao;
import Course;
@Service("CourseService")
public class CourseService {
@Resource
private CourseDao courseDao;
public int addCourse(String name,int weekday,int start){
Course course =new Course();
course.setName(name);
course.setWeekday(weekday);
course.setStart(start);
CurId();
}
}
EnrollService.java
package;
import ArrayList;
import List;
import Resource;
import Autowired; import Service;
import CourseDao;
import EnrollDao;
import StudentDao;
import Course;
import CourseForShow;
import Enroll;
import Enroll;
import Student;
@Service("EnrollService")
public class EnrollService {
@Resource
private EnrollDao enrollDao;
@Resource
private CourseDao courseDao;
// Autowired和Resource有⼀点区别,不过这⾥⽤哪个都⾏
@Autowired
private StudentDao studentDao;
public boolean enrollCourse(int sid,int cid){
try{
// check
List<Enroll> enrolled =llDao.selectBySid(sid);
if(enrolled.size()==0){
// student haven't enrolled any course
Enroll enroll =new Enroll();
enroll.setCid(cid);
enroll.setSid(sid);
return true;
}else{
List<String> times =new ArrayList<String>();
for(Enroll e : enrolled){
int c = e.getCid();
Course course =urseDao.selectByPrimaryKey(c);
String k = String.Weekday())+"-"+ String.Start());
// no spare time
ains(k)|| c == cid){
return false;
}else{
times.add(k);
continue;
}
}
/
/ can enroll
Enroll enroll =new Enroll();
enroll.setCid(cid);
enroll.setSid(sid);
return true;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public List<Student>queryStudentByCid(int cid){
List<Enroll> enrolled =llDao.selectByCid(cid);
List<Student> result =new ArrayList<Student>();
if(enrolled.size()!=0){
for(Enroll e : enrolled){
Student s =this.studentDao.Sid());
if(s !=null){
result.add(s);
}
}
}
return result;
}
public List<CourseForShow>queryCourseBySid(int sid){
List<Enroll> enrolled =llDao.selectBySid(sid);
List<CourseForShow> result =new ArrayList<CourseForShow>();
if(enrolled.size()!=0){
for(Enroll e : enrolled){
Course c =urseDao.Cid());
if(c !=null){
CourseForShow cfs =new CourseForShow();
cfs.Id());
cfs.Name());
cfs.Start());
cfs.Weekday());
result.add(cfs);
}
}
}
return result;
}
public List<CourseForShow>querySchedule(int sid,int weekday){
List<Enroll> enrolled =llDao.selectBySid(sid);
List<CourseForShow> result =new ArrayList<CourseForShow>();
if(enrolled.size()!=0){
for(Enroll e : enrolled){
Course c =urseDao.Cid());
if(c !=null&& c.getWeekday()== weekday){
CourseForShow cfs =new CourseForShow();
cfs.Id());
cfs.Name());
cfs.Start());
cfs.Weekday());
result.add(cfs);
}
}
}
return result;
}
}
controller层
controller层包含启动类和响应页⾯请求的函数,这些函数通过调⽤service层的函数,实现计算,然后组装好结果返回到页⾯。DemoApplication.java 启动类:@MapperScan最好写上,不然会报不到
package;
import MapperScan;
import SpringApplication;
import SpringBootApplication;
import DataSourceAutoConfiguration;
@SpringBootApplication()
@MapperScan(basePackages="ample.demo.mapper")
public class DemoApplication {
public static void main(String[] args){
SpringApplication.run(DemoApplication.class, args);
}
}
MainController.java
package;
import List;
import Resource;
import Controller;
import GetMapping;
import PostMapping;
import RequestMapping;
import RequestParam;
import RedirectAttributes;
import Course;
import CourseForShow;
import Student;
import CourseService;
import EnrollService;
import StudentService;
@Controller
public class MainController {
@Resource
private StudentService studentService;
@Resource
private CourseService courseService;
@Resource
private EnrollService enrollService;
// 可以理解成前往templates下的index.html
@RequestMapping("/")
public String toHome(){
return"index";
}
@RequestMapping("/result")
public String toResult(){
return"result";
}
@PostMapping("/addStudent")
public String toAddStudentResult(RedirectAttributes redirectAttributes,@RequestParam("studentName")String studentName){ //System.out.println("*" + studentName + "*");
// add student ok
int sid =this.studentService.addStudent(studentName);
redirectAttributes.addFlashAttribute("msg", studentName +" has been added successfully! Student id: "+ String.valueOf(sid)); return"redirect:/result";
}
@PostMapping("/addCourse")
public String toAddCourseResult(RedirectAttributes redirectAttributes,@RequestParam("courseName")String courseName,
@RequestParam("weekday")String weekday,@RequestParam("start")String start){
int cid =urseService.addCourse(courseName, Integer.parseInt(weekday), Integer.parseInt(start));
redirectAttributes.addFlashAttribute("msg", courseName +" has been added successfully! Course id: "+ String.valueOf(cid)); return"redirect:/result";
}
@PostMapping("/enroll")
public String toEnrollResult(RedirectAttributes redirectAttributes,@RequestParam("studentId")String studentId,
@RequestParam("courseId")String courseId){
boolean flag =llCourse(Integer.parseInt(studentId), Integer.parseInt(courseId));
if(flag){
redirectAttributes.addFlashAttribute("msg","Enroll successfully!");
}else{
redirectAttributes.addFlashAttribute("msg","Student has no spare time!");
}
return"redirect:/result";

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。