Spring boot5之整合mybatis

Spring boot整合mybatis

  • 配置pom.xml

    1 <?xml version="1.0" encoding="UTF-8"?>

    2 <project xmlns="http://maven.apache.org/POM/4.0.0"

    3                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    4                   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    5         <modelVersion>4.0.0</modelVersion>

    6 

    7         <groupId>com.wiggin</groupId>

    8         <artifactId>springbootmybatis</artifactId>

    9         <version>1.0-SNAPSHOT</version>

   10 

   11         <parent>

   12                 <groupId>org.springframework.boot</groupId>

   13                 <artifactId>spring-boot-parent</artifactId>

   14                 <version>2.1.5.RELEASE</version>

   15         </parent>

   16 

   17         <dependencies>

   18 

   19                 <dependency>

   20                         <groupId>org.springframework.boot</groupId>

   21                         <artifactId>spring-boot-starter-web</artifactId>

   22                 </dependency>

   23                 <dependency>

   24                         <groupId>org.mybatis.spring.boot</groupId>

   25                         <artifactId>mybatis-spring-boot-starter</artifactId>

   26                         <version>1.3.1</version>

   27                 </dependency>

   28                 <dependency>

   29                         <groupId>mysql</groupId>

   30                         <artifactId>mysql-connector-java</artifactId>

   31                 </dependency>

   32                 <dependency>

   33                         <groupId>org.projectlombok</groupId>

   34                         <artifactId>lombok</artifactId>

   35                 </dependency>

   36 

   37 

   38         </dependencies>

   39 

   40 </project>

  • 创建实体类Student

    1 package com.wiggin.entity;

    2 

    3 import lombok.Data;

    4 

    5 import java.util.Date;

    6 

    7 // 创建表不用关联数据库

    8 @Data

    9 public class Student {

   10         private Long id;

   11         private String name;

   12         private int score;

   13         private Date birthday;

   14 }

  • 创建接口StudentRepository

    1 package com.wiggin.repository;

    2 

    3 import com.wiggin.entity.Student;

    4 

    5 import java.util.List;

    6 

    7 public interface StudentReposity {

    8         public List<Student> findAll();

    9         public Student findById(Long id);

   10         public void save(Student student);

   11         public void update(Student student);

   12         public void deleteById(Long id);

   13 }

  • 在recourses中的mapping文件创建StudentRepository接口对应的Mapper.xml

    1 <?xml version="1.0" encoding="UTF-8"?>

    2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    3 

    4 <mapper namespace="com.wiggin.repository.StudentRepository">

    5         <select id="findAll" resultType="Student">

    6                 select * from student

    7         </select>

    8         <select id="findById" parameterType="java.lang.Long" resultType="Student">

    9                 select * from student where id = #{id}

   10         </select>

   11         <insert id="save" parameterType="Student">

   12                 insert into student(name,score,birthday) values (#{name},#{score},#{birthday})

   13         </insert>

   14         <update id="update" parameterType="Student">

   15                 update student set name = #{name},score = #{score},birthday = #{birthday} where id = #{id}

   16         </update>

   17         <delete id="deleteById" parameterType="java.lang.Long">

   18                 delete student where id = #{id}

   19         </delete>

   20 </mapper>

  • 创建StudentHandler,注入StudentRepository

    1 package com.wiggin.controller;

    2 

    3 import com.wiggin.entity.Student;

    4 import com.wiggin.repository.StudentRepository;

    5 import org.springframework.beans.factory.annotation.Autowired;

    6 import org.springframework.stereotype.Controller;

    7 import org.springframework.web.bind.annotation.*;

    8 

    9 import java.util.List;

   10 

   11 

   12 @RestController

   13 @RequestMapping("/student")

   14 public class StudentHandler {

   15         @Autowired

   16         private StudentRepository studentRepository;

   17 

   18         @GetMapping("/findAll")

   19         public List<Student> findAll(){

   20                 return studentRepository.findAll();

   21         }

   22         @GetMapping("/findById/{id}")

   23         public Student findById(@PathVariable("id") Long id){

   24                 return studentRepository.findById(id);

   25         }

   26         @PostMapping("/save")

   27         public void save(@RequestBody Student student){

   28                 studentRepository.save(student);

   29         }

   30         @PutMapping("/update")

   31         public void update(@RequestBody Student student){

   32                 studentRepository.update(student);

   33         }

   34         @DeleteMapping("/deleteById/{id}")

   35         public void deleteById(@PathVariable("id") Long id){

   36                 studentRepository.deleteById(id);

   37         }

   38 }

  • 创建application.yml

    1 spring:

    2     datasource:

    3         url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

    4         username: root

    5         password: 123456

    6         driver-class-name: com.mysql.cj.jdbc.Driver

    7 mybatis:

    8     # classpath代表resources文件,*.xml扫描所有的mapping.xml文件

    9     mapper-locations: classpath:/mapping/*.xml

   10     # 指定实体类的包名,在mapping中不用写包名

   11     

  • 创建Application启动类

    1 package com.wiggin;

    2 

    3 import org.mybatis.spring.annotation.MapperScan;

    4 import org.springframework.boot.SpringApplication;

    5 import org.springframework.boot.autoconfigure.SpringBootApplication;

    6 

    7 @SpringBootApplication

    8 // 将包扫入ioc中,这样才能取出

    9 @MapperScan("com.wiggin.repository")

   10 public class Application {

   11         public static void main(String[] args) {

   12                 SpringApplication.run(Application.class,args);

   13         }

   14 }

 

posted @ 2020-08-13 22:44  wigginess  阅读(109)  评论(0编辑  收藏  举报