1.将service,mapper层抽取出一个基础service、mapper层
步骤:
之前抽取了Query和Domain为BaseQuery 和BaseDomain
步骤:
1.先抽取Mapper接口为BaseMapper(将方法中的实体类型替换成泛型),类上也需要加泛型
interface BaseMapper<T> {
List<T> findAll();
}
Long queryCount(BaseQuery baseQuery);--BaseQuery--是抽取后的
List<T> queryData(BaseQuery baseQuery);
2.抽取service接口为IBaseService,和抽取Mapper相同
3.抽取实现类要继承抽取后的接口,
并且依赖注入抽取后的BaseMapper接口
class BaseServiceImpl<T> implements IBaseService<T>
@Autowired
private BaseMapper<T> baseMapper;
@Override
public PageList<T> query(BaseQuery baseQuery) {
实现:
service接口继承IBaseService接口;
extends IBaseService<Employee> {
mapper接口继承BaseMapper接口;
e EmployeeMapper extends BaseMapper<Employee> {
将重复的方法删除
service实现类要继承BaseServiceImpl业务类
EmployeeServiceImpl extends BaseServiceImpl<Employee>
1.纯前端项目需要在每个功能模块页面引入相应的插件
比如:vue axios common.js(这里是配置好axios的全局属性和基础路径的)
<!--引入vue-->
<script src="js/vue.js"></script>
<!-- 在线引入axios 这里是之前在minio做的-->
<script src="http://115.159.88.63:9090/resources/axios/dist/axios.min.js"></script>
<!-- 引入common.js,里面有axios的全局配置和基础属性-->
<script src="js/common.js"></script>
无状态的token方案:
1.总体思路:
前端发请求传递参数
后端接受参数,处理请求,响应数据
前端对响应的数据做处理
2.代码实现
前端发请求传递参数:
this.logining = true;//显示加载框
this.$http.post("/login/account",this.ruleForm).then(res=>{
if(res.data.success){//登录成功
//将token和logininfo放在localStorage
localStorage.setItem("token",res.data.resultObj.token); //后续获取redis的登录信息校验登录
localStorage.setItem("username",res.data.resultObj.logininfo.username);//登录后显示用户信息
//跳转到后台首页
this.$router.push({path: '/echarts'});
}else{
this.$message.error(res.data.message);
}
}).catch(res=>{
this.$message.error("系统繁忙,请稍后重试");
})
this.logining = false;
后端接受参数,处理请求,响应数据
dto - controller - service业务
//一:校验空值
if(StrUtil.isBlank(loginDto.getAccount()) || StrUtil.isBlank(loginDto.getPassword())){
throw new BusinessException("信息不能为空");
}
//二:校验用户名
Logininfo logininfo = logininfoMapper.findByAccount(loginDto);
if(logininfo==null){
throw new BusinessException("账号不存在");
}else{
//三:校验密码
String dbPwd = logininfo.getPassword();//加密加盐
String md5Pwd = SecureUtil.md5(loginDto.getPassword() + logininfo.getSalt());
if(!dbPwd.equals(md5Pwd)){
throw new BusinessException("密码错误");
}
}
//四:校验是否禁用
if(!logininfo.getDisable()){
throw new BusinessException("账号异常,请联系管理员");
}
String token = UUID.randomUUID().toString();
//String token = StrUtils.getComplexRandomString(36);
//String token = RandomUtil.randomString(36);
redisTemplate.opsForValue().set(token,logininfo,30, TimeUnit.MINUTES);
//这里为啥要用map?装多个数据
Map<String,Object> map = new HashMap<>();
map.put("token",token);
logininfo.setSalt(null);
logininfo.setPassword(null);
map.put("logininfo",logininfo);
//五:返回数据
return AjaxResult.me().setResultObj(map);
xml中:
select * from t_logininfo where type=#{type}
and (username = #{account}
or phone = #{account}
or email = #{account})
//获取request请求对象
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();