配置 application.yml4.0.0 com.example Untitled01 0.0.1-SNAPSHOT Untitled01 Untitled01 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter 3.5.1 mysql mysql-connector-java org.projectlombok lombok org.slf4j slf4j-api 1.7.21 org.slf4j slf4j-log4j12 1.7.21 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.example.Application repackage repackage
server:
port: 8086 # 启动端口
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver # 数据库驱动
username: root
password: 123456
url: jdbc:mysql://localhost:3306/sell?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
application:
name: Untitled01
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml #自动扫描mapper下的xml
global-config:
db-config:
table-prefix: t_ #表浅醉
用户表
CREATE TABLE IF NOT Exists tb_user
(
id INT(20) NOT NULL COMMENT '主键id',
user_name VARCHAr(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAr(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
replace into tb_user(id, user_name, age, email)
values (1, 'Jone', 18, 'test1@qq.com'),
(2, 'Jone', 22, 'test2@qq.com'),
(3, 'Jone', 23, 'test3@qq.com'),
(4, 'Jone', 12, 'test4@qq.com'),
(5, 'Jone', 34, 'test5@qq.com');
主启动类
Application.java
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
首先dao层接口
UserMapper.java
package com.example.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.bean.User; import java.util.List; public interface UserMapper extends BaseMapper然后Service层接口{ List findUser(); }
UserImpl.java
package com.example.impl;
import com.example.bean.User;
import java.util.List;
public interface UserImpl{
List findUser();
}
接着Service层实现
UserService.java
package com.example.service;
import com.example.bean.User;
import com.example.impl.UserImpl;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements UserImpl {
@Autowired
private UserMapper userMapper;
@Override
public List findUser() {
return userMapper.findUser();
}
}
最后Controller层实现
UserController.java
package com.example.controller;
import com.example.bean.User;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public List findUser(){
return userService.findUser();
}
}
另外还有
Usermapper.xml
完成了,访问接口 运行Application.java里面main方法



