你可以通过Spring Initializr添加MyBatis的Starer。
    JavaBean:

    1. package com.wzy.springbootmybatis.pojo;
    2. @Data //Getter、Setter方法
    3. @NoArgsConstructor//无参构造器
    4. @AllArgsConstructor//全参构造器
    5. public class Person {
    6. private Integer id;
    7. private String username;
    8. private String gender;
    9. private Integer age;
    10. private String phone;
    11. }

    Dao接口:在接口使用@Mapper注释。或者在启动类上使用@MapperScan(“com.wzy.springbootmybatis.dao”)扫描接口。

    1. package com.wzy.springbootmybatis.dao;
    2. @Mapper
    3. public interface PersonDao {
    4. public Person getPesonOne(Integer id);
    5. //注解版的查询
    6. @Select("select id,username,gender,age,phone from person where id=#{id}")
    7. public Person getPesonOne2(Integer id);
    8. //sql文件配置版的插入
    9. public int insertPerson(Person person);
    10. //注解版的插入,不需要在sql配置文件内写sql语句。
    11. //@Options是 @Insert胡配置注解.useGeneratedKeys="true":使用自增主键,keyProperty="id":自增主键在javaBean中的变量名
    12. //
    13. @Insert("insert into person(username,gender,age,phone) values (#{username},#{gender},#{age},#{phone})")
    14. @Options(useGeneratedKeys = true,keyProperty = "id")//可以获取插入的id
    15. public int insertPerson2(Person person);
    16. }

    Service层:

    1. package com.wzy.springbootmybatis.service;
    2. @Service
    3. public class PersonServlce {
    4. @Autowired
    5. PersonDao personDao;
    6. public Person getSelectPersonOne(Integer id){
    7. return personDao.getPesonOne(id);
    8. }
    9. public Person getSelectPersonOne2(Integer id){
    10. return personDao.getPesonOne2(id);
    11. }
    12. public int insertPersonOne(Person person){
    13. return personDao.insertPerson(person);
    14. }
    15. public int insertPersonOne2(Person person){
    16. return personDao.insertPerson2(person);
    17. }
    18. }


    controller:

    1. package com.wzy.springbootmybatis.controller;
    2. @Controller
    3. public class PerosnController {
    4. @Autowired
    5. PersonServlce personServlce;
    6. @ResponseBody
    7. @RequestMapping(value = "/person",method = RequestMethod.GET)
    8. public Person getPerson(@RequestParam("id") Integer id){
    9. return personServlce.getSelectPersonOne(id);
    10. }
    11. @ResponseBody
    12. @RequestMapping(value = "/person2",method = RequestMethod.GET)
    13. public Person getPerson2(@RequestParam("id") Integer id){
    14. return personServlce.getSelectPersonOne2(id);
    15. }
    16. @ResponseBody
    17. @RequestMapping(value = "/insertperson",method = RequestMethod.POST)
    18. public Person insertPerson1(Person person){//自动封装
    19. int i = personServlce.insertPersonOne(person);//插入数据
    20. return person;//返回插入的数据
    21. }
    22. @ResponseBody
    23. @RequestMapping(value = "/insertperson2",method = RequestMethod.POST)
    24. public Person insertPerson2(Person person){//自动封装
    25. int i = personServlce.insertPersonOne2(person);//插入数据
    26. return person;//返回插入的数据
    27. }
    28. }

    启动类:使用@MapperScan("com.lun.boot.mapper") 简化,Mapper接口就可以不用标注@Mapper注解。

    package com.wzy.springbootmybatis;
    
    //@MapperScan("com.wzy.springbootmybatis.dao")
    @SpringBootApplication
    public class SpringbootMybatisApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootMybatisApplication.class, args);
        }
    }