Java Code Examples for cn.hutool.core.bean.BeanUtil#beanToMap()

The following examples show how to use cn.hutool.core.bean.BeanUtil#beanToMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HutoolController.java    From mall-learning with Apache License 2.0 6 votes vote down vote up
@ApiOperation("BeanUtil使用:JavaBean的工具类")
@GetMapping("/beanUtil")
public CommonResult beanUtil() {
    PmsBrand brand = new PmsBrand();
    brand.setId(1L);
    brand.setName("小米");
    brand.setShowStatus(0);
    //Bean转Map
    Map<String, Object> map = BeanUtil.beanToMap(brand);
    LOGGER.info("beanUtil bean to map:{}", map);
    //Map转Bean
    PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false);
    LOGGER.info("beanUtil map to bean:{}", mapBrand);
    //Bean属性拷贝
    PmsBrand copyBrand = new PmsBrand();
    BeanUtil.copyProperties(brand, copyBrand);
    LOGGER.info("beanUtil copy properties:{}", copyBrand);
    return CommonResult.success(null, "操作成功");
}
 
Example 2
Source File: RestRoleController.java    From Guns with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 查看角色
 *
 * @author fengshuonan
 * @Date 2018/12/23 6:31 PM
 */
@RequestMapping(value = "/view/{roleId}")
public ResponseData view(@PathVariable Long roleId) {
    if (ToolUtil.isEmpty(roleId)) {
        throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
    }
    RestRole role = this.restRoleService.getById(roleId);
    Map<String, Object> roleMap = BeanUtil.beanToMap(role);

    Long pid = role.getPid();
    String pName = ConstantFactory.me().getSingleRoleName(pid);
    roleMap.put("pName", pName);

    //获取角色绑定的菜单
    List<Long> menuIds = this.restMenuService.getMenuIdsByRoleId(roleId);
    List<String> menuNames = this.restMenuService.getBaseMapper().getMenuNamesByRoleId(roleId);
    if (ToolUtil.isNotEmpty(menuIds)) {
        roleMap.put("menuIds", menuIds);
        roleMap.put("menuNames", menuNames);
    } else {
        roleMap.put("menuIds", new ArrayList<>());
        roleMap.put("menuNames", new ArrayList<>());
    }

    return ResponseData.success(roleMap);
}
 
Example 3
Source File: RestDictService.java    From Guns with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 查询字典列表,通过字典类型code
 *
 * @author fengshuonan
 * @Date 2019-06-20 15:14
 */
public List<Map<String, Object>> getDictsByCodes(List<String> dictCodes) {

    QueryWrapper<RestDict> wrapper = new QueryWrapper<>();
    wrapper.in("code", dictCodes).orderByAsc("sort");

    ArrayList<Map<String, Object>> results = new ArrayList<>();

    //转成map
    List<RestDict> list = this.list(wrapper);
    for (RestDict dict : list) {
        Map<String, Object> map = BeanUtil.beanToMap(dict);
        results.add(map);
    }

    return results;
}
 
Example 4
Source File: RoleController.java    From Guns with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 查看角色
 *
 * @author fengshuonan
 * @Date 2018/12/23 6:31 PM
 */
@RequestMapping(value = "/view/{roleId}")
@ResponseBody
public ResponseData view(@PathVariable Long roleId) {
    if (ToolUtil.isEmpty(roleId)) {
        throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
    }
    Role role = this.roleService.getById(roleId);
    Map<String, Object> roleMap = BeanUtil.beanToMap(role);

    Long pid = role.getPid();
    String pName = ConstantFactory.me().getSingleRoleName(pid);
    roleMap.put("pName", pName);

    return ResponseData.success(roleMap);
}
 
Example 5
Source File: DictService.java    From Guns with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 查询字典列表,通过字典类型code
 *
 * @author fengshuonan
 * @Date 2019-06-20 15:14
 */
public List<Map<String, Object>> getDictsByCodes(List<String> dictCodes) {

    QueryWrapper<Dict> wrapper = new QueryWrapper<>();
    wrapper.in("code", dictCodes).orderByAsc("sort");

    ArrayList<Map<String, Object>> results = new ArrayList<>();

    //转成map
    List<Dict> list = this.list(wrapper);
    for (Dict dict : list) {
        Map<String, Object> map = BeanUtil.beanToMap(dict);
        results.add(map);
    }

    return results;
}
 
Example 6
Source File: MenuController.java    From WebStack-Guns with MIT License 5 votes vote down vote up
/**
 * 跳转到菜单详情列表页面
 */
@Permission(Const.ADMIN_NAME)
@RequestMapping(value = "/menu_edit/{menuId}")
public String menuEdit(@PathVariable Long menuId, Model model) {
    if (ToolUtil.isEmpty(menuId)) {
        throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
    }
    Menu menu = this.menuService.selectById(menuId);

    //获取父级菜单的id
    Menu temp = new Menu();
    temp.setCode(menu.getPcode());
    Menu pMenu = this.menuService.selectOne(new EntityWrapper<>(temp));

    //如果父级是顶级菜单
    if (pMenu == null) {
        menu.setPcode("0");
    } else {
        //设置父级菜单的code为父级菜单的id
        menu.setPcode(String.valueOf(pMenu.getId()));
    }

    Map<String, Object> menuMap = BeanUtil.beanToMap(menu);
    menuMap.put("pcodeName", ConstantFactory.me().getMenuNameByCode(temp.getCode()));
    model.addAttribute("menu", menuMap);
    LogObjectHolder.me().set(menu);
    return PREFIX + "menu_edit.html";
}
 
Example 7
Source File: LogController.java    From WebStack-Guns with MIT License 5 votes vote down vote up
/**
 * 查询操作日志详情
 */
@RequestMapping("/detail/{id}")
@Permission(Const.ADMIN_NAME)
@ResponseBody
public Object detail(@PathVariable Integer id) {
    OperationLog operationLog = operationLogService.selectById(id);
    Map<String, Object> stringObjectMap = BeanUtil.beanToMap(operationLog);
    return super.warpObject(new LogWarpper(stringObjectMap));
}
 
Example 8
Source File: TestResource.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void testJson() throws Exception {
    User u = new User().setSex(Sex.M).setName("123");
    User user = BeanUtil.toBean(u, User.class);

    Map<String, Object> stringObjectMap = BeanUtil.beanToMap(u);
    String s = objectMapper.writeValueAsString(u);
    User user1 = objectMapper.readValue(s, User.class);
    System.out.println(user);
}
 
Example 9
Source File: TestResource.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testJson() throws Exception {
    User u = new User().setSex(Sex.M).setName("123");
    User user = BeanUtil.toBean(u, User.class);

    Map<String, Object> stringObjectMap = BeanUtil.beanToMap(u);
    String s = objectMapper.writeValueAsString(u);
    User user1 = objectMapper.readValue(s, User.class);
    System.out.println(user);
}
 
Example 10
Source File: RestLogController.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 查询操作日志详情
 *
 * @author fengshuonan
 * @Date 2018/12/23 5:34 PM
 */
@RequestMapping("/detail/{id}")
public Map<String, Object> detail(@PathVariable Long id) {
    RestOperationLog operationLog = restOperationLogService.getById(id);
    Map<String, Object> stringObjectMap = BeanUtil.beanToMap(operationLog);
    return new LogWrapper(stringObjectMap).wrap();
}
 
Example 11
Source File: RestUserFactory.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 过滤不安全字段并转化为map
 */
public static Map<String, Object> removeUnSafeFieldsRest(RestUser user) {
    if (user == null) {
        return new HashMap<>();
    } else {
        Map<String, Object> map = BeanUtil.beanToMap(user);
        map.remove("password");
        map.remove("salt");
        map.put("birthday", DateUtil.formatDate(user.getBirthday()));
        return map;
    }
}
 
Example 12
Source File: LogController.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 查询操作日志详情
 *
 * @author fengshuonan
 * @Date 2018/12/23 5:34 PM
 */
@RequestMapping("/detail/{id}")
@Permission(Const.ADMIN_NAME)
@ResponseBody
public Object detail(@PathVariable Long id) {
    OperationLog operationLog = operationLogService.getById(id);
    Map<String, Object> stringObjectMap = BeanUtil.beanToMap(operationLog);
    return super.warpObject(new LogWrapper(stringObjectMap));
}
 
Example 13
Source File: UserFactory.java    From Guns with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 过滤不安全字段并转化为map
 */
public static Map<String, Object> removeUnSafeFields(User user) {
    if (user == null) {
        return new HashMap<>();
    } else {
        Map<String, Object> map = BeanUtil.beanToMap(user);
        map.remove("password");
        map.remove("salt");
        map.put("birthday", DateUtil.formatDate(user.getBirthday()));
        return map;
    }
}
 
Example 14
Source File: Record.java    From Aooms with Apache License 2.0 4 votes vote down vote up
public <T> Record fromBean(T bean){
    return (Record) BeanUtil.beanToMap(bean,this,true,true);
}