项目层级

 UserModel

 1 package com.miaoshaProject.service.model;
 2 
 3 /**
 4  * @Author wangshuo
 5  * @Date 2022/4/12, 19:46
 6  * Please add a comment
 7  */
 8 public class UserModel {
 9 
10     private Integer id;
11     private String name;
12     private Integer gender;
13     private Integer age;
14     private String telpphone;
15     private String registerMode;
16     private String thiredPartyId;
17 
18     private String encrptPassword;
19 
20     public Integer getId() {
21         return id;
22     }
23 
24     public void setId(Integer id) {
25         this.id = id;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     public Integer getGender() {
37         return gender;
38     }
39 
40     public void setGender(Integer gender) {
41         this.gender = gender;
42     }
43 
44     public Integer getAge() {
45         return age;
46     }
47 
48     public void setAge(Integer age) {
49         this.age = age;
50     }
51 
52     public String getTelpphone() {
53         return telpphone;
54     }
55 
56     public void setTelpphone(String telpphone) {
57         this.telpphone = telpphone;
58     }
59 
60     public String getRegisterMode() {
61         return registerMode;
62     }
63 
64     public void setRegisterMode(String registerMode) {
65         this.registerMode = registerMode;
66     }
67 
68     public String getThiredPartyId() {
69         return thiredPartyId;
70     }
71 
72     public void setThiredPartyId(String thiredPartyId) {
73         this.thiredPartyId = thiredPartyId;
74     }
75 
76     public String getEncrptPassword() {
77         return encrptPassword;
78     }
79 
80     public void setEncrptPassword(String encrptPassword) {
81         this.encrptPassword = encrptPassword;
82     }
83 }

UserVO

 1 package com.miaoshaProject.controller.viewobject;
 2 
 3 /**
 4  * @Author wangshuo
 5  * @Date 2022/4/12, 20:25
 6  * Please add a comment
 7  */
 8 public class UserVO {
 9 
10     private Integer id;
11     private String name;
12     private Integer gender;
13     private Integer age;
14     private String telpphone;
15 
16     public Integer getId() {
17         return id;
18     }
19 
20     public void setId(Integer id) {
21         this.id = id;
22     }
23 
24     public String getName() {
25         return name;
26     }
27 
28     public void setName(String name) {
29         this.name = name;
30     }
31 
32     public Integer getGender() {
33         return gender;
34     }
35 
36     public void setGender(Integer gender) {
37         this.gender = gender;
38     }
39 
40     public Integer getAge() {
41         return age;
42     }
43 
44     public void setAge(Integer age) {
45         this.age = age;
46     }
47 
48     public String getTelpphone() {
49         return telpphone;
50     }
51 
52     public void setTelpphone(String telpphone) {
53         this.telpphone = telpphone;
54     }
55 }

UserController

 1 package com.miaoshaProject.controller;
 2 
 3 import com.miaoshaProject.controller.viewobject.UserVO;
 4 import com.miaoshaProject.service.UesrService;
 5 import com.miaoshaProject.service.model.UserModel;
 6 import org.springframework.beans.BeanUtils;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 
13 /**
14  * @Author wangshuo
15  * @Date 2022/4/12, 18:42
16  * Please add a comment
17  */
18 @Controller("user")
19 @RequestMapping("/user")
20 public class UserController {
21 
22     @Autowired
23     UesrService uesrService;
24 
25     @RequestMapping("/get")
26     @ResponseBody
27     public UserVO getUser(@RequestParam(name = "id")Integer id){
28 
29         //调用service服务获取对应id的用户对象并返回给前端
30         UserModel userModel = uesrService.getById(id);
31         //将核心领域模型对象转化为可供UI使用的viewObject
32         return convertFromModel(userModel);
33     }
34 
35     //BeanCopy
36     private UserVO convertFromModel(UserModel userModel){
37 
38         if (userModel == null)
39             return null;
40         UserVO userVO = new UserVO();
41         BeanUtils.copyProperties(userModel,userVO);
42         return userVO;
43     }
44 }

UserService

 1 package com.miaoshaProject.service;
 2 
 3 import com.miaoshaProject.service.model.UserModel;
 4 
 5 /**
 6  * @Author wangshuo
 7  * @Date 2022/4/12, 18:45
 8  * Please add a comment
 9  */
10 public interface UesrService {
11 
12     UserModel getById(Integer id);
13 }

UserServiceImpl

 1 package com.miaoshaProject.service.impl;
 2 
 3 import com.miaoshaProject.dao.UserDOMapper;
 4 import com.miaoshaProject.dao.UserPasswordDOMapper;
 5 import com.miaoshaProject.dataobject.UserDO;
 6 import com.miaoshaProject.dataobject.UserPasswordDO;
 7 import com.miaoshaProject.service.UesrService;
 8 import com.miaoshaProject.service.model.UserModel;
 9 import org.apache.catalina.User;
10 import org.springframework.beans.BeanUtils;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Service;
13 
14 /**
15  * @Author wangshuo
16  * @Date 2022/4/12, 18:45
17  * Please add a comment
18  */
19 @Service
20 public class UserServiceImpl implements UesrService {
21 
22     @Autowired
23     UserDOMapper userDOMapper;
24     @Autowired
25     UserPasswordDOMapper userPasswordDOMapper;
26 
27     @Override
28     public UserModel getById(Integer id) {
29 
30         //调用mapper获取到对应的UserDO
31         UserDO userDO = userDOMapper.selectByPrimaryKey(id);
32         if (userDO == null){
33             return null;
34         }
35         //通过用户id获取用户的加密密码信息
36         UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(id);
37         return convertFromDataObject(userDO,userPasswordDO);
38     }
39 
40     //不可以将service直接暴露给Controller,所以写方法返回UserModel
41     private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO){
42 
43         if (userDO == null){
44             return null;
45         }
46         UserModel userModel = new UserModel();
47         //把userDO的对应的bean属性copy到UserModel中
48         BeanUtils.copyProperties(userDO,userModel);
49         if (userPasswordDO != null) {
50             userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
51         }
52         return userModel;
53     }
54 }

UserDOMapper

 1 package com.miaoshaProject.dao;
 2 
 3 import com.miaoshaProject.dataobject.UserDO;
 4 
 5 public interface UserDOMapper {
 6     /**
 7      * This method was generated by MyBatis Generator.
 8      * This method corresponds to the database table user_info
 9      *
10      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
11      */
12     int insert(UserDO record);
13 
14     /**
15      * This method was generated by MyBatis Generator.
16      * This method corresponds to the database table user_info
17      *
18      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
19      */
20     int insertSelective(UserDO record);
21 
22     /**
23      * This method was generated by MyBatis Generator.
24      * This method corresponds to the database table user_info
25      *
26      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
27      */
28     UserDO selectByPrimaryKey(Integer id);
29 
30     /**
31      * This method was generated by MyBatis Generator.
32      * This method corresponds to the database table user_info
33      *
34      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
35      */
36     int updateByPrimaryKeySelective(UserDO record);
37 
38     /**
39      * This method was generated by MyBatis Generator.
40      * This method corresponds to the database table user_info
41      *
42      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
43      */
44     int updateByPrimaryKey(UserDO record);
45 }

UserPasswordDOMapper

 1 package com.miaoshaProject.dao;
 2 
 3 import com.miaoshaProject.dataobject.UserPasswordDO;
 4 import org.apache.catalina.User;
 5 
 6 public interface UserPasswordDOMapper {
 7     /**
 8      * This method was generated by MyBatis Generator.
 9      * This method corresponds to the database table user_password
10      *
11      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
12      */
13     int insert(UserPasswordDO record);
14 
15     /**
16      * This method was generated by MyBatis Generator.
17      * This method corresponds to the database table user_password
18      *
19      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
20      */
21     int insertSelective(UserPasswordDO record);
22 
23     /**
24      * This method was generated by MyBatis Generator.
25      * This method corresponds to the database table user_password
26      *
27      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
28      */
29     UserPasswordDO selectByPrimaryKey(Integer id);
30 
31     UserPasswordDO selectByUserId(Integer id);
32 
33     /**
34      * This method was generated by MyBatis Generator.
35      * This method corresponds to the database table user_password
36      *
37      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
38      */
39     int updateByPrimaryKeySelective(UserPasswordDO record);
40 
41     /**
42      * This method was generated by MyBatis Generator.
43      * This method corresponds to the database table user_password
44      *
45      * @mbg.generated Tue Apr 12 16:27:59 CST 2022
46      */
47     int updateByPrimaryKey(UserPasswordDO record);
48 }

UserPasswordDOMapper.xml

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