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

简介: MyEclipse2015整合SSM框架:详解Mybatis逆向工程配置一对一关联表,查询结果排序问题http://www.bieryun.com/3343.html 先说问题:在搭建SSM(Spring+SpringMVC+Mybatis)框架,从后台数据库查询出数据提供给前端页面进行分页显示时候,发现分页查询出来的结果在前端页面上显示并不是按照主键 id 排序的,而是先按照关联表id,也就是主表外键的 id 分成不同的部门(我这里是employee 和 department) ,每个部门中再按照 id 顺序显示记录。

MyEclipse2015整合SSM框架:详解Mybatis逆向工程配置一对一关联表,查询结果排序问题http://www.bieryun.com/3343.html

先说问题:在搭建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.  
  3.  
  4. SELECT* FROM tbl_emp e LEFT JOIN tbl_dept d ON e.d_id = d.dept_id
  5.  

解决步骤: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 中,需要使用更复杂的查询条件,可以在里面寻找合适的方法

原文地址https://blog.csdn.net/weixin_38533896/article/details/79926139

相关文章
|
9天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
9 0
|
SQL Java 数据库连接
MyBatis 优秀的持久层框架(一)
MyBatis 优秀的持久层框架
61 0
|
29天前
|
SQL Java 数据库连接
springboot中配置mybatis别名该怎么写?
springboot中配置mybatis别名该怎么写?
22 0
|
22天前
|
SQL XML Java
Mybatis中选择语句的使用:<choose>标签、分区排序 Row_num() over ()函数的使用呢
Mybatis中选择语句的使用:<choose>标签、分区排序 Row_num() over ()函数的使用呢
13 0
|
29天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
6天前
|
SQL Java 数据库连接
什么是MyBatis持久层框架?
MyBatis 是一个优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs (Plain Old Java Objects, 普通的 Java 对象) 映射成数据库中的记录。
16 5
|
7天前
|
Java 数据库连接 数据库
MyBatis之旅:从零开始的环境搭建与配置
MyBatis之旅:从零开始的环境搭建与配置
21 1
|
22天前
|
SQL XML Java
这样使用MyBatis框架,被攻击了
这样使用MyBatis框架,被攻击了
8 0
|
23天前
|
Java fastjson Apache
Spring Boot+Gradle+ MyBatisPlus3.x搭建企业级的后台分离框架
Spring Boot+Gradle+ MyBatisPlus3.x搭建企业级的后台分离框架
30 1
|
25天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
14 1