1、SpringBoot+Mybatis整合------简单CRUD的实现

编译工具:STS

    代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9e5c095ded585c90566

一、项目的建立

选择依赖:Mybatis,Web,MySql,JDBC

SpringBoot版本:2.0.3

图片描述

项目生成结构:

图片描述

pom依赖:

图片描述

图片描述

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>com.xm</groupId>
     <artifactId>demo005_Mybatis</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>jar</packaging>
 
     <name>demo005_Mybatis</name>
     <description>Demo project for Spring Boot</description>
 
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.0.2.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
 
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
     </properties>
 
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jdbc</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>org.mybatis.spring.boot</groupId>
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>1.3.2</version>
         </dependency>
 
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
 
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
 
 
 </project>

pom.xml

二、创建数据库

图片描述

三、项目配置

1.添加yml配置文件:application.yml

图片描述

图片描述

 #配置mybatis
 mybatis:
   #配置xml映射路径
   mapper-locations: classpath:mapper/*.xml
    #配置实体类的别名
   type-aliases-package: com.xm.pojo
   configuration:
     #开启驼峰命名法
     map-underscore-to-camel-case: true
     
 
 #配置mysql连接
 spring:
   datasource:
     url: jdbc:mysql://10.1.51.31:3306/xm
     username: root
     password: cube1501
     driver-class-name: com.mysql.jdbc.Driver

application.yml

2.在source文件夹下建立mapper文件夹

图片描述

3.在Springboot启动类添加@MapperScan注解

图片描述

图片描述

 package com.xm;
 
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 @MapperScan(value="com.xm.mapper")
 @SpringBootApplication
 public class Demo005MybatisApplication {
 
     public static void main(String[] args) {
         SpringApplication.run(Demo005MybatisApplication.class, args);
     }
 }

Demo005MybatisApplication.java注意:添加@MapperScan注解后,每个mapper都会自动扫描成为Bean。否则,需要在每个mapper接口上添加@Mapper接口

四、代码实现

1.实体类Student

图片描述

图片描述

 package com.xm.pojo;
 
 /**
  * name:学生实体
  * @author xxm
  *
  */
 public class Student {
     /**
      * content:主键id
      */
     private int id;
     /**
      * content:姓名
      */
     private String name;
     
     public int getId() {
         return id;
     }
     public void setId(int id) {
         this.id = id;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }
     
     
 
 }

Student.java

2.数据操作层StudentMapper

         

图片描述

图片描述

 package com.xm.mapper;
 
 import java.util.List;
 
 import com.xm.pojo.Student;
 
 public interface StudentMapper {
 
     /**
      * 根据id查询
      * @param id
      * @return
      */
     public Student getById(Integer id);
     
     /**
      * 查询全部
      * @return
      */
     public List<Student> list();
     
     /**
      * 插入
      * @param student
      */
     public void insert(Student student);
     
     /**
      * 根据student的id修改
      * @param student
      */
     public void update(Student student);
     
     /**
      * 根据id删除
      * @param id
      */
     public void delete(Integer id);
     
 }

StudentMapper.java

3.mapper映射Studentmapper

图片描述

图片描述

 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 <mapper namespace="com.xm.mapper.StudentMapper">
 
     <!-- 根据id查询 -->
     <select id="getById" parameterType="int" resultType="student">
     select * from student where id=#{id}
     </select>
     
     <!-- 查询所有 -->
     <select id="list" parameterType="int" resultType="student">
     select * from student
     </select>
     
     <!-- 插入一个学生 -->
     <insert id="insert" parameterType="student">
     insert into student(name) values(#{name})
     </insert>
     
     <!-- 根据id修改学生信息 -->
     <update id="update" parameterType="student">
     update student set name=#{name} where id=#{id}
     </update>
     
     <!-- 根据id删除学生 -->
     <delete id="delete" parameterType="int">
     delete  from student where id=#{id}
     </delete>
 </mapper>

StudentMapper.xml

4.控制层StudentController

        

图片描述

图片描述

 package com.xm.controller;
 
 import java.util.List;
 
 import javax.websocket.server.PathParam;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import com.xm.mapper.StudentMapper;
 import com.xm.pojo.Student;
 
 @RestController
 public class StudentController {
     @Autowired
     private StudentMapper studentMapper;
     
     /**
      * 根据id查询学生
      * @param id
      * @return
      */
     @GetMapping("/student/{id}")
     public Student getById(@PathVariable("id") Integer id) {
         
         Student student = studentMapper.getById(id);
         return student;
         
     }
     
     /**
      * 查询全部
      * @return
      */
     @GetMapping("/students")
     public List<Student> list(){
         List<Student> students = studentMapper.list();
         return students;
     }
     
     /**
      * 插入
      * @param student
      */
     @PostMapping("/student")
     public void insert( Student student) {
         studentMapper.insert(student);
     }
     
     /**
      * 修改
      * @param student
      */
     @PutMapping("/student/{id}")
     public void update(Student student,@PathVariable("id")Integer id) {
         studentMapper.update(student);
     }
     
     /**
      * 根据id删除
      * @param id
      */
     @DeleteMapping("/student/{id}")
     public void delete(@PathVariable("id") Integer id) {
         studentMapper.delete(id);
     }
 
 }

StudentController.java

2018-06-1517:31:14