本文将与大家一起使用SpringMVC开发获取前端查询用户id查询用户信息的功能,其中不仅仅是把数据传给前端,也会包括一些分层领域设计。
本文接着上一篇文章《SpringBoot快速整合Mybatis以及mybatis-generator的使用》继续进行设计,为避免产生疑惑,请先根据上一篇文章配置。本文按照DAO–>service–>Controller顺序讲解,实际开发可能有所不同。
1 DAO层
上一篇文章中,我们通过mybatis-generator分别生成了6个文件,分别是3对DO数据库对象类、Mapper接口以及XML配置文件(例如,UserDO–UserDOMapper–UserDOMapper.xml)。
UserDO
public class UserDO {
private Integer id;
private String name;
private Byte gender;
private Byte age;
private String telephone;
private String registerMode;
private String thirdPartyId;
// getter and setter
}
UserPasswordDO
public class UserPasswordDO {
private Integer id;
private String encrptPassword;
private Integer userId;
// getter and setter
}
之前的数据库设计中,出于安全等考虑,将用户信息和用户密码分成了两个表来存储。其实两者均为用户数据,因此我们会返回所有的数据给Service层做业务处理。
单纯对于查询用户信息这个业务来说,我们分别根据用户id从两个表查出用户基本信息和密码。UserDO和UserPasswordDO在上一篇文章中创建好,本文没有改变,仅需对Mapper接口和Mapper.xml做一些配置。
UserMapper接口和XML配置,mybatis-generator已经自动生成。
UserDO selectByPrimaryKey(Integer id);
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from seckill_user
where id = #{id,jdbcType=INTEGER}
</select>
UserPasswordMapper接口和XML配置
UserPasswordDO selectByUserId(Integer userId);
<select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from seckill_password
where id = #{userId,jdbcType=INTEGER}
</select>
2 Service层
上节DAO层完成了数据库数据映射的一些操作。Service层对DAO层数据封装成Model模型。Model层面才是真正意义上处理业务的核心模型,而Dataobject仅仅作为数据库的映射。
UserModel
public class UserModel {
private Integer id;
private String name;
private Byte gender;
private Byte age;
private String telephone;
private String registerMode;
private String thirdPartyId;
private String encrptPassword;
// getter and setter
}
Service接口–UserService
public interface UserService {
UserModel getUserById(Integer id);
}
Service接口实现–UserServiceImpl
@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());
if (userPasswordDO == null) {
return null;
}
return convertFromDataObject(userDO, userPasswordDO);
}
private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
UserModel userModel = new UserModel();
BeanUtils.copyProperties(userDO, userModel);
// 此处不能用copyProperties()方法,两个dataobject对象中重复字段id
userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
return userModel;
}
}
3 Controller层
上节中Service层已经对DAO层数据根据业务需求做了加工,并返回了Model。直接通过Controller层输出UserModel模型对象,我们会得到下面结果。
启动SpringBoot服务,访问http://localhost:8080/user/get?id=1。
{
"id": 1,
"name": "首位用户",
"gender": 1,
"age": 18,
"telephone": "18611001100",
"registerMode": "bywechat",
"thirdPartyId": "wechatnum",
"encrptPassword": "encrptpassword1234"
}
首先,根据用户id获取用户信息的业务需求已经满足了。但是,这样做会不规范的,会把不必要或者不安全的数据返回给前端,例如后三个字段:登录方式、用户第三方ID、用户密码,既没有必要,还可能带来风险。因此,我们需要在Controler层在Service层返回Model对象的基础上再封装一层对象VO。
UserVO
public class UserVO {
private Integer id;
private String name;
private Byte gender;
private Byte age;
private String telephone;
// getter and setter
}
UserController 代码实现
@Controller("user")
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/get")
@ResponseBody
public UserVO getUser(@RequestParam(name = "id") Integer id) {
// 调用service服务获取对应id的用户对象并返回给前端
UserModel userModel = userService.getUserById(id);
// 将核心领域模型用户对象转化为可供UI使用的viewObject
return convertFromModel(userModel);
}
private UserVO convertFromModel(UserModel userModel) {
if (userModel == null) {
return null;
}
UserVO userVO = new UserVO();
BeanUtils.copyProperties(userModel, userVO);
return userVO;
}
}
4 测试运行
此时,我们已经完成了DAO层、Service层、Controller层代码开发,代码目录结构如下。
启动SpringBoot服务,访问http://localhost:8080/user/get?id=1 ,得到如下对象。
{
"id": 1,
"name": "首位用户",
"gender": 1,
"age": 18,
"telephone": "18611001100"
}