4.0.0
org.example
testcsdn
1.0-SNAPSHOT
8
8
UTF-8
org.springframework.boot
spring-boot-starter-parent
2.2.10.RELEASE
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
true
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
mysql
mysql-connector-java
8.0.30
package org.example.controller;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //@RestController = @Controller + @ResponseBody 此处目的是返回json数据
@RequestMapping("/test")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/show")
public String test01(){
return studentService.findStudent();
}
}
package org.example.mapper;
import org.example.pojo.Student;
public interface StudentMapper {
Student findStudent();
}
springboot+mybatis实体类不需要加@Table等注解,只要有@Data注解即可(包含getter/setter方法)
package org.example.pojo;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
}
接口
package org.example.service;
public interface StudentService {
String findStudent();
}
实现类
package org.example.service;
import org.example.mapper.StudentMapper;
import org.example.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImp implements StudentService{
@Autowired
private StudentMapper studentMapper;
@Override
public String findStudent() {
Student student = studentMapper.findStudent();
return student.toString();
}
}
package org.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("org.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
server:
port: 8080 #服务端口号,可修改
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #mysql5版本把.cj去掉,此处为mysql8.0
url: jdbc:mysql://localhost:3306/test #test改成自己的数据库名
username: root
password: "123456"
mybatis:
mapper-locations: classpath:mybatis/*.xml
#目的是为了省略resultType里的代码量,可不加
# type-aliases-package: com.example.pojo
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
package org.example.test;
import org.example.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class StudentTest {
@Autowired
private StudentService studentService;
@Test
public void test(){
String student = studentService.findStudent();
System.out.println(student);
}
}
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 80033 (8.0.33)
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 80033 (8.0.33)
File Encoding : 65001
Date: 19/12/2023 10:14:04
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`age` int NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '李华', 19);
INSERT INTO `student` VALUES (2, '张飞', 20);
INSERT INTO `student` VALUES (3, '丑陋', 22);
INSERT INTO `student` VALUES (4, '孙尚香', 18);
INSERT INTO `student` VALUES (5, '狄仁杰', 12);
SET FOREIGN_KEY_CHECKS = 1;
右键启动类启动服务
在网页打开网址
http://localhost:8080/test/show
显示下图,即运行成功!
到这里你的springboot+mybatis项目就算是入门了,如果伙伴们有什么问题的话,评论留言,大家一起学习,一起进步!
环境:IDEA开发工具、数据库Mysql8.0、Springboot+Mybatis项目