最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中VO/DTO/PO的具體使用

 更新時間:2024年03月17日 09:22:03   作者:tang_seven  
VO/DTO/PO等實體類中字段常常會存在多數(shù)相同,根據(jù)業(yè)務(wù)需求少數(shù)不同,本文主要介紹了SpringBoot中VO/DTO/PO的具體使用,感興趣的可以了解一下

1.概念敘述

  • VO:View Object,主要用于展示層。它的作用是把某個指定前端頁面的所有數(shù)據(jù)封裝起來。他的作用主要是減少傳輸數(shù)據(jù)量大小和保護數(shù)據(jù)庫隱私數(shù)據(jù)(如用戶密碼、用戶郵箱等相關(guān)信息)不外泄,同時保護數(shù)據(jù)庫的結(jié)構(gòu)不外泄。
  • DTO:Data Transfer Object,數(shù)據(jù)傳輸對象,用于展示層與服務(wù)層之間的數(shù)據(jù)傳輸對象。(注:實際開發(fā)中還存在BO,其作用和DTO類似,當(dāng)業(yè)務(wù)邏輯不復(fù)雜時一般會被合并。)
  • PO:Persistant Object,持久化對象,和數(shù)據(jù)庫形成映射關(guān)系。簡單說PO就是每一個數(shù)據(jù)庫中的數(shù)據(jù)表,一個字段對應(yīng)PO中的一個變量。(也就是我們常用的Entities)

幾者之間的關(guān)系如下圖:

從前端頁面中收到JSON格式數(shù)據(jù),后端接口中將其封裝為一個VO對象;接口接收到VO對象后將其轉(zhuǎn)換為DTO對象,并調(diào)用業(yè)務(wù)類方法對其進行處理;然后處理為PO對象,調(diào)用Dao接口連接數(shù)據(jù)庫進行數(shù)據(jù)訪問(查詢、插入、更新等)。

后端從數(shù)據(jù)庫得到結(jié)果后,根據(jù)Dao接口將結(jié)果映射為PO對象,然后調(diào)用業(yè)務(wù)類方法將其轉(zhuǎn)換為需要的DTO對象,再根據(jù)前端頁面實際需求,轉(zhuǎn)換為VO對象進行返回。

2.類型轉(zhuǎn)換

上述過程中,VO/DTO/PO等實體類中字段常常會存在多數(shù)相同,根據(jù)業(yè)務(wù)需求少數(shù)不同。為避免頻繁的set和get操作對其進行轉(zhuǎn)換,spring為我們提供了多種方法。

(1)使用BeanUtils:(springframework包下)

UserDto user = new UserDto ();
BeanUtils.copyProperties(userInfo ,user ,new String[]{"birthday"});

上述代碼中,意思是將左邊UserInfo實體類(可以視為一個VO對象)和UserDto實體類類(可以視為一個DTO對象)中一樣的值進行賦值user對象中,new String[]{""}中是跳過賦值的字段,該屬性可以為空。

(2)使用BeanUtils:(Apache包下)

UserDto user = new UserDto ();
BeanUtils.copyProperties(user,userInfo);

上述代碼中,意思是將右邊UserInfo實體類(可以視為一個VO對象)和UserDto實體類類(可以視為一個DTO對象)中一樣的值進行賦值user對象中。(和spring包下方法相反)

(3)使用modelMapper:

導(dǎo)入依賴:

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.9</version>
        </dependency>

配置文件:

@Configuration
public class ModelMapperConfig {

    private Converter<Date, String> dateToStringConverter = new AbstractConverter<Date, String>() {
        @Override
        protected String convert(Date date) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            return date == null ? null : simpleDateFormat.format(date);
        }
    };

    @Bean
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();

        // 官方配置說明: http://modelmapper.org/user-manual/configuration/
        // 完全匹配
        modelMapper.getConfiguration().setFullTypeMatchingRequired(true);

        // 匹配策略使用嚴(yán)格模式
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

        modelMapper.addConverter(dateToStringConverter);

        configureUser(modelMapper);

        return modelMapper;
    }

    private void configureUser(ModelMapper modelMapper) {
        //將dto轉(zhuǎn)為UserAccount實體類
        modelMapper.typeMap(UserInfor.class, UserAccount.class)
                .addMappings(mapper -> mapper.map(UserInfor::getUsername, UserAccount::setUsername))
                .addMappings(mapper -> mapper.map(UserInfor::getPassword, UserAccount::setPassword));

}

使用:

UserAccount account= modelMapper.map(userInfo,UserAccount.class);

上述代碼,根據(jù)配置文件中configureUser方法中配置的字段,將兩個實體類中的字段進行復(fù)制賦值。

3.使用實例

下面我們模擬一個業(yè)務(wù):后端接口從前端中接收到用戶注冊數(shù)據(jù),進行注冊;前端調(diào)用查詢接口,獲取用戶的數(shù)據(jù)。

首先,我們定義VO、DTO、PO對象:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfo implements Serializable {
    //該實體類為封裝好的前端傳輸頁面VO對象
    @NotBlank(message = "用戶名不能為空")
    private String Username;
    @NotBlank(message = "密碼不能為空")
    private String Password;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserDTO implements Serializable {
    //該實體類為業(yè)務(wù)處理DTO對象
    private int Account;
    private String Username;
    private String Password;
    private String Roles;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user_account")
public class UserAccount implements Serializable {
    //該實體類為用戶賬號表對應(yīng)PO對象
    @TableId
    private int SerialNum;
    private int Account;
    private String Username;
    private String Password;
    private String Roles;
    private String status;
    private String registerDate;
}

然后,我們編寫用戶注冊相關(guān)代碼(基于springboot+mybatis plus),由于使用mybatis plus,此處省略mapper層代碼,僅展示serviceImpl和controller中代碼。

控制層:

    //用戶注冊
    @PostMapping("/register")
    public CommonResult<Object> userRegister(@RequestBody @Valid UserInfo user){
        int result = userService.userRegister(user);
        if(result!=0){
            return CommonResult.success(result);
        }else {
            return CommonResult.fail(result);
        }
    }

業(yè)務(wù)處理:

    @Override
    public int userRegister(UserInfo user) {
        if(accountMapper.selectCount(new QueryWrapper<UserAccount>().eq("username",user.getUsername()))!=0){
            log.info("用戶名已存在");
            return 0;
        }
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userDTO,user);
        //加密密碼
        Md5Hash md5Hash = new Md5Hash(userDTO.getPassword(), userDTO.getUsername(),2);
        userDTO.setPassword(md5Hash.toString());
        //模擬生成賬號
        userDTO.setAccount(12345678);
        //賦予權(quán)限
        userDTO.setRoles("user");
        return accountMapper.insert(userDTO);
    }

最終,我們可以往數(shù)據(jù)庫中傳入一條用戶剛注冊的賬號數(shù)據(jù),status和RegisterDate等屬性取數(shù)據(jù)庫設(shè)置好的默認值。

然后,我們再定義一個VO用于展示用戶的信息:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserVo implements Serializable {
    //該VO用于展示用戶信息,去除了用戶密碼等敏感信息
    private int serialNum;
    private int account;
    private String username;
}

進行用戶信息的查詢,獲取所有的用戶賬號:

業(yè)務(wù)層:

    public List<UserAccount> getAllUser() {
        return accountMapper.selectList(null);
    }

控制層:

    @GetMapping("/get/all")
    public CommonResult<Object> getAllAccount() throws InvocationTargetException, IllegalAccessException {
        //查詢所有數(shù)據(jù)
        List<UserAccount> userAccountList = userService.getAllUser();
        List<AdminInfo> resultList = new ArrayList<>();
        //封裝為VO進行展示
        for(UserAccount u:userAccountList){
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(userVo,u);
            resultList.add(userVo);
        }
        //根據(jù)序列號進行排序,后注冊的放在最前面
        resultList.sort(new Comparator<UserVo>() {
            @Override
            public int compare(UserVo o1, UserVo o2) {
                return o2.getSerialNum() - o1.getSerialNum();
            }
        });
        return new CommonResult<>(200,"賬號列表如下:",resultList,resultList.size());
    }

然后我們就可查詢得到所有的去除了敏感信息可用于展示的用戶賬號信息。

注:此處由于業(yè)務(wù)簡單,沒有將PO對象轉(zhuǎn)換為DTO再轉(zhuǎn)換為VO。

到此這篇關(guān)于SpringBoot中VO/DTO/PO的具體使用的文章就介紹到這了,更多相關(guān)SpringBoot VO/DTO/PO內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

南宁市| 渝中区| 合川市| 安泽县| 满城县| 凤山市| 合水县| 罗甸县| 闵行区| 红安县| 鄂托克旗| 唐山市| 文山县| 方正县| 镇巴县| 卢龙县| 南充市| 观塘区| 观塘区| 苏尼特左旗| 平昌县| 镇康县| 贺州市| 寿光市| 那曲县| 瓮安县| 绿春县| 嘉荫县| 托克托县| 彩票| 收藏| 温宿县| 张家港市| 龙州县| 浠水县| 黔东| 马边| 弋阳县| 贵德县| 琼结县| 元江|