myBatis --(3)数据的增删改查

上一篇文章《接口方式编程》中讲述的是返回单个对象(包括对象、整型、字符串)等等,这篇文章讲述的是返回一个list。

查询单个对象的返回可以采用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类型的

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

3>返回是整型类型的

    <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>


一、这篇讲述的是返回的是一个list,用resultMap方式来接收转换

接收的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层

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方法
    }


二、插入

    <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字段设置为自增长。


三、更新

    <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>


四、删除

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


查询、增加、修改和删除均讲完了,注意:后三个执行操作后,需要在接口实现层commit(),这个主要针对的是事务管理那一块,目前我配置的事务还没有起效果,原因还没找到,这里就不贴出实现层的代码了。上面讲述的内容可以满足单次的增加、修改和删除。

你可能感兴趣的:(myBatis --(3)数据的增删改查)