springboot整合mybatis
目录结构学习
1.mapper/Dao层,用于操作数据库
接口是对于数据的增删改查,xml文件是对应的sql语句
2.service层,业务层接口是对于业务的描述,实现类是通过调用mapper层中的方法来实现
3.controller层是视图层是与页面交互,用来响应用户的请求
controller层:
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/student")
@ResponseBody
public Object student(Integer id){
Student student=studentService.queryStudentById(id);
return student;
}
}
@Autowired:给指定字段或方法注入所需的外部资源
注入service层调用所需的方法
@Controller创建控制器对象
@RequestMapping(value=””):将指定请求交给方法来处理
@ResponseBody:将返回值转化为json格式
service层接口:
public interface StudentService {
Student queryStudentById(Integer id);
}
service层实现类:
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student queryStudentById(Integer id) {
Student stu=studentMapper.selectByPrimaryKey(id);
return stu;
}
}
@Autowired注入mapper层方法
@Service将当前该类自动注入到spring容器中
Dao层:
@Mapper
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
@Mapper扫描Dao接口到Spring容器



