版本
- springboot 2.7.16
- druid 1.1.22
- mybatis-plus-boot-starter 3.4.2
- mysql 5.7.38
当然别的版本应该也差不多,最多改改相关类
步骤
1、搭建一个简单的springboot项目,版本选择2.7.16
参考从官网开始用IDEA在JDK1.8环境搭建springboot2.7.14项目
2、引入druid,mybatis-plus的相关依赖
<!--数据库配置-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<!--自动化配置-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
<!--mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
3、配置文件引入数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource #Druid类型
4、建库建表插入数据
CREATE DATABASE demo;
CREATE TABLE user(
userid BIGINT not null primary key COMMENT '主键ID,雪花算法生成',
username VARCHAR(64) COMMENT '用户名称:这里用微信的昵称'
);
INSERT INTO `demo`.`user` (`userid`, `username`) VALUES (1, '小林');
INSERT INTO `demo`.`user` (`userid`, `username`) VALUES (2, '小马');
5、建User.class
package com.example.demo;
public class User {
private String userid;
private String username;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"userid='" + userid + '\'' +
", username='" + username + '\'' +
'}';
}
}
6、建UserMapper.class
package com.example.demo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
7、在DemoController加上测试逻辑
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class DemoController {
@Autowired
UserMapper userMapper;
@GetMapping("/find")
public List<User> Test1(){
return userMapper.selectList(null);
}
@GetMapping("/hello")
public Map<String,Object> hello(){
Map<String,Object> result = new HashMap<String,Object>();
result.put("hello","Hello World");
return result;
}
}
8、启动类加上注解@MapperScan
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
9、启动访问http://localhost:8080/find
结果返回
[
{
"userid": "1",
"username": "小林"
},
{
"userid": "2",
"username": "小马"
}
]
10、项目结构简单粗暴
搞定!