myBatis association和collection用法实例

Xml代码:
<resultMap id="BaseResultMap" type="com.yc.pojo.Crouse">
    <id column="crouse_id" property="crouseId" jdbcType="BIGINT"/>
    <result column="title" property="title" jdbcType="VARCHAR"/>
    <result column="crouse_code" property="crouseCode" jdbcType="VARCHAR"/>
    <result column="user_id" property="userId" jdbcType="BIGINT"/>
    <result column="max_persons" property="maxPersons" jdbcType="INTEGER"/>
    <result column="price" property="price" jdbcType="DECIMAL"/>
    <result column="crouse_all_times" property="crouseAllTimes" jdbcType="INTEGER"/>
    <result column="create_time" property="createTime" jdbcType="VARCHAR"/>
    <result column="state" property="state" jdbcType="INTEGER"/>
<association property="students" javaType="com.yc.pojo.CrouseStudent"> <!--association用于对象 -->
    <result column="s_id" property="id"
            jdbcType="BIGINT"/>
    <result column="s_user_id" property="userId"
            jdbcType="BIGINT"/>
    <result column="s_notes" property="notes"
            jdbcType="VARCHAR"/>
    <result column="s_state" property="state"
            jdbcType="INTEGER"/>
    <result column="s_user_status" property="userStatus"
            jdbcType="INTEGER"/>
</association>
<collection column="user_id" property="user" ofType="com.yc.pojo.User"> <!--collection用于数组  -->
    <result column="u_email" property="email"
            jdbcType="VARCHAR"/>
    <result column="u_name" property="name"
            jdbcType="VARCHAR"/>
    <result column="u_head_img" property="headImg"
            jdbcType="VARCHAR"/>
    <result column="u_tel" property="tel"
            jdbcType="VARCHAR"/>
    <result column="u_wx_number" property="wxNumber"
            jdbcType="VARCHAR"/>
    <result column="u_sex" property="sex"
            jdbcType="VARCHAR"/>
    <result column="u_create_time" property="createTime"
            jdbcType="VARCHAR"/>
</collection>
</resultMap>
 
<select id="selectByQuery" resultMap="BaseResultMap" parameterType="com.yc.query.CrouseQuery">
        SELECT
        c.*, u.user_id AS u_user_id,
        u.email AS u_email,
        u. NAME AS u_name,
        u.head_img AS u_head_img,
        u.tel AS u_tel,
        u.wx_number AS u_wx_number,
        u.sex AS u_sex,
        s.id AS s_id,
        s.user_id AS s_user_id,
        s.notes AS s_notes,
        s.state AS s_state,
        s.state AS s_state,
        s.user_status AS s_user_status
        FROM
        crouse c
        LEFT JOIN USER u ON u.user_id = c.user_id
        LEFT JOIN crouse_details d ON d.crouse_id = c.crouse_id
        LEFT JOIN crouse_code cc ON c.crouse_id = cc.code_id
        LEFT JOIN crouse_img i ON cc. CODE = c.crouse_code
        LEFT JOIN rating r ON r.crouse_id = c.crouse_id
        LEFT JOIN crouse_student s ON s.crouse_id = c.crouse_id
        <where>c.crouse_id is not null <if test="crouseCode != null">and c.crouseCode LIKE CONCAT('%',
            CONCAT(#{crouseCode}, '%'))
        </if>
            <if test="minPrice != null">and c.price >= #{minPrice}</if>
            <if test="maxPrice != null">and c.price
                <
                #{minPrice}
            </if>
            <if test="maxPersons != null">and c.max_persons<= #{maxPersons}
            </if>
            <if test="minTimes != null">and c.price >= #{minPrice}</if>
            <if test="maxTimes != null">and c.price
                <
                #{maxTimes}
            </if>
            <if test="crouceState == null">and d.state =1</if>
            <if test="address == null">and d.address is not null</if>
            <if test="startTime == null">and d.address is not null</if>
        </where>
    </select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖醋排骨不拿拿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值