org.springframework.boot
spring-boot-starter-data-redis
2.3.2.RELEASE
下面是有关联的依赖
5.1.38 3.4.2 1.1.9 org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery mysql mysql-connector-java com.baomidou mybatis-plus-boot-starter ${mybatis-plus.version} com.alibaba druid-spring-boot-starter ${druid.version} org.projectlombok lombok org.springframework.boot spring-boot-starter-data-redis
redis:
host: 192.168.614.128
#Redis服务器连接端口
port: 6379
jedis:
pool:
#连接池最大连接数(使用负值表示没有限制)
max-active: 30
#连接池中的最大空闲连接
max-idle: 5
#连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
#连接池中的最小空闲连接
min-idle: 5
#Redis服务器连接密码(默认为空)
password: ok
package com.jgc.towercrane.tcauthorization.services.impl;
import com.jgc.towercrane.tcauthorization.common.TokenGenerator;
import com.jgc.towercrane.tcauthorization.domain.module.Employees;
import com.jgc.towercrane.tcauthorization.services.LoginService;
import com.jgc.towercrane.tcauthorization.domain.vo.EmployeesVO;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@Service
@Transactional
public class LoginServiceImpl implements LoginService {
// @Resource
// private SysloginsMapper sysMapper;
// @Resource
// private EmployeesMapper empMapper;
@Resource
private RedisTemplate templatess;
@Override
public EmployeesVO user_login(String loginname, String loginpwd) {
// ·1 去mysql查询登录信息 直接读mysql数据 对于高并发查询效率较低
// LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
// lqw.eq(Syslogins::getLoginname,loginname).eq(Syslogins::getLoginpwd,loginpwd);
//
// Syslogins syslogins = sysMapper.selectOne(lqw);
// if (syslogins == null){
// return null;
// }else {
// LambdaQueryWrapper ew = new LambdaQueryWrapper<>();
// ew.eq(Employees::getEmpid,syslogins.getEmpid());
// Employees emp = empMapper.selectOne(ew);
T selectOne(@Param("ew") Wrapper queryWrapper);
// //把employees转成EmployeesVO
// EmployeesVO evo = new EmployeesVO();
// BeanUtils.copyProperties(emp,evo);
// return evo;
// }
//·2 去redis里查数据 判断有没有用户
Boolean hasUser = templatess.opsForHash().hasKey("users", loginname + loginpwd);
if(hasUser){
String users = templatess.opsForHash().get("users", loginname + loginpwd).toString();
//分割字符串填充EmpLoyeesVO
String[] infos = users.split(",");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
EmployeesVO evo = null;
try {
evo = EmployeesVO.builder().
empid(Long.parseLong(infos[0]))
.empname(infos[1])
.depid(Integer.parseInt(infos[2]))
.age(Integer.parseInt(infos[3]))
.gender(Integer.parseInt(infos[4]))
.jobid(Integer.parseInt(infos[5]))
.entertime(sdf.parse(infos[6]))
.telephone(infos[7])
// .token(TokenGenerator.getInstance().getToken(infos[0]))
.build();
return evo;
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
@Override
public EmployeesVO token_login(String token) {
// String users = templatess.opsForHash().get("users", token).toString();
return null;
}
// private EmployeesVO buildEmpByStr(){
// return null;
// }
}
测试完成!!!
附:mysql数据导入redis
1opt目录下编写sql
select concat(
'*4\r\n',
'$', length(redis_cmd), '\r\n', redis_cmd, '\r\n',
'$', length(redis_tab), '\r\n', redis_tab, '\r\n',
'$', length(redis_key), '\r\n', redis_key, '\r\n',
'$', length(redis_val), '\r\n', redis_val, '\r'
)
from
(select 'HMSET' as redis_cmd, 'users' as redis_tab,
concat(s.loginname, s.loginpwd) redis_key, concat_ws (',',e.empid, e.empname, e.depid, e.age, e.gender, e.jobid, e.entertime,e.telephone) redis_val
from employees e inner join syslogins s on e.empid = s.empid) t;
2执行导入命令
#-p后为数据库密码,-D后为mysql的数据库名,后面IP是redis服务器的,-a后为redis数据库密码
mysql -uroot -p3090_Cmok -Dtc_hr --default-character-set=utf8 --skip-column-names --raw < /opt/emp.sql | redis-cli -h 192.168.14.118 -a ok --pipe