myBatis --(3)數據的增刪改查

上一篇文章《接口方式編程》中講述的是返回單個對象(包括對象、整型、字符串)等等,這篇文章講述的是返回一個list。
java

查詢單個對象的返回能夠採用resultType,例如
數據庫

1>返回是一個對象的,這個時候,數據庫查詢到的數據:字段命名別名的時候要和接收對象中的屬性名稱要一一對應起來,不然會出現數據庫查詢獲得數據,可是顯示出來的卻爲null的現象。
編程

    <select id="getJobFocus" resultType="com.zhiji.caren.VO.JobFocus">
        select
        h.JOB_ID as jobId,
        j.JOB_NAME as jobName,
        j.ENTERPRISE_ID as enterpriseId,
        j.SALARY_MIN as salaryMin,
        j.SALARY_MAX as salaryMax,
        j.WORK_YEARS_ID as workYearsId,
        j.LOCATION_ID as locationId,
        tt.LOCATION_NAME as locationName,
        e.ENTERPRISE_NAME as enterpriseName,
        e.ENTERPRISE_LOGO as enterpriseLogo,
        j.NEGOTIABLE_FLAG as negotiableFlag,
        w.WORK_YEARS_INFO as workYearsInfo,
        h.ADD_TIME as addTime
        FROM
        T_USER_JOB_FOCUS h
        LEFT JOIN
        T_JOB j
        on
        j.JOB_ID=h.JOB_ID
        LEFT JOIN
        T_ENTERPRISE e
        on
        e.ENTERPRISE_ID = j.ENTERPRISE_ID
        LEFT JOIN
        T_WORK_YEARS w
        on
        w.WORK_YEARS_ID = j.WORK_YEARS_ID
        LEFT JOIN T_LOCATION
        tt
        on
        tt.LOCATION_ID = j.LOCATION_ID
        where
        h.USER_ID=#{uesrId}
        <if test="lastTime != null">
            and h.ADD_TIME
            &lt;=#{lastTime}
        </if>
        order by h.ADD_TIME desc
        limit 0,#{pageIndex}
    </select>
    
    返回對象爲
    public class JobFocus {
    /** 職位id */
    private String jobId;
    /** 職位名稱 */
    private String jobName;
    /** 企業id */
    private String enterpriseId;
    /** 企業名稱 */
    private String enterpriseName;
    /** 企業LogoURL */
    private String enterpriseLogo;
    /** 薪資類別 */
    private String negotiableFlag;
    /** 薪水最小值 */
    private String salaryMin;
    /** 薪水最大值 */
    private String salaryMax;
    /** 工做地點id */
    private String locationId;
    /** 工做地點名稱 */
    private String locationName;
    /** 工做經驗id */
    private String workYearsId;
    /** 工做經驗信息 */
    private String workYearsInfo;
    /** 添加時間*/
    private String addTime;
    
    //此處省略屬性的get與set方法
    }

2>返回是String類型的ui

    <select id="getJobTypeUpdTime" resultType="java.lang.String">
        SELECT MAX(UPD_TIME) FROM T_JOB_TYPE limit 0,1
    </select>

3>返回是整型類型的code

    <select id="selectOprNum" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM
        t_user_enterprise_oper tueo
        WHERE
        tueo.ENTERPRISE_ID = #{objectId}
        AND
        tueo.OPER_TYPE = #{type}
    </select>


1、這篇講述的是返回的是一個list,用resultMap方式來接收轉換xml

接收的resultMap對象

    <resultMap id="BaseResultMapJobList" type="com.zhiji.caren.VO.JobList">
        <result column="JOB_ID" jdbcType="NUMERIC" property="jobId" />
        <result column="JOB_NAME" jdbcType="VARCHAR" property="jobName" />
        <result column="ENTERPRISE_ID" jdbcType="NUMERIC" property="enterpriseId" />
        <result column="ENTERPRISE_NAME" jdbcType="VARCHAR" property="enterpriseName" />
        <result column="ENTERPRISE_LOGO" jdbcType="VARCHAR" property="enterpriseLogo" />
        <result column="NEGOTIABLE_FLAG" jdbcType="NUMERIC" property="negotiableFlag" />
        <result column="SALARY_MIN" jdbcType="NUMERIC" property="salaryMin" />
        <result column="SALARY_MAX" jdbcType="NUMERIC" property="salaryMax" />
        <result column="LOCATION_ID" jdbcType="NUMERIC" property="locationId" />
        <result column="LOCATION_NAME" jdbcType="VARCHAR" property="locationName" />
        <result column="WORK_YEARS_ID" jdbcType="NUMERIC" property="workYearsId" />
        <result column="WORK_YEARS_INFO" jdbcType="VARCHAR" property="workYearsInfo" />
        <result column="ADD_TIME" jdbcType="VARCHAR" property="addTime" />
        <result column="flag" jdbcType="VARCHAR" property="flag" />
    </resultMap>

查詢的SQL語句接口

    <select id="getJobListInfo" parameterType="com.zhiji.caren.VO.JobParams"
        resultMap="BaseResultMapJobList">

        SELECT
        tj.JOB_ID,
        tj.LOCATION_ID,
        tl.LOCATION_NAME,
        tj.JOB_NAME,
        tj.NEGOTIABLE_FLAG,
        tj.SALARY_MAX,
        tj.SALARY_MIN,
        tj.ADD_TIME,
        tj.ENTERPRISE_ID,
        te.ENTERPRISE_NAME,
        te.ENTERPRISE_LOGO,
        twy.WORK_YEARS_ID,
        twy.WORK_YEARS_INFO,
        '' AS flag
        FROM
        t_job
        tj,t_location tl,t_enterprise te,t_hr th,t_work_years
        twy,t_job_category tjc
        WHERE tj.LOCATION_ID = tl.LOCATION_ID
        AND
        tl.LOCATION_LEVEL = 2
        AND tl.USE_FLAG = 1
        AND tj.ENTERPRISE_ID =
        te.ENTERPRISE_ID
        AND te.USE_FLAG = 1
        AND tj.HR_ID = th.HR_ID
        AND
        th.USE_FLAG = 1
        AND tj.WORK_YEARS_ID = twy.WORK_YEARS_ID
        AND
        tj.JOB_CATEGORY_ID = tjc.JOB_CATEGORY_ID
        AND tjc.USE_FLAG = 1
        <if test="jobCategoryId != null">
            AND
            tjc.JOB_CATEGORY_ID =
            #{jobCategoryId}
        </if>
        <if test="locationId != null">
            AND
            tl.LOCATION_ID = #{locationId}
        </if>
        <if test="workYearId != null">
            AND
            twy.WORK_YEARS_ID = #{workYearId}
        </if>
        <if test="enterpriseId != null">
            AND
            tj.ENTERPRISE_ID = #{enterpriseId}
        </if>
        <if test="hrId != null">
            AND
            tj.HR_ID = #{hrId}
        </if>
        <if test="searchStr != null">
            AND
            tj.JOB_NAME LIKE concat(concat('%',#{searchStr}),'%')
        </if>
        <if test="lastTime != null">
            AND
            tj.ADD_TIME &lt;= #{lastTime}
        </if>
        GROUP BY tj.JOB_ID
        ORDER BY
        tj.ADD_TIME DESC
        LIMIT #{pageIndex}

    </select>

接口層事務

    /**
     * 獲取工做列表
     * 
     * @param job
     *            參數
     * @return 工做列表
     */
    List<JobList> getJobListInfo(JobParams job);

bean層ip

public class JobParams {

    //bean
    private Integer jobCategoryId;
    private Integer locationId;
    private Integer salaryId;
    private Integer workYearId;
    private Integer pageIndex;
    private String  lastTime;
    private String  searchStr;
    private Integer enterpriseId;
    private Integer hrId;
    
    //此處省略set和get方法
    }


2、插入

    <insert id="insert" parameterType="com.zhiji.caren.model.Job" useGeneratedKeys="true" keyProperty="job_id" >
        insert into t_job (
        JOB_NAME, JOB_TYPE_ID,
        JOB_DEPARTMENT, ENTERPRISE_ID, JOB_CATEGORY_ID,
        SALARY_MIN, SALARY_MAX, LOCATION_ID,
        HR_ID, WORK_YEARS_ID,
        EDU_REQUIREMENT_ID,
        JOB_DESCRIPTION, JOB_ADDRESS, AUDIT_FLAG,
        STIKY_FLAG, NEGOTIABLE_FLAG, HR_INFO_FLAG,
        USE_FLAG, VALID_TIME,
        UPD_USER_ID,
        ADD_TIME, UPD_TIME)
        values (
        #{jobName,jdbcType=VARCHAR},
        #{jobTypeId,jdbcType=INTEGER},
        #{jobDepartment,jdbcType=VARCHAR}, #{enterpriseId,jdbcType=INTEGER},
        #{jobCategoryId,jdbcType=INTEGER},
        #{salaryMin,jdbcType=INTEGER},
        #{salaryMax,jdbcType=INTEGER},
        #{locationId,jdbcType=INTEGER},
        #{hrId,jdbcType=INTEGER}, #{workYearsId,jdbcType=INTEGER},
        #{eduRequirementId,jdbcType=INTEGER},
        #{jobDescription,jdbcType=VARCHAR}, #{jobAddress,jdbcType=VARCHAR},
        #{auditFlag,jdbcType=CHAR},
        #{stikyFlag,jdbcType=INTEGER},
        #{negotiableFlag,jdbcType=INTEGER}, #{hrInfoFlag,jdbcType=INTEGER},
        #{useFlag,jdbcType=INTEGER}, #{validTime,jdbcType=CHAR},
        #{updUserId,jdbcType=INTEGER},
        #{addTime,jdbcType=CHAR},
        #{updTime,jdbcType=CHAR})
    </insert>

注意:這裏插入語句中沒有帶上job_id,由於在這裏設置了主鍵自增加

<insert id="insert" parameterType="com.zhiji.caren.model.Job" useGeneratedKeys="true" keyProperty="job_id" >

同時還要求,將該表的job_id字段設置爲自增加。


3、更新

    <update id="updateByPrimaryKey" parameterType="com.zhiji.caren.model.Job">
        update t_job
        set
        JOB_NAME = #{jobName,jdbcType=VARCHAR},
        JOB_TYPE_ID =
        #{jobTypeId,jdbcType=INTEGER},
        JOB_DEPARTMENT =
        #{jobDepartment,jdbcType=VARCHAR},
        ENTERPRISE_ID =
        #{enterpriseId,jdbcType=INTEGER},
        JOB_CATEGORY_ID =
        #{jobCategoryId,jdbcType=INTEGER},
        SALARY_MIN =
        #{salaryMin,jdbcType=INTEGER},
        SALARY_MAX =
        #{salaryMax,jdbcType=INTEGER},
        LOCATION_ID =
        #{locationId,jdbcType=INTEGER},
        HR_ID = #{hrId,jdbcType=INTEGER},
        WORK_YEARS_ID = #{workYearsId,jdbcType=INTEGER},
        EDU_REQUIREMENT_ID =
        #{eduRequirementId,jdbcType=INTEGER},
        JOB_DESCRIPTION =
        #{jobDescription,jdbcType=VARCHAR},
        JOB_ADDRESS =
        #{jobAddress,jdbcType=VARCHAR},
        AUDIT_FLAG =
        #{auditFlag,jdbcType=CHAR},
        STIKY_FLAG = #{stikyFlag,jdbcType=INTEGER},
        NEGOTIABLE_FLAG = #{negotiableFlag,jdbcType=INTEGER},
        HR_INFO_FLAG =
        #{hrInfoFlag,jdbcType=INTEGER},
        USE_FLAG = #{useFlag,jdbcType=INTEGER},
        VALID_TIME = #{validTime,jdbcType=CHAR},
        UPD_USER_ID =
        #{updUserId,jdbcType=INTEGER},
        ADD_TIME = #{addTime,jdbcType=CHAR},
        UPD_TIME = #{updTime,jdbcType=CHAR}
        where JOB_ID =
        #{jobId,jdbcType=INTEGER}
    </update>


4、刪除

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from t_job
        where JOB_ID = #{jobId,jdbcType=INTEGER}
    </delete>


查詢、增長、修改和刪除均講完了,注意:後三個執行操做後,須要在接口實現層commit(),這個主要針對的是事務管理那一塊,目前我配置的事務尚未起效果,緣由還沒找到,這裏就不貼出實現層的代碼了。上面講述的內容能夠知足單次的增長、修改和刪除。

相關文章
相關標籤/搜索