ssm高级整合:(2)

逆向工程创建基础模块,修改mapper文件(新增查询员工显示部门的方法),mapper测试,添加测试数据

 

ssm高级整合案例目录: 目录

 

逆向工程创建基础模块

MyBatis:逆向工程(清晰简洁版)

MBG配置文件generatorConfig.xml中加入,可生成没有注释的

        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

修改Mapper文件

新增带部门查询的方法

在EmpMapper.java中添加两个方法

    List<Emp> selectByExampleWithDept(EmpExample example);

    Emp selectByPrimaryKeyWithDept(Integer empId);

在Emp.java中添加Dept类型的属性与get/set方法

在EmpMapper.xml中 加入 新的<sql>与<select>与<resultMap>

 

测试mapper

 

 <!--=====resultMap=====-->
 <resultMap id="WithDeptResultMap" type="com.tinstu.ssm.bean.Emp">
    <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="dept" javaType="com.tinstu.ssm.bean.Dept">
      <id column="dept_id" property="deptId" />
      <result column="dept_name" property="deptName" />
    </association>
  </resultMap>
<!--=====sql=====-->
  <sql id="Withdept_Column_List">
    e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name
  </sql>
<!--=====两个查询=====-->
  <!--
    查询员工 带部门信息
    List<Emp> selectByExampleWithDept(EmpExample example);
    Emp selectByPrimaryKeyWithDept(Integer empId);
  -->
  <select id ="selectByExampleWithDept" resultMap="WithDeptResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Withdept_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_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>

 

测试mapper

junti5单元测试

Spring5新功能-整合日志框架/@Nullable/函数式注册对象/整合JUnit5单元测试框架

对接口进行简单的测试

首先在Spring的配置文件applicationContext.xml 中配置

 

 

 

 

阅读剩余
THE END