12、在resources下log4j.properties配置文件
log4j.properties文件内容:(设置log的打印级别)
### 把日志信息输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
### 设置优先级别、以及输出源 ###
log4j.rootLogger=debug,stdout
13、编写业务代码
先给出结构目录,防止出现错误
13.1、controller下的内容:
ClassesController.java内容:
package com.aaa.ssm.controller;
import com.aaa.ssm.entity.Classes;
import com.aaa.ssm.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("classes")
public class ClassesController {
@Autowired
private ClassesService classesService;
@RequestMapping("findClasses")
public String findClasses(Model model) {
List<Classes> list = classesService.findClasses();
model.addAttribute("claList", list);
return "showClasses";
}
@RequestMapping("addClasses")
public String addClasses(Classes classes, Model model) {
classesService.addClasses(classes);
model.addAttribute("msg", "班级录入成功!");
return findClasses(model);
}
}
StudentController.java内容:
package com.aaa.ssm.controller;
import com.aaa.ssm.entity.Student;
import com.aaa.ssm.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("stu")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("findStu")
public String findStu(Model model) {
List<Student> list = studentService.findAllStudent();
model.addAttribute("list", list);
return "showStu";
}
@RequestMapping("addStu")
public String addStu(Student student, Model model) {
studentService.addStudent(student);
model.addAttribute("msg", "学生信息录入成功!");
return findStu(model);
}
}
13.2、 service下的内容:
ClassesService.java接口:
package com.aaa.ssm.service;
import com.aaa.ssm.entity.Classes;
import org.springframework.stereotype.Service;
import java.util.List;
public interface ClassesService {
public List<Classes> findClasses();
public void addClasses(Classes classes);
}
ClassesServiceImpl.java实现类:
package com.aaa.ssm.service.serviceImpl;
import com.aaa.ssm.dao.ClassesMapper;
import com.aaa.ssm.entity.Classes;
import com.aaa.ssm.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class ClassesServiceImpl implements ClassesService {
@Autowired
private ClassesMapper classesMapper;
@Override
@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
public void addClasses(Classes classes) {
classesMapper.insert(classes);
}
@Override
@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS)
public List<Classes> findClasses() {
return classesMapper.selectAll();
}
}
StudentService.java接口:
package com.aaa.ssm.service;
import com.aaa.ssm.entity.Student;
import org.springframework.stereotype.Service;
import java.util.List;
public interface StudentService {
public void addStudent(Student student);
public List<Student> findAllStudent();
}
StudentServiceImpl.java实现类:
package com.aaa.ssm.service.serviceImpl;
import com.aaa.ssm.dao.StudentMapper;
import com.aaa.ssm.entity.Student;
import com.aaa.ssm.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public void addStudent(Student student) {
studentMapper.insert(student);
}
@Override
public List<Student> findAllStudent() {
return studentMapper.selectAll();
}
}
13.3 dao下内容
ClassesMapper接口内容:
package com.aaa.ssm.dao;
import com.aaa.ssm.entity.Classes;
import java.util.List;
public interface ClassesMapper {
int deleteByPrimaryKey(Integer classId);
int insert(Classes record);
int insertSelective(Classes record);
Classes selectByPrimaryKey(Integer classId);
int updateByPrimaryKeySelective(Classes record);
int updateByPrimaryKey(Classes record);
List<Classes> selectAll();
}
StudentMapper接口内容:
package com.aaa.ssm.dao;
import com.aaa.ssm.entity.Student;
import java.util.List;
public interface StudentMapper {
int deleteByPrimaryKey(Integer stuId);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer stuId);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
List<Student> selectAll();
}
13.4、在resources.config.mybatis.mapper下编写selectAll查询语句(只给出截图)
ClassesMapper.xml里
StudentMapper.xml里