个人随笔
目录
五分钟搭建springboot2.7.16+druid+mybatis-plus环境
2023-10-18 16:48:16

版本

  • 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的相关依赖

  1. <!--数据库配置-->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>8.0.25</version>
  6. </dependency>
  7. <!--数据源-->
  8. <dependency>
  9. <groupId>com.alibaba</groupId>
  10. <artifactId>druid</artifactId>
  11. <version>1.1.22</version>
  12. </dependency>
  13. <!--自动化配置-->
  14. <dependency>
  15. <groupId>com.alibaba</groupId>
  16. <artifactId>druid-spring-boot-starter</artifactId>
  17. <version>1.1.22</version>
  18. </dependency>
  19. <!--mybatis-plus -->
  20. <dependency>
  21. <groupId>com.baomidou</groupId>
  22. <artifactId>mybatis-plus-boot-starter</artifactId>
  23. <version>3.4.2</version>
  24. </dependency>

3、配置文件引入数据源

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai
  5. username: root
  6. password: 123456
  7. type: com.alibaba.druid.pool.DruidDataSource #Druid类型

4、建库建表插入数据

  1. CREATE DATABASE demo;
  2. CREATE TABLE user(
  3. userid BIGINT not null primary key COMMENT '主键ID,雪花算法生成',
  4. username VARCHAR(64) COMMENT '用户名称:这里用微信的昵称'
  5. );
  6. INSERT INTO `demo`.`user` (`userid`, `username`) VALUES (1, '小林');
  7. INSERT INTO `demo`.`user` (`userid`, `username`) VALUES (2, '小马');

5、建User.class

  1. package com.example.demo;
  2. public class User {
  3. private String userid;
  4. private String username;
  5. public String getUserid() {
  6. return userid;
  7. }
  8. public void setUserid(String userid) {
  9. this.userid = userid;
  10. }
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. @Override
  18. public String toString() {
  19. return "User{" +
  20. "userid='" + userid + '\'' +
  21. ", username='" + username + '\'' +
  22. '}';
  23. }
  24. }

6、建UserMapper.class

  1. package com.example.demo;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import org.springframework.stereotype.Repository;
  4. @Repository
  5. public interface UserMapper extends BaseMapper<User> {
  6. }

7、在DemoController加上测试逻辑

  1. package com.example.demo;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. @RestController
  9. public class DemoController {
  10. @Autowired
  11. UserMapper userMapper;
  12. @GetMapping("/find")
  13. public List<User> Test1(){
  14. return userMapper.selectList(null);
  15. }
  16. @GetMapping("/hello")
  17. public Map<String,Object> hello(){
  18. Map<String,Object> result = new HashMap<String,Object>();
  19. result.put("hello","Hello World");
  20. return result;
  21. }
  22. }

8、启动类加上注解@MapperScan

  1. package com.example.demo;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan("com.example.demo")
  7. public class DemoApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(DemoApplication.class, args);
  10. }
  11. }

9、启动访问http://localhost:8080/find

结果返回

  1. [
  2. {
  3. "userid": "1",
  4. "username": "小林"
  5. },
  6. {
  7. "userid": "2",
  8. "username": "小马"
  9. }
  10. ]

10、项目结构简单粗暴

搞定!

 261

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2