mybatis一对多解决element中select下拉框值的填充问题

场景描述:如下,我要做一个下拉框
在这里插入图片描述
客户来源是数据字典中的数据,电话销售、朋友介绍、移动广告是数据字典明细中的数据,前后者关系是一对多;
或许你已经知道了,我只要拿到客户来源,就能根据一对多关系拿到其相关的所有的数据字典明细了

1.先拿到客户来源

                <el-form-item label="客户来源" prop="customerSource">
                    <el-select v-model="addForm.customerSource" placeholder="请选择客户来源">
                        <el-option v-for="(item, index) in customerSourceDic"
                                   :key="index"
                                   :value="item.value"
                                   :label="item.label"></el-option>
                    </el-select>
                </el-form-item>
            /* 后台查询客户来源数据字典返回给customerSourceDic */
            getCusDic() {
                /* 在这里可以将传入的id写死为1 */
                /* 因为查的是数据字典 */
                this.$http.get("/systemDictionary/getDic/1").then(res => {
                    for (let result of res.data) {
                        this.customerSourceDic.push({
                            /* requence就是数据字典明细中的数据字典序号,根据该序号就能找到数据字典明细值 */
                            /* 这里用value标签接收,因为在customer中保存的实际上是数据字典序列,
                               只有查的时候才替换成数据字典明细值 */
                            value: result.requence,
                            label: result.name
                        });
                    }
                });
            },

2.Mapper中
SystemDictionary和SystemDictionaryItem是一对多的关系
SystemDictionaryMapper.xml:

    <!-- 配置一对多 -->
    <!-- 既然是一对多,那么一中某个属性必然要和多中某个属性相匹配,这样一与多才能产生关系,这个属性就是sn -->
    <resultMap id="getDic" type="cn.myllxy.crm.domain.SystemDictionary">
        <result property="id" column="id"/>
        <result property="sn" column="sn"/>
        <result property="intro" column="intro"/>
        <result property="state" column="state"/>
        <collection property="details" javaType="ArrayList"
                    ofType="cn.myllxy.crm.domain.SystemDictionaryItem"
                    column="sn"
                    select="cn.myllxy.crm.mapper.SystemDictionaryItemMapper.selectSystemDictionaryItemByParentSn">
            <id property="id" column="id" javaType="long" jdbcType="BIGINT"/>
            <result property="name" column="name" javaType="string" jdbcType="VARCHAR"/>
            <result property="requence" column="requence" javaType="long" jdbcType="BIGINT"/>
            <result property="intro" column="intro" javaType="string" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>
    <!-- 通过sn来查询数据字典,再查出对应所有数据字典明细 -->
    <select id="selectBySn" resultMap="getDic" parameterType="long">
        SELECT sd.id,sd.name,sd.sn
        FROM t_systemdictionary sd WHERE sn=#{sd.sn}
    </select>

SystemDictionaryItemMapper.xml:

    <resultMap id="SystemDictionaryItemResultMap" type="cn.myllxy.crm.domain.SystemDictionaryItem">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="requence" property="requence"/>
        <result column="intro" property="intro"/>
        <association property="parent" column="parent_sn" javaType="SystemDictionary">
            <id property="id" column="id"/>
            <result property="sn" column="sn"/>
            <result property="intro" column="intro"/>
            <result property="state" column="state"/>
        </association>
    </resultMap>
    <select id="selectSystemDictionaryItemByParentSn" parameterType="long"
            resultMap="SystemDictionaryItemResultMap">
        SELECT * FROM t_systemdictionaryitem WHERE parent_sn = #{sn}
    </select>

3.Controller

    /**
     * 返回数据字典明细实体对象集合用于页面数据显示
     *
     * @param sn
     * @return
     */
    @RequestMapping(value = "/getDic/{sn}", method = RequestMethod.GET)
    @ResponseBody
    public List<SystemDictionaryItem> getDic(@PathVariable("sn") Long sn) {
        SystemDictionary systemDictionary = systemDictionaryService.selectBySn(sn);
        return systemDictionary.getDetails();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot使用MyBatis实现一对多关系,可以通过在实体类定义关联关系,以及在Mapper.xml文件编写SQL语句来实现。以下是一个简单的示例: 假设我们有两个实体类:Order和OrderItem,一个订单可以包含多个订单项。Order类包含一个List<OrderItem>类型的属性,表示该订单包含的所有订单项。我们可以在Order类定义一个方法,使用MyBatis的@Select注解来查询该订单的所有订单项: ```java public class Order { private Long id; private String orderNo; private List<OrderItem> orderItems; // getter和setter方法省略 @Select("SELECT * FROM order_item WHERE order_id = #{id}") public List<OrderItem> getOrderItems() { return orderItems; } } ``` 在Mapper.xml文件,我们可以编写一个查询订单及其所有订单项的SQL语句: ```xml <select id="getOrderById" resultType="Order"> SELECT * FROM orders WHERE id = #{id}; <collection property="orderItems" ofType="OrderItem"> SELECT * FROM order_item WHERE order_id = #{id} </collection> </select> ``` 在这个SQL语句,我们使用了MyBatis的collection元素来定义订单项的集合属性。当MyBatis执行这个SQL语句时,它会自动将查询结果映射到Order对象,并将所有查询到的订单项添加到Order对象的orderItems属性。 在Spring Boot使用MyBatis实现一对多关系,需要在pom.xml文件添加MyBatisMyBatis-Spring的依赖,以及配置MyBatis的SqlSessionFactory和MapperScannerConfigurer。具体的配置方法可以参考官方文档或其他教程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值