SpringBoot 2.x 实战仿B站高性能后端项目吾爱分享

SpringBoot 2.x 实战仿B站高性能后端项目吾爱分享,第1张

SpringBoot 2.x 实战仿B站高性能后端项目吾爱分享

 

 

快速入门
  1. 创建一个SpringBoot项目

  2. 导入依赖

    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.3.4.RELEASE
             
        
        com.example
        mybatis-plus
        0.0.1-SNAPSHOT
        mybatis-plus
        
            1.8
        
        
            
                org.springframework.boot
                spring-boot-starter
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
                org.springframework.boot
                spring-boot-configuration-processor
            
            
                com.baomidou
                mybatis-plus-boot-starter
                3.4.2
            
            
                mysql
                mysql-connector-java
                runtime
            
            
                org.projectlombok
                lombok
            
        
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    复制代码
  3. 配置数据库

    # application.yml
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/yogurt?serverTimezone=Asia/Shanghai
        username: root
        password: root
        
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启SQL语句打印
    复制代码
  4. 创建一个实体类

    package com.example.mp.po;
    import lombok.Data;
    import java.time.LocalDateTime;
    @Data
    public class User {
    	private Long id;
    	private String name;
    	private Integer age;
    	private String email;
    	private Long managerId;
    	private LocalDateTime createTime;
    }
    复制代码
  5. 创建一个mapper接口

    package com.example.mp.mappers;
    import com.baomidou.mybatisplus.core.mapper.baseMapper;
    import com.example.mp.po.User;
    public interface UserMapper extends baseMapper { }
    复制代码
  6. 在SpringBoot启动类上配置mapper接口的扫描路径

    package com.example.mp;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    @MapperScan("com.example.mp.mappers")
    public class MybatisPlusApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(MybatisPlusApplication.class, args);
    	}
    }
    复制代码
  7. 在数据库中创建表

    DROP TABLE IF EXISTS user;
    CREATE TABLE user (
    id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主键',
    name VARCHAr(30) DEFAULT NULL COMMENT '姓名',
    age INT(11) DEFAULT NULL COMMENT '年龄',
    email VARCHAr(50) DEFAULT NULL COMMENT '邮箱',
    manager_id BIGINT(20) DEFAULT NULL COMMENT '直属上级id',
    create_time DATETIME DEFAULT NULL COMMENT '创建时间',
    ConSTRAINT manager_fk FOREIGN KEY(manager_id) REFERENCES user (id)
    ) ENGINE=INNODB CHARSET=UTF8;
    
    INSERT INTO user (id, name, age ,email, manager_id, create_time) VALUES
    (1, '大BOSS', 40, 'boss@baomidou.com', NULL, '2021-03-22 09:48:00'),
    (2, '李经理', 40, 'boss@baomidou.com', 1, '2021-01-22 09:48:00'),
    (3, '黄主管', 40, 'boss@baomidou.com', 2, '2021-01-22 09:48:00'),
    (4, '吴组长', 40, 'boss@baomidou.com', 2, '2021-02-22 09:48:00'),
    (5, '小菜', 40, 'boss@baomidou.com', 2, '2021-02-22 09:48:00')
    复制代码
  8. 编写一个SpringBoot测试类

    package com.example.mp;
    import com.example.mp.mappers.UserMapper;
    import com.example.mp.po.User;
    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;
    import java.util.List;
    import static org.junit.Assert.*;
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SampleTest {
    	@Autowired
    	private UserMapper mapper;
    	@Test
    	public void testSelect() {
    		List list = mapper.selectList(null);
    		assertEquals(5, list.size());
    		list.forEach(System.out::println);
    	}
    }


 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5684089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存