MyEclipse整合SSM框架(三):详解Mybatis逆向工程配置一对一关联表,查询结果排序问题

先说问题在搭建SSM(Spring+SpringMVC+Mybatis)框架,从后台数据库查询出数据提供给前端页面进行分页显示时候,发现分页查询出来的结果在前端页面上显示并不是按照主键 id 排序的,而是先按照关联表id,也就是主表外键的 id 分成不同的部门(我这里是employee 和 department) ,每个部门中再按照 id 顺序显示记录。

这样导致主键 id 为 2的反而跑到了查询结果的后面,或是在插入新的数据,由于排序显示问题,可能不能出现在列表的最后面,导致不容易找到刚刚插入的记录。

其中,我想按照 emp_id(employeeid) 这个主键顺序排列查询的记录,显示到页面上

默认出现的问题如图:


表的关联结构如图:



修改后的效果如图:

    


    

解决思路:开始认为是 pageHelper 这个分页插件,在分页的时候对于记录按照某种规则排列了,最后经过测试,mybatis生成逆向工程时候 配置sql 语句 如果不指定 order by XX ,就会按照上述情况排序。其中,实现一对一关联的sql 查询语句有两种写法:          

  1. SELECT * FROM tbl_emp e, tbl_dept d WHERE e.d_id = d.dept_id  


  2. SELECTFROM tbl_emp e LEFT JOIN tbl_dept d ON e.d_id = d.dept_id  


解决步骤:Mybatis逆向工程配置以及dao层增删改查测试参见:

                    https://blog.csdn.net/weixin_38533896/article/details/79866813

                    这里主要是写如何在 mybatis 映射文件 实现一对一关联的sql 并设置order by 属性,排序查询结果


1.  mybatis 逆向工程生成的目录结构如下,包括bean 、dao  以及 mapper文件夹下的sql 映射文件


2. 在com.lbc.crud.bean 包中的 Employee.java 中添加Department 属性以及相应的 get 、set 方法

  

  1. private Department department;  
  2.   
  3. public Department getDepartment() {  
  4.     return department;  
  5. }  
  6.   
  7. public void setDepartment(Department department) {  
  8.     this.department = department;  
  9. }  

3.  在 com.lbc.crud.dao 包中的EmployeeMapper.java 中添加 员工 关联 部门的查询接口

  1. List<Employee> selectByExampleWithDept(EmployeeExample example);  
  2.   
  3. Employee selectByPrimaryKeyWithDept(Integer empId);  


4. 在mapper 文件夹下 EmployeeMapper.xml 将对应的sql 语句填入,这里直接copy 了整个映射文件select 相关语句

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3. <mapper namespace="com.lbc.crud.dao.EmployeeMapper">  
  4.   
  5.     <resultMap id="BaseResultMap" type="com.lbc.crud.bean.Employee">  
  6.         <id column="emp_id" jdbcType="INTEGER" property="empId" />  
  7.         <result column="emp_name" jdbcType="VARCHAR" property="empName" />  
  8.         <result column="gender" jdbcType="CHAR" property="gender" />  
  9.         <result column="email" jdbcType="VARCHAR" property="email" />  
  10.         <result column="d_id" jdbcType="INTEGER" property="dId" />  
  11.     </resultMap>  
  12.   
  13.     <resultMap id="WithDeptResultMap" type="com.lbc.crud.bean.Employee">  
  14.         <id column="emp_id" jdbcType="INTEGER" property="empId" />  
  15.         <result column="emp_name" jdbcType="VARCHAR" property="empName" />  
  16.         <result column="gender" jdbcType="CHAR" property="gender" />  
  17.         <result column="email" jdbcType="VARCHAR" property="email" />  
  18.         <result column="d_id" jdbcType="INTEGER" property="dId" />  
  19.         <!-- 指定联合查询出部门字段的封装 -->  
  20.         <association javaType="com.lbc.crud.bean.Department" property="department">  
  21.             <id column="dept_id" property="deptId" />  
  22.             <result column="dept_name" property="deptName" />  
  23.         </association>  
  24.     </resultMap>  
  25.   
  26.     <sql id="Example_Where_Clause">  
  27.         <where>  
  28.             <foreach collection="oredCriteria" item="criteria" separator="or">  
  29.                 <if test="criteria.valid">  
  30.                     <trim prefix="(" prefixOverrides="and" suffix=")">  
  31.                         <foreach collection="criteria.criteria" item="criterion">  
  32.                             <choose>  
  33.                                 <when test="criterion.noValue">  
  34.                                     and ${criterion.condition}  
  35.                                 </when>  
  36.                                 <when test="criterion.singleValue">  
  37.                                     and ${criterion.condition} #{criterion.value}  
  38.                                 </when>  
  39.                                 <when test="criterion.betweenValue">  
  40.                                     and ${criterion.condition} #{criterion.value}  
  41.                                     and  
  42.                                     #{criterion.secondValue}  
  43.                                 </when>  
  44.                                 <when test="criterion.listValue">  
  45.                                     and ${criterion.condition}  
  46.                                     <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">  
  47.                                         #{listItem}  
  48.                                     </foreach>  
  49.                                 </when>  
  50.                             </choose>  
  51.                         </foreach>  
  52.                     </trim>  
  53.                 </if>  
  54.             </foreach>  
  55.         </where>  
  56.     </sql>  
  57.     <sql id="Update_By_Example_Where_Clause">  
  58.         <where>  
  59.             <foreach collection="example.oredCriteria" item="criteria" separator="or">  
  60.                 <if test="criteria.valid">  
  61.                     <trim prefix="(" prefixOverrides="and" suffix=")">  
  62.                         <foreach collection="criteria.criteria" item="criterion">  
  63.                             <choose>  
  64.                                 <when test="criterion.noValue">  
  65.                                     and ${criterion.condition}  
  66.                                 </when>  
  67.                                 <when test="criterion.singleValue">  
  68.                                     and ${criterion.condition} #{criterion.value}  
  69.                                 </when>  
  70.                                 <when test="criterion.betweenValue">  
  71.                                     and ${criterion.condition} #{criterion.value}  
  72.                                     and  
  73.                                     #{criterion.secondValue}  
  74.                                 </when>  
  75.                                 <when test="criterion.listValue">  
  76.                                     and ${criterion.condition}  
  77.                                     <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">  
  78.                                         #{listItem}  
  79.                                     </foreach>  
  80.                                 </when>  
  81.                             </choose>  
  82.                         </foreach>  
  83.                     </trim>  
  84.                 </if>  
  85.             </foreach>  
  86.         </where>  
  87.     </sql>  
  88.     <sql id="Base_Column_List">  
  89.         emp_id, emp_name, gender, email, d_id  
  90.     </sql>  
  91.     <sql id="WithDept_Colume_List">  
  92.         e.emp_id, e.emp_name, e.gender, e.email, e.d_id, d.dept_id,  
  93.         d.dept_name  
  94.     </sql>  
  95.     <select id="selectByExampleWithDept" resultMap="WithDeptResultMap">  
  96.         select  
  97.         <if test="distinct">  
  98.             distinct  
  99.         </if>  
  100.         <include refid="WithDept_Colume_List" />  
  101.         from tbl_emp e, tbl_dept d where e.d_id = d.dept_id  
  102.         <if test="_parameter != null">  
  103.             <include refid="Example_Where_Clause" />  
  104.         </if>  
  105.         <if test="orderByClause != null">  
  106.             order by ${orderByClause}  
  107.         </if>  
  108.     </select>  
  109.     <select id="selectByPrimaryKeyWithDept" resultMap="WithDeptResultMap">  
  110.         select  
  111.         <include refid="WithDept_Colume_List" />  
  112.         from tbl_emp e left join tbl_dept d on e.d_id = d.dept_id  
  113.         where emp_id  
  114.         = #{empId,jdbcType=INTEGER}  
  115.     </select>  
  116.     <select id="selectByExample" parameterType="com.lbc.crud.bean.EmployeeExample" resultMap="BaseResultMap">  
  117.         select  
  118.         <if test="distinct">  
  119.             distinct  
  120.         </if>  
  121.         <include refid="Base_Column_List" />  
  122.         from tbl_emp  
  123.         <if test="_parameter != null">  
  124.             <include refid="Example_Where_Clause" />  
  125.         </if>  
  126.         <if test="orderByClause != null">  
  127.             order by ${orderByClause}  
  128.         </if>  
  129.     </select>  
  130.     <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">  
  131.         select  
  132.         <include refid="Base_Column_List" />  
  133.         from tbl_emp  
  134.         where emp_id = #{empId,jdbcType=INTEGER}  
  135.     </select>  
  136.   
  137. </mapper>  


5. 编写MapperTest.java 进行联合查询的测试,顺带通过employeeExample.setOrderByClause() 方法,给查询结果指定排序顺序,这里是通过emp_id 默认以升序排序

  1. @Test  
  2. public void testEmpSelectByExampleNull(){  
  3.     EmployeeExample employeeExample = new EmployeeExample();  
  4.     // 如果不指定order by id,将默认按照其他方式排序查询结果  
  5.     employeeExample.setOrderByClause("emp_id");  
  6.           
  7.     List<Employee> listTeammembers =employeeMapper.selectByExampleWithDept(employeeExample);    
  8.     for(Employee e : listTeammembers){            
  9.         System.out.println(e);  
  10.     }  
  11. }  


PS: 给sql 提供更多的查询条件在 com.lbc.crud.bean 包的 EmployeeExample.java 中,需要使用更复杂的查询条件,可以在里面寻找合适的方法

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值