栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot从如何搭建到启动的全过程(包含mybatis的数据显示,以及redis缓存)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot从如何搭建到启动的全过程(包含mybatis的数据显示,以及redis缓存)

第一步建立表和数据

这个就不需要我多说了吧,就是直接进入正题,然后在mysql的命令行中进行数据库和表的创建

第二步搭建springboot

搭建好了之后下载如下的依赖,当然了,如果你不会下载依赖的话,可以在里面进行勾选,我现在把我下载的依赖复制过来给大家看看



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.example
    springboot
    0.0.1-SNAPSHOT
    springboot
    Demo project for Spring Boot
    
        11
    
    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            mysql
            mysql-connector-java
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
           2.2.0
        
        
            org.projectlombok
            lombok
        

        
            org.springframework.boot
            spring-boot-starter-redis
            1.3.5.RELEASE
        



       

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



第三步(搭建entity这个实体包下的user)
package com.example.springboot.entity;

import jdk.jfr.DataAmount;
import lombok.Data;

import java.io.Serializable;

@Data
public class User implements Serializable {
    private Integer id;
    private String username;
    private String address;
    public User(){
        super();
    }
    public User(Integer id,String username,String address){
        super();
        this.address=address;
        this.username=username;
        this.id=id;
    }
}

第四步建立mapper、service、controller的关系


首先我想说的就是在springboot里面,我们同样要用这三个包,并且他们的依赖关系依次是mapper->service->controller
顾名思义,首先要创建的就是mapper,然后在里面定义一些注解,比如说你这个类代表着mapper,那么你就在类的最上方定义这个注解叫@Mapper,这样就是告诉springboot你添加了这个类到容器中,让它帮你管理,然后用的时候,就可以用注解@Autowired进行一个自动的创建对象

package com.example.springboot.Mapper;

import com.example.springboot.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from tb_user")
    List getAllUsers();

    @Delete("delete from tb_user where id=#{id}")
    void delete(Integer id);
}

接下来就是创建service这个包中的东西,那就是在里面也搭建接口,然后通过实习类的时候,在实现类利用mapper的方法来实现
UserService.interface

package com.example.springboot.Service;

import com.example.springboot.entity.User;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public interface UserService {
    //查询所有
    List getAllUsers();
    //靠id来删除值
    void deleteUser(Integer id);
}

下面就是其实现类

package com.example.springboot.Service;

import com.example.springboot.Mapper.UserMapper;
import com.example.springboot.entity.User;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service

public class UserServiceImpl implements UserService{

    //注入用户Mapper
    @Autowired
    UserMapper userMapper;
    //这里就是引入UserService里面的接口
    @Cacheable(value = "UserCache",key="'user.getAllUsers'")
    public List getAllUsers(){
        return this.userMapper.getAllUsers();
    }

    @Override
    @CacheEvict(value="UserCache",key="'user.getAllUsers'")
    public void deleteUser(Integer id) {
        System.out.println("删除了id为"+id+"的用户");
        this.userMapper.delete(id);
    }
}


当然,这里你可以暂时不慌去管我代码里面建立的@CacheEvict,因为这是在设置redis缓存的时候会用到。
接下来,就是去配置文件了

第五步 配置resoure中的application.properties
server.port=8085

spring.datasource.url=jdbc:mysql://localhost:3306/cd?serverTimezone=GMT%2B8
spring.datasource.password=123456789
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


spring.redis.port=6379
spring.redis.host=127.0.0.1

logging.level.com.example.springboot=debug

首先,对应的server.port就是端口号,然后在里面进行mysql的配置,配置方法同样也是看图吧~

接下来,就可以进行启动啦


假如你现在想要根据id号来进行删除数据的话,就可以通过

下图来进行,然后通过观察控制台

就代表删除成功,接下来,我们为了保证删除的操作之后,数据库和redis缓存的数据保持一致,所以在delete方法上面加上了刚刚的注解

所以现在就可以啦!

总结

反正呢,SpringBoot的搭建和启动就是这样的了,今天主要是在群里看到那些大佬说,他们面试的时候问到过springboot的启动过程和原理,因此在这里提醒一下小伙伴们,一定还是要多看看注解,以及对应的一些源码呀,比如说对应得springbootApplication这个类中的注解@SpringBootApplication,它是一个复合注解,其包含了@EnableautoConfiguration和@ComponentScan还有@SpringbootConfiguration这样的注解,还是要多去理解,其中,@SpringbootConfiguration就是告诉ioc容器这个类是ioc的配置类,@EnableautoConfiguration这个就是一个自动装配的东西。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/274832.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号