• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

mybatis-plus实体json对象的使用

武飞扬头像
一只渣渣
帮助1

1、sql

  1.  
     
  2.  
    create table menu_test
  3.  
    (
  4.  
    id bigint auto_increment comment '主键' primary key,
  5.  
    menu_data json null comment '数据'
  6.  
    ) comment '测试菜单表';

2、对象实体

  1.  
    //autoResultMap = true 自动指定ResultMap
  2.  
    @TableName(value="menu_test",autoResultMap = true)
  3.  
    @AllArgsConstructor
  4.  
    @NoArgsConstructor
  5.  
    @Getter
  6.  
    @Setter
  7.  
    public class MenuTestPO extends Model<MenuTestPO> implements Serializable {
  8.  
    @TableId(value = "id",type = IdType.AUTO)
  9.  
    private Long id;
  10.  
    //指定json解析类型:typeHandler = FastjsonTypeHandler.class
  11.  
    //PS:此处指定只对baseMapper中的方法有效,XML中的方法需要在xml中重新指定typeHandler
  12.  
    @TableField(value="menu_data",typeHandler = FastjsonTypeHandler.class)
  13.  
    private List<Menu> menuData;
  14.  
    }
  15.  
     
  16.  
    @ToString
  17.  
    @Getter
  18.  
    @Api("菜单")
  19.  
    @Setter
  20.  
    public class Menu {
  21.  
    @ApiModelProperty(value = "菜单名称")
  22.  
    private String menuName;
  23.  
    @ApiModelProperty(value = "菜单code")
  24.  
    private String menuCode;
  25.  
    @ApiModelProperty(value = "0:禁用;1:启用;默认:0")
  26.  
    private Integer enable;
  27.  
    @ApiModelProperty(value = "排序1~99,数值越小越靠前")
  28.  
    private Integer sort;
  29.  
    @ApiModelProperty(value = "叶子结点路由标识")
  30.  
    private String routeKey;
  31.  
    @ApiModelProperty(value = "子菜单集合")
  32.  
    private List<Menu> subList;
  33.  
     
  34.  
    public Menu(){
  35.  
    }
  36.  
     
  37.  
    public Menu(String menuCode,Integer enable,List<Menu> subList){
  38.  
    this.menuCode = menuCode;
  39.  
    this.enable = enable;
  40.  
    this.subList = subList;
  41.  
    }
  42.  
     
  43.  
    public Menu(String menuName,String menuCode,Integer enable,Integer sort,String routeKey,List<Menu> subList){
  44.  
    this.menuName = menuName;
  45.  
    this.menuCode = menuCode;
  46.  
    this.enable = enable;
  47.  
    this.sort = sort;
  48.  
    this.routeKey = routeKey;
  49.  
    this.subList = CollectionUtil.isNotEmpty(subList) ? subList : Collections.emptyList();
  50.  
    }
  51.  
     
  52.  
    public Menu(String menuCode,Integer enable) {
  53.  
    this.menuCode = menuCode;
  54.  
    this.enable = enable;
  55.  
    }
  56.  
    }
学新通

3、xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3.  
    <mapper namespace="com.axzo.bidding.service.infrastructure.mapper.MenuTestMapper">
  4.  
     
  5.  
    <resultMap id="menuTest" type="com.axzo.bidding.service.infrastructure.persistence.MenuTestPO">
  6.  
    <id column="id" property="id" ></id>
  7.  
    <!-- 指定解析类型 typeHandler : FastjsonTypeHandler -->
  8.  
    <result column="menu_data" property="menuData" typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler"></result>
  9.  
    </resultMap>
  10.  
    <select id="findById" resultMap="menuTest">
  11.  
    SELECT * FROM menu_test WHERE id = #{id}
  12.  
    </select>
  13.  
    </mapper>

4、调用方法

  1.  
    public interface MenuTestMapper extends BaseMapper<MenuTestPO> {
  2.  
    //PS:BaseMapper中自带的方法会被自动解析成json结构,自定义方法不行,需要在对应的xml中指定属性的解析类型
  3.  
    MenuTestPO findById(@Param("id")Long id);
  4.  
    }
  5.  
     
  6.  
    public interface MenuTestRepository {
  7.  
     
  8.  
    void save(MenuTestPO po);
  9.  
     
  10.  
    MenuTestPO findByid(Long id);
  11.  
    }
  12.  
     
  13.  
    @Slf4j
  14.  
    @Repository
  15.  
    @RequiredArgsConstructor
  16.  
    public class MenuTestRepositoryImpl implements MenuTestRepository {
  17.  
     
  18.  
    private final MenuTestMapper menuTestMapper;
  19.  
     
  20.  
    @Override
  21.  
    public void save(MenuTestPO po) {
  22.  
    //basemapper自带的方法
  23.  
    menuTestMapper.insert(po);
  24.  
    }
  25.  
     
  26.  
    @Override
  27.  
    public MenuTestPO findByid(Long id) {
  28.  
    //自定义的方法
  29.  
    return menuTestMapper.findById(id);
  30.  
    }
  31.  
    }
学新通

这篇好文章是转载于:编程之路

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 编程之路
  • 本文地址: /boutique/detail/tanhfkgjkj
系列文章
更多 icon
同类精品
更多 icon
继续加载