• Spring Boot 构建电商基础秒杀项目 (二) 使用 Spring MVC 方式获取用户信息


    SpringBoot构建电商基础秒杀项目 学习笔记

    修改 DOMapper

    在 UserPasswordDOMapper.xml 添加:

      <select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from user_password
        where user_id = #{userId,jdbcType=INTEGER}
      </select>
    

    在 UserPasswordDOMapper 添加:

    UserPasswordDO selectByUserId(Integer userId);
    

    添加 model

    public class UserModel {
    
        private Integer id;
        private String name;
        private Byte gender;
        private Integer age;
        private String telphone;
        private String registerMode;
        private String thirdPartyId;
    
        private String encrptPassword;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Byte getGender() {
            return gender;
        }
    
        public void setGender(Byte gender) {
            this.gender = gender;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getTelphone() {
            return telphone;
        }
    
        public void setTelphone(String telphone) {
            this.telphone = telphone;
        }
    
        public String getRegisterMode() {
            return registerMode;
        }
    
        public void setRegisterMode(String registerMode) {
            this.registerMode = registerMode;
        }
    
        public String getThirdPartyId() {
            return thirdPartyId;
        }
    
        public void setThirdPartyId(String thirdPartyId) {
            this.thirdPartyId = thirdPartyId;
        }
    
        public String getEncrptPassword() {
            return encrptPassword;
        }
    
        public void setEncrptPassword(String encrptPassword) {
            this.encrptPassword = encrptPassword;
        }
    
    }
    

    添加 service

    添加接口:

    public interface UserService {
    
        UserModel getUserById(Integer id);
    }
    

    添加接口的实现:

    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserDOMapper userDOMapper;
    
        @Autowired
        private UserPasswordDOMapper userPasswordDOMapper;
    
        @Override
        public UserModel getUserById(Integer id){
            UserDO userDO = userDOMapper.selectByPrimaryKey(id);
    
            if(userDO == null){
                return null;
            }
    
            UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
    
            return convertFromDataObject(userDO, userPasswordDO);
    
        }
    
        private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO){
            if(userDO == null){
                return null;
            }
    
            UserModel userModel = new UserModel();
            BeanUtils.copyProperties(userDO, userModel);
    
            if(userPasswordDO != null){
                userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
            }
    
            return userModel;
        }
    }
    

    添加 viewobject

    public class UserVO {
    
        private Integer id;
        private String name;
        private Byte gender;
        private Integer age;
        private String telphone;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Byte getGender() {
            return gender;
        }
    
        public void setGender(Byte gender) {
            this.gender = gender;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getTelphone() {
            return telphone;
        }
    
        public void setTelphone(String telphone) {
            this.telphone = telphone;
        }
    }
    

    添加 controller

    @Controller("user")
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @RequestMapping("/get")
        @ResponseBody
        public UserVO getUser(@RequestParam(name="id") Integer id){
            UserModel userModel = userService.getUserById(id);
    
            return convertFromModel(userModel);
        }
    
        private UserVO convertFromModel(UserModel userModel){
            if(userModel == null){
                return null;
            }
    
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userModel, userVO);
    
            return userVO;
        }
    }
    
    • data object: 与数据库完全映射,不包含任何逻辑

    • model: 业务交互逻辑模型

    • view object: 前端展示数据

    源码:spring-boot-seckill

  • 相关阅读:
    牛客小白月赛2 D 虚虚实实 【欧拉图】【连通图】
    牛客小白月赛2 A 数字方阵【随机】【找规律】
    牛客小白月赛1 J おみやげをまらいました 【MAP】
    牛客小白月赛1 I あなたの蛙が帰っています 【卡特兰数】
    欧拉函数
    乘法逆元
    扩展欧几里得
    快速乘法
    JPEG图像压缩出现资源不足问题的解决
    如何避免关键程序被意外关闭?
  • 原文地址:https://www.cnblogs.com/victorbu/p/10544653.html
Copyright © 2020-2023  润新知