SpringBoot,Mysql,Redis综合案例(1)

一、项目搭建

​ 1.在idea创建一个父项目,在pom中导入下述依赖

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>3.2.8</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.右击父项目创建新模块,在新模块添加下述依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-3-starter</artifactId>
            <version>1.2.20</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>3.5.12</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.32</version>
        </dependency>
    </dependencies>

3.在子项目resource目录下创建新文件application.yml(此名称固定)

​ 在此目录下配置,代码如下:

spring:
  data:
    redis:
      host: 你的Linux主机IP地址
      port: 6379
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/你的数据库名称
    username: root
    password: 你的数据库密码
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4.配置springboot启动项

​ 在子项目Main目录下添加下述代码(建议改名为项目名称+Application)

@SpringBootApplication
public class Boot05Application {
    public static void main(String[] args) {

        SpringApplication.run(Boot05Application.class, args);
    }
}

5.创建各个包

​ 创建bin,mapper,service,test包

bin包内创建Student类

@Data
@NoArgsConstructor
public class Student implements Serializable {
    @TableId(value = "id")
    private Long id;
    private String name;
    private String stuNo;
    private String password;
    private Character gender;
    private String birthday;
    private String placeOfOrigin;
    private String enrollmentDate;
    private Long schoolId;
    private Long courseId;
    @TableLogic(delval = "1", value = "0")
    private Integer state;
    private String createTime;
    private String updateTime;

}

mapper包中创建StudentMapper类

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}

service包中创建StudentService类和impl包,在包中创建StudentServiceImpl类

@Service
public interface StudentService extends IService<Student> {
    // 根据学生ID查询单个学生信息
// 参数id:学生唯一标识ID
// 返回值:对应的Student对象(若存在),不存在时可能返回null
Student getStudentById(String id);

// 查询所有学生信息(缓存策略1:Redis中以单个String存储所有学生的JSON数组)
// 返回值:包含所有学生信息的List<Student>集合
List<Student> findAllStudent1();

// 查询所有学生信息(缓存策略2:Redis中以List存储每个学生的JSON对象)
// 返回值:包含所有学生信息的List<Student>集合
List<Student> findAllStudent2();

// 查询所有学生信息(缓存策略3:Redis中以List存储学生ID,以String存储每个学生的JSON详情)
// 返回值:包含所有学生信息的List<Student>集合
List<Student> findAllStudent3();

// 新增或保存学生信息(同时同步更新Redis缓存)
// 参数student:待保存的Student对象
// 返回值:1表示操作成功,0表示操作失败
int saveStudent(Student student);

// 更新学生信息(同时同步更新Redis缓存中对应的数据)
// 参数student:包含更新信息的Student对象(需包含ID)
// 返回值:1表示操作成功,0表示操作失败
int updateStudent(Student student);

// 根据ID删除学生信息(同时删除Redis缓存中对应的数据)
// 参数id:待删除学生的唯一标识ID
// 返回值:1表示操作成功,0表示操作失败
int removeStudentById(String id);
}

sql中创建student表含有下述字段

id							bigint
name					varchar
stu_no					varchar
password				varchar
gender					char
birthday					date
place_of_origin		varchar
enrollment_date		datetime
school_id				bigint
course_id				bigint
state						int
create_time			datetime
update_time			datetime





Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐