JAVA全系列 教程
3762个小节阅读:7089.8k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
Lombok
spring mvc
spring data redis
spring data jpa
xxxxxxxxxx
########################################################
### 配置连接池数据库访问配置
########################################################
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://47.104.210.57:3306/zhonglian?characterEncoding=utf-8&&useSSL=false
spring.datasource.username=root
spring.datasource.password=mytest
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
########################################################
### Java Persistence Api --y
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
########################################################
### 配置连接池数据库访问配置
########################################################
#Redis服务器连接地址
spring.redis.host=192.168.56.31
#Redis服务器连接端口
spring.redis.port=6379
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.pool.max-idle=8
#连接池中的最小空闲连接
spring.redis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=30000
logging.pattern.console=%d{MM/dd HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n
xxxxxxxxxx
@Data
@Entity
@Table(name = "goods")
public class GoodsEntity {
//自增ID
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)//自增
private Long id;
// 商品名字
private String goodsName;
// 订单id
private String orderId;
// 商品数量
private Integer goodsNum;
// 商品价格
private Double price;
}
xxxxxxxxxx
public interface GoodsRepository extends JpaRepository<GoodsEntity,Long> {
}
xxxxxxxxxx
@Repository
@Service
public class GoodsService {
//商品
@Autowired
private GoodsRepository goodsRepository;
/**
* 根据id获取商品信息
* @param id
* @return
*/
public GoodsEntity getId(Long id){
//根据id查询商品信息
Optional<GoodsEntity> goodsEntity = goodsRepository.findById(id);
if (goodsEntity.isPresent()){
return goodsEntity.get();
}else {
return null;
}
}
}
xxxxxxxxxx
/**
* 商品控制层
*/
@RequestMapping("/goods")
@RestController
public class GoodsController {
@Autowired
private GoodsService goodsService;
/**
* 根据id查询商品信息
* @param id
* @return
*/
@GetMapping("/getById/{id}")
public GoodsEntity getById(@PathVariable String id){
return goodsService.getId(Long.valueOf(id));
}
}
登录官网Jmeter下载,得到压缩包jmeter-5.0.zip
D:\apache-jmeter-5.4.3\bin\jmeter.bat文件双击运行。
xxxxxxxxxx
@Repository
@Service
public class GoodsService {
//商品
@Autowired
private GoodsRepository goodsRepository;
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 根据id获取商品信息
* @param id
* @return
*/
public GoodsEntity getId(Long id){
// 从Redis中获取缓存
String goodsStr = redisTemplate.opsForValue().get("goods:id:"+ id);
// 判断是否有缓存
if (StringUtils.isEmpty(goodsStr)){
//根据id查询商品信息
GoodsEntity goodsEntity = goodsRepository.getById(id);
//添加缓存
redisTemplate.opsForValue().set("goods:id:"+ id, JSON.toJSONString(goodsEntity));
return goodsEntity;
}else {
// 把json数据转为goods对象
return JSON.parseObject(goodsStr,GoodsEntity.class);
}
}
}