Spring boot笔记6

1.整合Redis

1.1. 加入依赖

在pom.xml加入依赖

<!-- 配置使用redis启动器 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-redis</artifactId>

</dependency>


1.2. 修改引导类

修改开启缓存,添加注解@EnableCaching

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cache.annotation.EnableCaching;


@SpringBootApplication

@EnableCaching

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}


}


1.3. 设置实现序列化接口

需要修改实体,让实体实现序列化接口

@Entity

public class User implements Serializable {

@Id

private Long id;

private String userName;

private String password;

private String name;

}


1.4. 实现添加/删除缓存

修改UserServiceImpl,添加@Cacheable注解实现缓存添加

@Override

@Cacheable(value = "userCache", key = "'user.findAll'")

public List<User> findAll() {

System.out.println("从Mysql中查询");

List<User> list = this.userDao.findAll();

return list;

}


@Override

@CacheEvict(value = "userCache", key = "'user.findAll'")

public List<User> queryUserByName(String name) {

System.out.println("缓存清理了!");

List<User> list = this.userMapper.queryUserByName(name);

return list;

}


2. 打jar包

在工程的pom.xml中添加以下依赖

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>


执行package 命令产生一个jar 包


3. 打war包


3.1修改pom.xml

将打包方式修改为war

<packaging>war</packaging>


3.2添加依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<scope>provided</scope>

</dependency>



3.3添加ServletInitializer

import org.springframework.boot.builder.SpringApplicationBuilder;

import

org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder

application) {

return application.sources(Application.class);

}

}


3.4运行package 打包命令生成war    

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容