mybatis基础增删改查(动态sql).xml

本文介绍了一个基于MyBatis实现的员工信息管理系统,包括员工基本信息、值班记录的增删改查操作,以及条件查询和模糊查询功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BasePersonInfo.java

public class BasePersonInfo extends BaseEntity  implements Serializable,Cloneable{
    /** 人员ID */
    private Integer personId ;
    /** 姓名 */
    private String personName ;
    /** 性别;1:女;2:男 */
    private Integer sex ;
    /** 手机号 */
    private String mobilePhone ;
    /** 办公电话 */
    private String officePhone ;
    /** 处室ID */
    private Integer officeId ;
    /** 职务;1.领导,2.处长,3.副处长 */
    private Integer post ;
    /** 状态;1:可排班;2:不可排班 */
    private Integer status ;
    //一对多,1个员工可能有多条值班记录。
    private List<DutyRecord> dutyRecord;

PersonMapper.XML 

<mapper namespace="com.ewebtd.mapper.PersonMapper">
    <resultMap type="com.ewebtd.entities.BasePersonInfo" id="BasePersonInfoResult">
        <id     property="personId"       column="PERSON_ID"      />
        <result property="personName"     column="PERSON_NAME"    />
        <result property="sex"    column="SEX"   />
        <result property="mobilePhone"     column="MOBILE_PHONE"     />
        <result property="officePhone"     column="OFFICE_PHONE"    />
        <result property="officeId"    column="OFFICE_ID"   />
        <result property="post"     column="POST"     />
        <result property="status"     column="STATUS"     />
        <!--javatype指定的是user对象的属性的类型(例如id,posts),而oftype指定的是映射到list集合属性中pojo的类型(本例指的是post类型)-->
        <collection property="dutyRecord" ofType="com.ewebtd.entities.DutyRecord">
            <id     property="recordId"       column="RECORD_ID"      />
            <result property="groupId"     column="GROUP_ID"    />
            <result property="recordDate"    column="RECORD_DATE"   />
            <result property="period"     column="PERIOD"     />
            <result property="personId"     column="PERSON_ID"     />
        </collection>
    </resultMap>
    <!-- useGeneratedKeys设置为"true"表明要MyBatis获取由数据库自动生成的主键;keyProperty="personId"指定把获取到的主键值注入到类的id属性 useGeneratedKeys=true-->
    <insert id="addPerson"  parameterType="com.ewebtd.entities.BasePersonInfo" keyProperty="personId">
        insert into base_person_info(PERSON_NAME,SEX,MOBILE_PHONE,OFFICE_PHONE,OFFICE_ID,POST,STATUS) values
         (#{personName},#{sex},#{mobilePhone},#{officePhone},#{officeId},#{post},#{status})
    </insert>
    <!--查询所有员工-->
    <select id="selectPersons" resultMap="BasePersonInfoResult">
        select * from base_person_info
    </select>
    <!--通过员工id查询一个员工-->
    <select id="selectPersonById" parameterType="Integer" resultMap="BasePersonInfoResult">
        select * from base_person_info where PERSON_ID=#{personId}
    </select>
    <!--通过员工id查询一个员工记录-->
    <select id="selectPersonDutyRecordsById" parameterType="Integer" resultMap="BasePersonInfoResult">
        select r.*,p.* from duty_record r,base_person_info p where p.PERSON_ID=#{personId} and p.PERSON_ID=r.PERSON_ID
    </select>
    <!--条件查询-->
    <select id="selectPersonByAllParams" parameterType="com.ewebtd.entities.BasePersonInfo" resultMap="BasePersonInfoResult">
        select * from base_person_info
        <where> 1=1
            <if test="personId !=null">
                AND PERSON_ID = #{personId}
            </if>
            <if test="personName !=null and personName !=''">
                AND PERSON_NAME = #{personName}
            </if>
            <if test="sex !=null">
                AND SEX = #{sex}
            </if>
            <if test="mobilePhone !=null and mobilePhone !=''">
                AND MOBILE_PHONE = #{mobilePhone}
            </if>
            <if test="officePhone !=null and officePhone !=''">
                AND OFFICE_PHONE = #{officePhone}
            </if>
            <if test="officeId !=null">
                AND OFFICE_ID = #{officeId}
            </if>
            <if test="post !=null">
                AND POST = #{post}
            </if>
            <if test="status !=null">
                AND STATUS = #{status}
            </if>
        </where>
    </select>
    <!--模糊查询-->
    <select id="selectPersonsByStr" parameterType="String" resultMap="BasePersonInfoResult">
        select * from base_person_info where PERSON_NAME like '%${str}%'
        <if test="str=='领导'.toString">
            or POST = 1
        </if>
        <if test="str=='处长'.toString">
            or POST = 2
        </if>
        <if test="str=='副处长'.toString">
            or POST = 3
        </if>
        <if test="str=='男'.toString">
            or SEX = 2
        </if>
        <if test="str=='女'.toString">
            or SEX = 1
        </if>
        <if test="str=='可排班'.toString">
            or STATUS = 1
        </if>
        <if test="str=='不可排班'.toString">
            or STATUS = 2
        </if>
    </select>
    <!--更新一个员工信息-->
    <update id="updatePerson" parameterType="com.ewebtd.entities.BasePersonInfo">
        update base_person_info
        <set>
            <if test="personName!=null">
                PERSON_NAME=#{personName},
            </if>
            <if test="sex!=null">
                SEX=#{sex},
            </if>
            <if test="mobilePhone!=null">
                MOBILE_PHONE=#{mobilePhone},
            </if>
            <if test="officePhone!=null">
                OFFICE_PHONE=#{officePhone},
            </if>
            <if test="officeId!=null">
                OFFICE_ID=#{officeId},
            </if>
            <if test="post!=null">
                POST=#{post},
            </if>
            <if test="status!=null">
                STATUS=#{status}
            </if>
        </set>
         where PERSON_ID=#{personId}
    </update>
    <!--更新一个员工的状态-->
    <update id="updatePersonById" parameterType="Integer">
        update base_person_info set
        STATUS=#{status}
        where PERSON_ID=#{personId}
    </update>
    <!--通过id删除一个员工-->
    <delete id="deletePerson" parameterType="Integer">
        delete from base_person_info where PERSON_ID=#{personId}
    </delete>
    <!--通过id集合批量删除-->
    <delete id="deleteByBatch">
        delete from base_person_info
        where PERSON_ID IN
        <foreach collection="personIds" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </delete>
</mapper>

DutyRecord .java

public class DutyRecord extends BaseEntity  implements Serializable,Cloneable{
    /** 值班记录ID */
    private Integer recordId ;
    /** 值班组ID */
    private Integer groupId ;
    /** 值班日期 */
    private Date recordDate ;
    /** 值班时段 */
    private Integer period ;
    /** 人员ID */
    private Integer personId ;
    //多对一
    private BasePersonInfo basePersonInfo;

DutyRecordMapper.XML

<mapper namespace="com.ewebtd.mapper.DutyRecordMapper">
    <resultMap type="com.ewebtd.entities.DutyRecord" id="DutyRecordResult">
        <id     property="recordId"       column="RECORD_ID"      />
        <result property="groupId"     column="GROUP_ID"    />
        <result property="recordDate"    column="RECORD_DATE"   />
        <result property="period"     column="PERIOD"     />
        <result property="personId"     column="PERSON_ID"     />
        <association property="basePersonInfo" javaType="com.ewebtd.entities.BasePersonInfo">
            <id     property="personId"       column="PERSON_ID"      />
            <result property="personName"     column="PERSON_NAME"    />
            <result property="sex"    column="SEX"   />
            <result property="mobilePhone"     column="MOBILE_PHONE"     />
            <result property="officePhone"     column="OFFICE_PHONE"    />
            <result property="officeId"    column="OFFICE_ID"   />
            <result property="post"     column="POST"     />
            <result property="status"     column="STATUS"     />
        </association>
    </resultMap>
    <!--通过员工id查询值班记录-->
    <select id="selectDutyRecordByPersonId"  parameterType="com.ewebtd.entities.BasePersonInfo" resultMap="DutyRecordResult">
        select r.*,p.* from duty_record r,base_person_info p where r.PERSON_ID=#{personId} and p.PERSON_ID=r.PERSON_ID
    </select>


</mapper>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值