forked from Yin-Hongwei/music-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
6.13 轮播图存入redis 直接从非关系型数据库中读取 不需要每次都去数据库中查 虽然这个项目轮播图是从前端拿的
- Loading branch information
1 parent
885827e
commit 0e3b860
Showing
26 changed files
with
839 additions
and
49 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
music-server/src/main/java/com/example/yin/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.example.yin.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.CachingConfigurerSupport; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.RedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import java.time.Duration; | ||
|
||
@EnableCaching //开启缓存注解 | ||
@Configuration | ||
public class RedisConfig extends CachingConfigurerSupport { | ||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { | ||
RedisTemplate<String, Object> template = new RedisTemplate<>(); | ||
RedisSerializer<String> redisSerializer = new StringRedisSerializer(); | ||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); | ||
ObjectMapper om = new ObjectMapper(); | ||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | ||
jackson2JsonRedisSerializer.setObjectMapper(om); | ||
template.setConnectionFactory(factory); | ||
//key序列化方式 | ||
template.setKeySerializer(redisSerializer); | ||
//value序列化 | ||
template.setValueSerializer(jackson2JsonRedisSerializer); | ||
//value hashmap序列化 | ||
template.setHashValueSerializer(jackson2JsonRedisSerializer); | ||
return template; | ||
} | ||
|
||
@Bean | ||
public CacheManager cacheManager(RedisConnectionFactory factory) { | ||
RedisSerializer<String> redisSerializer = new StringRedisSerializer(); | ||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); | ||
//解决查询缓存转换异常的问题 | ||
ObjectMapper om = new ObjectMapper(); | ||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | ||
jackson2JsonRedisSerializer.setObjectMapper(om); | ||
// 配置序列化(解决乱码的问题),过期时间600秒 | ||
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() | ||
.entryTtl(Duration.ofSeconds(600)) | ||
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) | ||
.disableCachingNullValues(); | ||
return RedisCacheManager.builder(factory) | ||
.cacheDefaults(config) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
music-server/src/main/java/com/example/yin/controller/BannerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.example.yin.controller; | ||
|
||
import com.example.yin.common.R; | ||
import com.example.yin.service.BannerService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @Author 祝英台炸油条 | ||
* @Time : 2022/6/13 13:16 | ||
**/ | ||
@RestController | ||
@RequestMapping("/banner") | ||
public class BannerController { | ||
|
||
@Autowired | ||
private BannerService bannerService; | ||
|
||
@GetMapping("/getAllBanner") | ||
public R getAllBanner(){ | ||
return R.success("成功获取轮播图与",bannerService.getAllBanner()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
music-server/src/main/java/com/example/yin/mapper/BannerMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.example.yin.mapper; | ||
|
||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
import com.example.yin.model.domain.Banner; | ||
|
||
/** | ||
* @author asus | ||
* @description 针对表【banner】的数据库操作Mapper | ||
* @createDate 2022-06-13 13:13:42 | ||
* @Entity generator.domain.Banner | ||
*/ | ||
public interface BannerMapper extends BaseMapper<Banner> { | ||
|
||
|
||
} |
62 changes: 62 additions & 0 deletions
62
music-server/src/main/java/com/example/yin/model/domain/Banner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.example.yin.model.domain; | ||
|
||
import lombok.Data; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* | ||
* @TableName banner | ||
*/ | ||
@Data | ||
public class Banner implements Serializable { | ||
/** | ||
* | ||
*/ | ||
private Integer id; | ||
|
||
/** | ||
* | ||
*/ | ||
private String pic; | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public boolean equals(Object that) { | ||
if (this == that) { | ||
return true; | ||
} | ||
if (that == null) { | ||
return false; | ||
} | ||
if (getClass() != that.getClass()) { | ||
return false; | ||
} | ||
Banner other = (Banner) that; | ||
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) | ||
&& (this.getPic() == null ? other.getPic() == null : this.getPic().equals(other.getPic())); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); | ||
result = prime * result + ((getPic() == null) ? 0 : getPic().hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(getClass().getSimpleName()); | ||
sb.append(" ["); | ||
sb.append("Hash = ").append(hashCode()); | ||
sb.append(", id=").append(id); | ||
sb.append(", pic=").append(pic); | ||
sb.append(", serialVersionUID=").append(serialVersionUID); | ||
sb.append("]"); | ||
return sb.toString(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
music-server/src/main/java/com/example/yin/service/BannerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.yin.service; | ||
|
||
import com.baomidou.mybatisplus.extension.service.IService; | ||
import com.example.yin.model.domain.Banner; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author asus | ||
* @description 针对表【banner】的数据库操作Service | ||
* @createDate 2022-06-13 13:13:42 | ||
*/ | ||
public interface BannerService extends IService<Banner> { | ||
|
||
List<Banner> getAllBanner(); | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
music-server/src/main/java/com/example/yin/service/impl/BannerServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.yin.service.impl; | ||
|
||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | ||
import com.example.yin.mapper.BannerMapper; | ||
import com.example.yin.model.domain.Banner; | ||
import com.example.yin.service.BannerService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author asus | ||
* @description 针对表【banner】的数据库操作Service实现 | ||
* @createDate 2022-06-13 13:13:42 | ||
*/ | ||
@Service | ||
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> | ||
implements BannerService { | ||
|
||
@Autowired | ||
private BannerMapper bannerMapper; | ||
|
||
@Cacheable(value = "banner", key = "'list'") //放在缓存中 redis 是以key-value进行存储的 | ||
@Override | ||
public List<Banner> getAllBanner() { | ||
System.out.println("没有走缓存"); | ||
return bannerMapper.selectList(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
spring.datasource.url=jdbc:mysql://localhost:3306/tp_music?serverTimezone=Asia/Shanghai&useSSL=false | ||
spring.datasource.username=root | ||
spring.datasource.password=root | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
spring.datasource.url=jdbc:mysql://121.43.39.136:3306/zeng?serverTimezone=GMT%2B8&useSSL=false | ||
spring.datasource.username=zyt | ||
spring.datasource.password=zyt | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,24 @@ | ||
#spring.datasource.url=jdbc:mysql://localhost:3306/tp_music?serverTimezone=Asia/Shanghai&useSSL=false | ||
#spring.datasource.username=root | ||
#spring.datasource.password=root | ||
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
|
||
spring.datasource.url=jdbc:mysql://121.43.39.136:3306/zeng?serverTimezone=GMT%2B8&useSSL=false | ||
spring.datasource.username=zyt | ||
spring.datasource.password=zyt | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
|
||
mybatis.typeAliasesPackage=com.example.yin.model.domain | ||
mybatis.mapperLocations=classpath:mapper/*.xml | ||
|
||
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss | ||
spring.jackson.time-zone=GMT+8 | ||
|
||
server.port=8888 | ||
|
||
#热部署生效 | ||
spring.devtools.restart.enabled=false | ||
|
||
#设置重启的目录 | ||
spring.devtools.restart.additional-paths=src/main/java | ||
|
||
#classpath目录下的WEB-INF文件夹内容修改不重启 | ||
spring.devtools.restart.exclude=WEB-INF/** | ||
|
||
# 关闭CONDITIONS EVALUATION REPORT及自动配置内容向控制台的输出 | ||
logging.level.org.springframework.boot.autoconfigure=ERROR | ||
logging.level.org.springframework.boot.autoconfigure=ERROR | ||
# redis相应的地址 还有一些配置 | ||
spring.redis.host=127.0.0.1 | ||
spring.redis.port=6379 | ||
spring.redis.database=0 | ||
spring.redis.timeout=1800000 | ||
spring.redis.lettuce.pool.max-active=20 | ||
spring.redis.lettuce.pool.max-wait=-1 | ||
#最大阻塞等待时间(负数表示没限制) | ||
spring.redis.lettuce.pool.max-idle=5 | ||
spring.redis.lettuce.pool.min-idle=0 | ||
spring.profiles.active=dev |
Oops, something went wrong.