目录
一、分层领域模型规约
DO(Data Object):与数据库表结构一一对应,通过DAO层向上传输数据源对象。
DTO(Data Transfer Object):数据传输对象,service或manager向外传输的对象。
BO(Business Object):业务对象。由service层输出的封装业务逻辑的对象。
AO(Application Object):应用对象。在web层与service层之间抽象的复用对象模型,极为贴近展示层,复用度不高。
VO(View Object):显示层对象,通常是web向模板渲染引擎层传输的对象。
Query:数据查询对象,各层接收上层的查询请求。注意超过2个参数的查询封装,禁止使用map类来传输。
目的是为了保证RPC传输数据的安全性的问题。
二、整合数据库DB层
相关测试表结构
CREATE TABLE `demo_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
`user_pwd` varchar(255) DEFAULT NULL,
`user_age` int(3) DEFAULT NULL,
`user_addres` varchar(255) DEFAULT NULL,
`user_integral` int(10) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
三、整合mybatisplus
1. Maven依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
2. application.yml 新增
spring:
application:
###服务的名称
name: demo-member
cloud:
nacos:
discovery:
###nacos注册地址
server-addr: 127.0.0.1:8848
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo-member?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
logging:
level:
###打印mybatis日志
com.demo.api.impl.mapper : debug
server:
port: 7000
3. 实体类
com.demo.api.impl.entity
package com.demo.api.impl.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @Author: JYC
* @Title: UserEntity
* @Description: TODO
* @Date: 2022/4/24 20:17
*/
@Data
@TableName("demo_user")
public class UserDo {
@TableId
private Integer userId;
private String userName;
private String userPwd;
private Integer userAge;
private String userAddres;
private Integer userIntegral;
}
4. UserMapper
package com.demo.api.impl.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.api.impl.entity.UserDo;
/**
* @Author: JYC
* @Title: UserMapper
* @Description: TODO
* @Date: 2022/4/24 20:20
*/
public interface UserMapper extends BaseMapper<UserDo> {
}
5. 错误代码演示
MemberService
package com.demo.api.member;
import com.demo.api.base.BaseResponse;
import com.demo.api.member.dto.req.UserReqDto;
import com.demo.api.member.dto.resp.UserRespDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
public interface MemberService {
@GetMapping("memberToWeiXin")
String memberToWeiXin(Integer a);
/**
* 不符合规范
* @return
*/
@GetMapping("addMember")
BaseResponse<String> addMember(String userName, String pwd, Integer age);
/**
* updateUser
* @param map
* @return
*/
@PostMapping("updateUser")
Object updateUser(@RequestBody Map<String, String> map);
}
MemberServiceImpl
package com.demo.api.impl.member;
import com.alibaba.fastjson.JSON;
import com.demo.api.base.BaseApiService;
import com.demo.api.base.BaseResponse;
import com.demo.api.impl.entity.UserDo;
import com.demo.api.impl.feign.WeChatServiceFeign;
import com.demo.api.impl.mapper.UserMapper;
import com.demo.api.member.MemberService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @Author: JYC
* @Title: MemberServiceImpl
* @Description: TODO
* @Date: 2022/4/24 14:37
*/
@RestController
public class MemberServiceImpl extends BaseApiService implements MemberService {
@Autowired
private WeChatServiceFeign weChatServiceFeign;
@Autowired
private UserMapper userMapper;
@Override
public String memberToWeiXin(Integer a) {
// Feign调用微信服务接口
return weChatServiceFeign.getWeChat(a);
}
@Override
public BaseResponse<String> addMember(String userName, String pwd, Integer age) {
if (StringUtils.isEmpty(userName)) {
return setResultError("userName is null");
}
int j = 1 / age;
return setResultSuccess();
}
@Override
public Object updateUser(Map<String, String> map) {
//1.接受参数
String jsonStr = JSON.toJSONString(map);
UserDo user = JSON.parseObject(jsonStr, UserDo.class);
Integer userId = user.getUserId();
//2.update
int i = userMapper.updateById(user);
if (i <= 0) {
return "error";
}
//3.查询db数据
UserDo userDo = userMapper.selectById(userId);
return userDo == null ? "error" : userDo;
}
}
测试请求接口:
127.0.0.1:7000/updateUser
请求参数:
{
"userId": "1",
"userName": "demo",
"userPwd": "123",
"userAge": "22",
"userAddres": "杭州市",
"userIntegral": "888"
}
响应内容:
{
"userId": 1,
"userName": "demo",
"userPwd": "123",
"userAge": 22,
"userAddres": "杭州市",
"userIntegral": 888
}
结果:可以注入修改userPwd、积分,返回用户的密码 等铭感信息。
6. 改造成dto转化
请求包名:com.demo.api.member.dto.req
响应包名:com.demo.api.member.dto.resp
7. 新增: reqdto
package com.demo.api.member.dto.req;
import lombok.Data;
/**
* @Author: JYC
* @Title: UserReqDto
* @Description: TODO
* @Date: 2022/4/24 22:50
*/
@Data
public class UserReqDto {
private Integer userId;
private String userName;
private Integer userAge;
private String userAddres;
}
8. 新增: respdto
package com.demo.api.member.dto.resp;
import lombok.Data;
/**
* @Author: JYC
* @Title: UserRespDto
* @Description: TODO
* @Date: 2022/4/24 22:52
*/
@Data
public class UserRespDto {
private Integer userId;
private String userName;
private Integer userAge;
private String userAddres;
private Integer userIntegral;
}
9. dto与do转换工具类
com.demo.api.utils
package com.demo.api.utils;
import org.springframework.beans.BeanUtils;
/**
* @Author: JYC
* @Title: BeanUtils
* @Description: dto与do转换工具
* @Date: 2022/4/24 22:58
*/
public class DemoBeanUtils<Dto, Do> {
/**
* dto 转换为Do 工具类
*/
public static <Do> Do dtoToDo(Object dtoEntity, Class<Do> doClass) {
// 判断dto是否为空!
if (dtoEntity == null) {
return null;
}
// 判断DoClass 是否为空
if (doClass == null) {
return null;
}
try {
Do newInstance = doClass.newInstance();
BeanUtils.copyProperties(dtoEntity, newInstance);
// Dto转换Do
return newInstance;
} catch (Exception e) {
return null;
}
}
/**
* do 转换为Dto 工具类
*/
public static <Dto> Dto doToDto(Object doEntity, Class<Dto> dtoClass) {
// 判断dto是否为空!
if (doEntity == null) {
return null;
}
// 判断DoClass 是否为空
if (dtoClass == null) {
return null;
}
try {
Dto newInstance = dtoClass.newInstance();
BeanUtils.copyProperties(doEntity, newInstance);
// Dto转换Do
return newInstance;
} catch (Exception e) {
return null;
}
}
}
10. BaseApiService 新增 dtoTodoApi
/**
* dto 转换do
*
* @param dtoEntity
* @param doClass
* @param <Do>
* @return
*/
public static <Do> Do dtoToDo(Object dtoEntity, Class<Do> doClass) {
return DemoBeanUtils.dtoToDo(dtoEntity, doClass);
}
/**
* do转换成dto
* @param doEntity
* @param dtoClass
* @param <Dto>
* @return
*/
public static <Dto> Dto doToDto(Object doEntity, Class<Dto> dtoClass) {
return DemoBeanUtils.doToDto(doEntity, dtoClass);
}
public BaseResponse<T> setResultDb(int dbCount, T successMsg, String errorMsg) {
return dbCount > 0 ? setResultSuccess(successMsg) :
setResultError(errorMsg);
}
11. 接口:
@PostMapping("updateUserDto")
BaseResponse<UserRespDto> updateUserDto(@RequestBody UserReqDto userReqDto);
12. Impl
@Override
public BaseResponse<UserRespDto> updateUserDto(UserReqDto userReqDto) {
// 1.验证参数
// 2.userReqDto
// 3.userReqDto属性值复制 userDo
UserDo updateUserDo = doToDto(userReqDto, UserDo.class);
// 4.updateById
int i = userMapper.updateById(updateUserDo);
if (i <= 0) {
return setResultError("修改失败!");
}
// 5.查询db数据
Integer userId = updateUserDo.getUserId();
UserDo getDbUserDo = userMapper.selectById(userId);
if(getDbUserDo == null){
return setResultError("查询数据为空!");
}
// UserDo 转化成dto
UserRespDto userRespDto = doToDto(getDbUserDo, UserRespDto.class);
return setResultSuccess(userRespDto);
}
127.0.0.1:7000/updateUserDto
请求参数:
{
"userId": "1",
"userName": "demo",
"userPwd": "123",
"userAge": "22",
"userAddres": "杭州市",
"userIntegral": "888"
}
响应内容:
{
"code": 200,
"msg": "success",
"data": {
"userId": 1,
"userName": "demo",
"userAge": 22,
"userAddres": "杭州市",
"userIntegral": 888
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/131256.html