一、示例截图
-
1.接口调用图
-
2.项目结构图
-
3.数据库截图
二、代码示例
- 1.pom.xml依赖
<?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">
<!-- 没有父工程则不需要,改为maven pom即可-->
<parent>
<artifactId>springboot-learning</artifactId>
<groupId>com.learning</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-mybatis</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- springboot启动starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis依赖包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- 数据库依赖包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
- 2.启动类
package com.learning.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Description 启动类
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 3.接口类
package com.learning.mybatis.controller;
import com.learning.mybatis.dao.StudentDao;
import com.learning.mybatis.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @Description 接口类
*/
@RequestMapping("student")
@RestController
public class StudentController {
@Autowired
private StudentDao studentDao;
@GetMapping("/{id}")
@ResponseBody
public Student getById(@PathVariable String id){
Student student = studentDao.getById(id);
return student;
}
}
- 4.持久层
package com.learning.mybatis.dao;
import com.learning.mybatis.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* 持久层
*/
@Mapper
public interface StudentDao {
@Select("select * from student where id = #{id}")
public Student getById(String id);
}
- 5.实体类
package com.learning.mybatis.entity;
import lombok.Data;
/**
* @Description 学生实体类
*/
@Data
public class Student {
private String id;
private String name;
private Integer age;
}
- 6.application配置类
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root
- 7.数据库sql脚本
CREATE TABLE `test`.`student` (
`id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'id',
`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '姓名',
`age` int(0) DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `test`.`student`(`id`, `name`, `age`) VALUES ('1', '张三', 20);