电子商务实验JAVA_Java电商秒杀实战1

搭建springboot项目

添加依赖

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-configuration-processor

true

mysql

mysql-connector-java

com.alibaba

druid

1.0.5

目录结构

6ab7b10d93028109c0296d14e7780840.png

集成Mybatis

添加mybatis-spring-boot-starter依赖

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

添加配置

#mybatis

mybatis.type-aliases-package=com.imooc.miaosha.domain

mybatis.configuration.map-underscore-to-camel-case=truemybatis.configuration.default-fetch-size=100mybatis.configuration.default-statement-timeout=3000mybatis.mapperLocations=classpath:com/imooc/miaosha/dao/*.xml

添加druid配置

#druid

spring.datasource.url=jdbc:mysql://localhost:3306/miaosha?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

spring.datasource.username=root

spring.datasource.password=123spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.filters=stat

spring.datasource.tomcat.max-active=2spring.datasource.dbcp2.initial-size=1spring.datasource.tomcat.max-wait=60000spring.datasource.tomcat.min-idle=1spring.datasource.tomcat.time-between-eviction-runs-millis=60000spring.datasource.tomcat.min-evictable-idle-time-millis=300000spring.datasource.tomcat.validation-query=select 'x'spring.datasource.tomcat.test-on-borrow=truespring.datasource.dbcp2.test-on-borrow=falsespring.datasource.dbcp2.test-on-return=falsespring.datasource.dbcp2.pool-prepared-statements=truespring.datasource.dbcp2.max-open-prepared-statements=20

添加mysql、druid依赖

mysql

mysql-connector-java

com.alibaba

druid

1.0.5

集成redis

首先安装Redis

修改配置文件redis.conf

将bind 127.0.0.1注释掉

1cb5097a9d11446946512e7c50504ef9.png

修改daemonize为yes

adfc131f7e37d82383e4692dc4c6ecf8.png

设置密码

30a299a599e50f64c8adb1a252a275e5.png

redies连接失败:Could not get a resource from the pool] with root cause

解决方法:关闭linux中的防火墙

命令:systemctl stop firewalld

添加Jedis依赖、添加Fastison依赖

redis.clients

jedis

com.alibaba

fastjson

1.2.38

添加redis配置

#redis

spring.redis.host=47.97.197.62spring.redis.port=6379spring.redis.timeout=3spring.redis.password=123456spring.redis.pool-max-total=10spring.redis.pool.max-idle=10spring.redis.pool.max-wait=3spring.redis.pool.max-active=8

RedisConfig

packagecom.imooc.miaosha.redis;importorg.apache.ibatis.annotations.Param;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.stereotype.Component;

@Component

@ConfigurationProperties(prefix="spring.redis")public classRedisConfig {privateString host;private intport;private inttimeout;privateString password;private intpoolMaxTotal;private intpoolMaxIdle;private intpoolMaxWait;publicString getHost() {returnhost;

}public voidsetHost(String host) {this.host =host;

}public intgetPort() {returnport;

}public void setPort(intport) {this.port =port;

}public intgetTimeout() {returntimeout;

}public void setTimeout(inttimeout) {this.timeout =timeout;

}publicString getPassword() {returnpassword;

}public voidsetPassword(String password) {this.password =password;

}public intgetPoolMaxTotal() {returnpoolMaxTotal;

}public void setPoolMaxTotal(intpoolMaxTotal) {this.poolMaxTotal =poolMaxTotal;

}public intgetPoolMaxIdle() {returnpoolMaxIdle;

}public void setPoolMaxIdle(intpoolMaxIdle) {this.poolMaxIdle =poolMaxIdle;

}public intgetPoolMaxWait() {returnpoolMaxWait;

}public void setPoolMaxWait(intpoolMaxWait) {this.poolMaxWait =poolMaxWait;

}

}

RedisService

packagecom.imooc.miaosha.redis;importcom.alibaba.fastjson.JSON;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Bean;importorg.springframework.stereotype.Service;importredis.clients.jedis.Jedis;importredis.clients.jedis.JedisPool;importredis.clients.jedis.JedisPoolConfig;

@Servicepublic classRedisService {

@Autowired

JedisPool jedisPool;/*** 获取单个对象*/

public T get(KeyPrefix prefix,String key, Classclazz) {

Jedis jedis=null;try{

jedis=jedisPool.getResource();//生成真正的key

String realKey=prefix.getPrefix()+key;

String str=jedis.get(realKey);

T t=stringToBean(str,clazz);returnt;

}finally{

returnToPool(jedis);

}

}/*** 设置对象*/

public booleanset(KeyPrefix prefix, String key, T value) {

Jedis jedis=null;try{

jedis=jedisPool.getResource();

String str=beanToString(value);if(str==null||str.length()<=0){return false;

}//生成真正的key

String realKey=prefix.getPrefix()+key;int seconds=prefix.expireSeconds();if(seconds<=0){

jedis.set(realKey,str);

}else{

jedis.setex(realKey,seconds,str);

};return true;

}finally{

returnToPool(jedis);

}

}/*** 判断key是否存在*/

public booleanexists(KeyPrefix prefix, String key) {

Jedis jedis=null;try{

jedis=jedisPool.getResource();//生成真正的key

String realKey=prefix.getPrefix()+key;returnjedis.exists(realKey);

}finally{

returnToPool(jedis);

}

}/*** 增加值*/

public Long incr(KeyPrefix prefix, String key) {

Jedis jedis=null;try{

jedis=jedisPool.getResource();//生成真正的key

String realKey=prefix.getPrefix()+key;returnjedis.incr(realKey);

}finally{

returnToPool(jedis);

}

}/*** 减少值*/

public Long decr(KeyPrefix prefix, String key) {

Jedis jedis=null;try{

jedis=jedisPool.getResource();//生成真正的key

String realKey=prefix.getPrefix()+key;returnjedis.decr(realKey);

}finally{

returnToPool(jedis);

}

}private String beanToString(T value){if(value==null){return null;

}

Class> clazz=value.getClass();if(clazz==int.class||clazz==Integer.class){return ""+value;

}else if(clazz==String.class){return(String)value;

}else if(clazz==long.class||clazz==Long.class){return ""+value;

}else{returnJSON.toJSONString(value);

}

}

@SuppressWarnings("unchecked")private T stringToBean(String str,Classclazz){if(str==null||str.length()<=0||clazz==null){return null;

}if(clazz==int.class||clazz==Integer.class){return(T)Integer.valueOf(str);

}else if(clazz==String.class){return(T) str;

}else if(clazz==long.class||clazz==Long.class){return(T)Long.valueOf(str);

}else{returnJSON.toJavaObject(JSON.parseObject(str), clazz);

}

}private voidreturnToPool(Jedis jedis){if(jedis!=null){

jedis.close();

}

}

}

运行结果

086996bfe52c21a4f572dabe6115a694.png

bd44fcd5f19b9cf7e9573938ffc81bf3.png

68f724bad5a33d9a9f6bd2ca7369ecb7.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值