美文网首页
SSM框架的(CRUD)基础环境搭建4.0

SSM框架的(CRUD)基础环境搭建4.0

作者: 念念碎平安夜 | 来源:发表于2019-05-21 17:20 被阅读0次
    具体步骤请参考由本文作者编写的另一篇文章《SSM框架配置》
    编写SSM整合的关键配置文件
    • web.xml,spring,springmvc,mybatis,使用mybatis的逆向工程生成对应的bean以及mapper
    修改Mapper文件

    查询员工信息的时候,还需要展示部门的名称,逆向生成的mapper中select查询部分只有部门的d_id,所以需要联合查询,提高性能。

    <sql id="Base_Column_List">
        emp_id, emp_name, gender, email, d_id
    </sql>
    

    步骤一:进入EmployeeMapper.java,新增两个方法

    List<Employee> selectByExampleWithDept(EmployeeExample example);
    Employee selectByPrimaryKeyWithDept(Integer empId);
    

    步骤二、进入Employee.java

    //希望查询员工的同时部门信息也是查询好的
    private Department department;
    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }
    

    步骤三、进入EmployeeMapper.xml
    (1)新建id为WithDeptResultMap的resultMap

    <resultMap type="com.christmaseve.crud.bean.Employee" id="WithDeptResultMap">
        <id column="emp_id" jdbcType="INTEGER" property="empId" />
        <result column="emp_name" jdbcType="VARCHAR" property="empName" />
        <result column="gender" jdbcType="VARCHAR" property="gender" />
        <result column="email" jdbcType="VARCHAR" property="email" />
        <result column="d_id" jdbcType="INTEGER" property="dId" />
        <!-- 指定联合查询出的部门字段的封装 -->
        <association property="department" javaType="com.christmaseve.crud.bean.Department">
            <id column="dept_id" property="deptId"/>
            <result column="dept_name" property="deptName"/>
        </association>
    </resultMap>
    

    (2)新建id为WithDept_Base_Column_List的sql

    <sql id="WithDept_Base_Column_List">
        e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name
    </sql>
    

    (3)新建select查询(查询员工同时带部门信息)

        <!-- 查询员工同时带部门信息 -->
      <select id="selectByExampleWithDept" resultMap="WithDeptResultMap">
        select
        <if test="distinct">
          distinct
        </if>
        <include refid="WithDept_Base_Column_List" />
        FROM tbl_emp e
            left join tbl_dept d on e.`d_id`=d.`dept_id`
        <if test="_parameter != null">
          <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null">
          order by ${orderByClause}
        </if>
      </select>
      <select id="selectByPrimaryKeyWithDept" resultMap="WithDeptResultMap">
        select 
        <include refid="WithDept_Base_Column_List" />
        FROM tbl_emp e
            left join tbl_dept d on e.`d_id`=d.`dept_id`
        where emp_id = #{empId,jdbcType=INTEGER}
      </select>
    

    相关文章

      网友评论

          本文标题:SSM框架的(CRUD)基础环境搭建4.0

          本文链接:https://www.haomeiwen.com/subject/visqnqtx.html