Spring boot笔记6

1.整合Redis

1.1. 加入依赖

在pom.xml加入依赖

org.springframework.boot

spring-boot-starter-redis


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 findAll() {

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

List list = this.userDao.findAll();

return list;

}


@Override

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

public List queryUserByName(String name) {

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

List list = this.userMapper.queryUserByName(name);

return list;

}


2. 打jar包

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

org.springframework.boot

spring-boot-maven-plugin


执行package 命令产生一个jar 包


3. 打war包


3.1修改pom.xml

将打包方式修改为war

war


3.2添加依赖

org.springframework.boot

spring-boot-starter-tomcat

provided



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    

你可能感兴趣的:(Spring boot笔记6)