目录结构

image.png

application.yml文件

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.mybatis.spring.boot</groupId>
  8. <artifactId>mybatis-spring-boot-starter</artifactId>
  9. <version>2.1.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-test</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>mysql</groupId>
  17. <artifactId>mysql-connector-java</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.projectlombok</groupId>
  21. <artifactId>lombok</artifactId>
  22. </dependency>
  23. <!-- mock数据 -->
  24. <dependency>
  25. <groupId>com.github.javafaker</groupId>
  26. <artifactId>javafaker</artifactId>
  27. <version>0.17.2</version>
  28. </dependency>
  29. </dependencies>

xml文件

  1. <!DOCTYPE mapper
  2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.my.learn.mybatis2.mapper.StudentMapper">
  5. <resultMap id="student" type="com.my.learn.mybatis2.domain.Student">
  6. <id column="id" property="id"/>
  7. <result column="name" property="name"/>
  8. </resultMap>
  9. <select id="getById" resultMap="student">
  10. select id,`name` from student where id = #{id}
  11. </select>
  12. <select id="getAll" resultMap="student">
  13. select id,`name` from student
  14. </select>
  15. <insert id="add" parameterType="com.my.learn.mybatis2.domain.Student" useGeneratedKeys="true" keyProperty="id">
  16. insert into student (`name`) value (#{name})
  17. </insert>
  18. <update id="update" parameterType="com.my.learn.mybatis2.domain.Student">
  19. update student set `name`= #{name} where id = #{id}
  20. </update>
  21. <delete id="deleteById" parameterType="Integer">
  22. delete from student where id = #{id}
  23. </delete>
  24. </mapper>

Java文件

  1. //主类
  2. @SpringBootApplication
  3. @MapperScan("com.my.learn.mybatis2.mapper")
  4. public class MybatisSpringDemoApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MybatisSpringDemoApplication.class, args);
  7. }
  8. }
  9. //controller
  10. @RestController
  11. @RequestMapping("/stu")
  12. public class StudentController {
  13. @Autowired
  14. private StudentMapper mapper;
  15. @GetMapping
  16. public List<Student> getAll() {
  17. List<Student> all = mapper.getAll();
  18. return all;
  19. }
  20. @GetMapping("/{id}")
  21. public Student getById(@PathVariable("id") Integer id) {
  22. Student student = mapper.getById(id);
  23. return student;
  24. }
  25. @PostMapping
  26. public Integer add(Student student) {
  27. mapper.add(student);
  28. return student.getId();
  29. }
  30. @PutMapping
  31. public Student update(Student student) {
  32. mapper.update(student);
  33. return mapper.getById(student.getId());
  34. }
  35. @DeleteMapping("/{id}")
  36. public Object deleteById(@PathVariable("id") Integer id) {
  37. mapper.deleteById(id);
  38. return "";
  39. }
  40. }
  41. //实体类
  42. @Data
  43. @NoArgsConstructor
  44. @AllArgsConstructor
  45. public class Student {
  46. private Integer id;
  47. private String name;
  48. }
  49. //mapper
  50. public interface StudentMapper {
  51. Student getById(Integer id);
  52. List<Student> getAll();
  53. void add(Student student);
  54. void update(Student student);
  55. void deleteById(Integer id);
  56. }