用户名唯一性验证

dubbo项目:

唯一性验证接口开发规范

用户名唯一性验证_第1张图片

 

创建 TbUserService 接口

 /**
     * 实现用户名唯一性验证
     * @param cond
     * @param type
     * @return
     */
    public EgoResult loadTbUserByCondService(String cond,Integer type);

 

创建 TbUserServiceImpl 实现类

package com.bjsxt.ego.rpc.service.impl;

import com.bjsxt.ego.beans.EgoResult;
import com.bjsxt.ego.rpc.mapper.TbUserMapper;
import com.bjsxt.ego.rpc.pojo.TbUser;
import com.bjsxt.ego.rpc.pojo.TbUserExample;
import com.bjsxt.ego.rpc.service.TbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TbUserServiceImpl implements TbUserService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public EgoResult loadTbUserByCondService(String cond, Integer type) {
        TbUserExample example=new TbUserExample();
        TbUserExample.Criteria c = example.createCriteria();
        //封装查询条件
        if (type.equals(1)){
            c.andUsernameEqualTo(cond);
        }else{
            c.andEmailEqualTo(cond);
        }
        List tbUsers = tbUserMapper.selectByExample(example);

        //创建EgoResult对象
        EgoResult result=new EgoResult();
        result.setStatus(200);
        result.setMsg("ok");
        if (tbUsers!=null&&tbUsers.size()>0){
            result.setData(false);
        }else{
            result.setData(true);
        }
        return result;
    }

    @Override
    public EgoResult saveUserTbService(TbUser tbUser) {

        EgoResult result=new EgoResult();
        try {
            Date date=new Date();
            tbUser.setCreated(date);
            tbUser.setUpdated(date);
            tbUserMapper.insert(tbUser);
            result.setStatus(200);
            result.setMsg("注册成功~");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(200);
            result.setMsg("注册失败~");
        }
        return result;
    }

    @Override
    public TbUser selectUserByUserName(String uname) {
        TbUserExample example=new TbUserExample();
        TbUserExample.Criteria c = example.createCriteria();
        c.andUsernameEqualTo(uname);

        List tbUsers = tbUserMapper.selectByExample(example);
        if (tbUsers!=null&&tbUsers.size()==1){
            return tbUsers.get(0);
        }else {
            return null;
        }
    }
}

 

修改 applicationContext-dubbo.xml

	

 

ego-sso-web 实现

创建 SsoUserService 接口

/**
     * 验证用户名唯一性
     * @param cond
     * @param type
     * @return
     */
    public EgoResult loadUserByCondService(String cond,Integer type);

 

创建 SsoUserServiceImpl 实现类

package com.bjsxt.ego.sso.service.impl;

import com.bjsxt.ego.beans.CookieUtils;
import com.bjsxt.ego.beans.EgoResult;
import com.bjsxt.ego.beans.JsonUtils;
import com.bjsxt.ego.rpc.pojo.TbUser;
import com.bjsxt.ego.rpc.service.TbUserService;
import com.bjsxt.ego.sso.service.SsoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Service
public class SsoUserServiceImpl implements SsoUserService {

    @Autowired
    private TbUserService tbUserServiceProxy;


    @Override
    public EgoResult loadUserByCondService(String cond, Integer type) {
        EgoResult result = tbUserServiceProxy.loadTbUserByCondService(cond, type);
        return result;
    }

}

 

 

创建 SsoUserController

package com.bjsxt.ego.sso.controller;

import com.bjsxt.ego.beans.EgoResult;
import com.bjsxt.ego.rpc.pojo.TbUser;
import com.bjsxt.ego.sso.service.SsoUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class SsoUserController {

    @Autowired
    private SsoUserService ssoUserService;

    @RequestMapping("/user/check/{param}/{type}")
    @ResponseBody
    public MappingJacksonValue userCheck(@PathVariable String param,
                                         @PathVariable Integer type,
                                         @RequestParam(required = false) String callback){
        EgoResult result = ssoUserService.loadUserByCondService(param, type);
        MappingJacksonValue value=new MappingJacksonValue(result);
        if (!StringUtils.isEmpty(callback)){
            value.setJsonpFunction(callback);
        }
        return value;
    }



}

 

修改 applicationContext-dubbo.xml


   

 

你可能感兴趣的:(JavaEE)