mybatis多表关联的问题及处理

今天在多表相关联里面碰到的问题。

主要做的是容器里面能关联到应用系统,就是在添加容器的时候能选择相应的应用,然后能在容器的数据库里面有相应的增加

  • 首先,要实现两个表之间相互关联,因为本项目里面使用的是mybatis框架来处理数据,所以对应的mapper.xml里面要进行相应的关联,这个里面要注意一点就是不要滥用mybatis里面的标签。这个标签的主要目的是为了节省代码,但是在两个表之间相互关联的时候需要对表之间的相同的数据之间进行查找,使用这个标签的话要注意后面的查询语句之间的区别。

    <resultMap type="ContainerInfo" id="ContainerInfoResult">
            <result property="containerId"    column="container_id"    />
            <result property="containerName"    column="container_name"    />
            <result property="signKeyInfoId"    column="sign_key_info_id"    />
            <result property="encKeyInfoId"    column="enc_key_info_id"    />
            <result property="appId" column="app_id"/>
            <association property="appInfo" column="app_id" javaType="AppInfo" resultMap="appInfo"></association>
        </resultMap>
    
        <resultMap id="appInfo" type="AppInfo">
            <result property="appId"    column="app_id"    />
            <result property="appName"    column="app_name"    />
            <result property="appCode"    column="app_code"    />
            <result property="appKey"    column="app_key"    />
            <result property="appCert"    column="app_cert"    />
            <result property="appUrl"    column="app_url"    />
            <result property="appStatus"    column="app_status"    />
            <result property="appDesc"    column="app_desc"    />
            <result property="createBy"    column="create_by"    />
            <result property="createTime"    column="create_time"    />
            <result property="updateBy"    column="update_by"    />
            <result property="updateTime"    column="update_time"    />
            <result property="remark"    column="remark"    />
        </resultMap>
    
        <sql id="selectContainerInfoVo">
            select container_id, container_name, sign_key_info_id, enc_key_info_id, app_id from sign_container_info
        </sql>
    
        <select id="selectContainerInfoList" parameterType="ContainerInfo" resultMap="ContainerInfoResult">
            select a.container_id, a.container_name, a.sign_key_info_id, a.enc_key_info_id, a.app_id,
            b.app_name
            from sign_container_info a
            left join base_app_info b on a.app_id = b.app_id
            <where>
                <if test="appId != null "> and app_id = #{appId}</if>
                <if test="containerId != null "> and container_id = #{containerId}</if>
                <if test="containerName != null  and containerName != '' "> and container_name = #{containerName}</if>
                <if test="signKeyInfoId != null "> and sign_key_info_id = #{signKeyInfoId}</if>
                <if test="encKeyInfoId != null "> and enc_key_info_id = #{encKeyInfoId}</if>
            </where>
            order by container_id desc
        </select>
    
        <select id="selectContainerInfoById" parameterType="Integer" resultMap="ContainerInfoResult">
            <include refid="selectContainerInfoVo"/>
            select a.container_id, a.container_name, a.sign_key_info_id, a.enc_key_info_id, a.app_id,
            b.app_name
            from sign_container_info a
            left join base_app_info b on a.app_id = b.app_id
            where container_id = #{containerId}
        </select>
    
  • 既然对应的mapper.xml写完了,那就能对数据进行处理,因为数据都已经关联好了,相应的service和mapper层都和正常一样写就好,本次着重要强调的是对应的controller里面的处理。因为controller层才是对页面上的请求进行处理的。所以在这个里面得新建对应的实体类,然后利用springmvc里面的东西将对应的其他表里的数据放在对应的页面里面。

     /**
         * 新增密钥容器
         */
        @GetMapping("/add")
        public String add(ModelMap mmap) {
            AppInfo appInfo = new AppInfo();
            appInfo.setAppStatus(String.valueOf(DictionaryConstant.STATUS_OK));
            List<KeyInfo> keyInfos = keyInfoService.selectUsableList();
            mmap.put("keyInfos", keyInfos);
            List<AppInfo> appList = appInfoService.selectAppInfoList(appInfo);
            mmap.put("appList", appList);
            return prefix + "/add";
        }
    

所以这个容器关联业务系统就样处理了,想清楚了该做的事情就会清晰。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值