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

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

MyEclipse2015整合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. SELECT* FROM 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. public Department getDepartment() {
  3.     return department;
  4. }
  5. public void setDepartment(Department department) {
  6.     this.department = department;
  7. }

 

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

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

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

原文地址http://www.bieryun.com/3343.html

相关文章
|
15天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
19 0
|
SQL Java 数据库连接
MyBatis 优秀的持久层框架(一)
MyBatis 优秀的持久层框架
66 0
|
13天前
|
SQL Java 数据库连接
什么是MyBatis持久层框架?
MyBatis 是一个优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs (Plain Old Java Objects, 普通的 Java 对象) 映射成数据库中的记录。
25 5
|
28天前
|
SQL XML Java
这样使用MyBatis框架,被攻击了
这样使用MyBatis框架,被攻击了
14 0
|
29天前
|
Java fastjson Apache
Spring Boot+Gradle+ MyBatisPlus3.x搭建企业级的后台分离框架
Spring Boot+Gradle+ MyBatisPlus3.x搭建企业级的后台分离框架
30 1
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
42 1
|
1月前
|
druid Java 数据库连接
Spring Boot3整合MyBatis Plus
Spring Boot3整合MyBatis Plus
45 1
|
3月前
|
Java 数据库连接 Maven
SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!
SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!
|
29天前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
69 0