mybatis 一对一association的使用

实际场景应用需求 ,  统计用户某年某月的工资台账,从考勤表中统计出勤天数,反算出日工资   .

 

一 从excel中导入工资台账信息。如  张三 ,2018  01 中国银行 6000元          需要算出出勤日期和每日工资 

 

1、一对一映射

<!--查询 台账记录resultMap结构-->
<resultMap id="wagesResultMap" type="com.imydao.cpsp.wgb.entity.Wages">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="idCard" column="id_card"/>
    <result property="bankCard" column="bank_card"/>
    <result property="bank" column="bank"/>
    <result property="mobile" column="mobile"/>
    <result property="year" column="year"/>
    <result property="month" column="month"/>
    <result property="wages" column="wages"/>
    <result property="engTeamId" column="eng_team_id"/>
    <association property="days" javaType="java.lang.Integer"
                 column="{mobile=mobile,year=year,month=month}" select="signCount"/>
</resultMap>

 2  分页查询工资台账记录sql

 

<select id="selectPageInfo" resultMap="wagesResultMap" parameterType="com.imydao.cpsp.core.common.beans.PageInfo">
    select id, uuid, name, id_card, creator_id, create_time, update_time, status, bank_card,
    bank, mobile, year, month, wages, eng_team_id
    from tb_wages
    where status = 1
    <include refid="whereMapSql"/>
    <if test="whereMap.orderColumns != null">
        order by
        <foreach collection="whereMap.orderColumns" item="orderColumn" index="index"
                 open="" close="" separator=",">
            ${orderColumn} ${orderSort}
        </foreach>
    </if>
    <if test="startRecord != null and pageSize != null">
        limit #{startRecord,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
    </if>
</select>

 

3 查询出勤天数sql

 

<select id="signCount" resultType="java.lang.Integer" parameterType="java.util.Map">
    <include refid="signListSql"/>
</select>
<sql id="signListSql">
     SELECT COUNT(tsr.id) AS days  FROM tb_sign_record tsr LEFT JOIN tb_users tu ON tsr.user_id=tu.id
    WHERE tu.user_status=1 AND tu.telephone_number=#{mobile} AND tsr.sign_in_time IS NOT NULL  AND
    DATE_FORMAT(sign_date,'%Y-%m') =   CONCAT(#{year},'-',IF(#{month}>9,#{month},CONCAT('0',#{month})))
</sql>

 

 

备注: 本文只是mabatis association元素使用的一个小案例。如需深入了解。请参考mybatis官方文档 http://www.mybatis.org/mybatis-3/

 

java分享交流群 110408216  java分享交流群

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值