栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合mybatis

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容器

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/727986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号