spring boot整合mybatis查询数据库不使用实体,使用map, 则返回字段部分为null的则不显示出来!!!

码农持续挖坑中,今天遇到一个问题,因为sql是前端动态传入的,我每次执行查询的时候发现都会缺失一些字段,最后发现,为null的值都被过滤掉了,整合网络上的各种解决方案,如下几点!

 1:springboot里面yml 文件配置 

mybatis:
configuration:
call-setters-on-nulls: true

2:springboot里面yml 文件继续配置 

 spring:
 jackson:
#设置空如何序列化
defaultPropertyInclusion: ALWAYS

3:mybaits的xml文件mybatis-config.xml 配置如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!--解决,查询返回结果含null没有对应字段值问题-->
        <setting name="callSettersOnNulls" value="true"/>
    </settings>
</configuration>

 

以上三点都没有解决我的问题!!!</

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Spring BootMyBatis 进行连表查询时,可以通过递归的方式将查询结果转换成树状结构的数据。 假设有两张表:部门表和员工表,部门表中有一个 parent_id 字段表示该部门的上级部门,员工表中有一个 dept_id 字段表示该员工所在的部门。现在需要查询出所有部门以及下属员工,并将其转换成树状结构的数据。 首先,需要定义一个部门实体类和一个员工实体类,代码如下: ```java public class Department { private Long id; private String name; private Long parentId; private List<Employee> employees; // getter 和 setter 略 } public class Employee { private Long id; private String name; private Long deptId; // getter 和 setter 略 } ``` 接下来,可以使用 MyBatis 进行连表查询,代码如下: ```xml <select id="getDepartments" resultMap="departmentResultMap"> select d.id, d.name, d.parent_id, e.id as employee_id, e.name as employee_name, e.dept_id as employee_dept_id from department d left join employee e on d.id = e.dept_id order by d.id, e.id </select> <resultMap id="departmentResultMap" type="Department"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="parentId" column="parent_id"/> <collection property="employees" ofType="Employee" resultMap="employeeResultMap" columnPrefix="employee_"/> </resultMap> <resultMap id="employeeResultMap" type="Employee"> <id property="id" column="employee_id"/> <result property="name" column="employee_name"/> <result property="deptId" column="employee_dept_id"/> </resultMap> ``` 注意,这里使用MyBatis 的 resultMap 来映射查询结果到实体类中。 接下来,可以编写一个递归方法,将查询结果转换成树状结构的数据,代码如下: ```java private List<Department> buildTree(List<Department> departments) { Map<Long, Department> map = new HashMap<>(); for (Department department : departments) { map.put(department.getId(), department); } List<Department> roots = new ArrayList<>(); for (Department department : departments) { Long parentId = department.getParentId(); if (parentId == null || parentId == 0) { roots.add(department); } else { Department parent = map.get(parentId); if (parent != null) { parent.getEmployees().add(department.getEmployees().get(0)); } } } for (Department root : roots) { buildChildren(map, root); } return roots; } private void buildChildren(Map<Long, Department> map, Department department) { List<Department> children = new ArrayList<>(); for (Employee employee : department.getEmployees()) { Department child = map.get(employee.getDeptId()); if (child != null) { children.add(child); buildChildren(map, child); } } department.setChildren(children); } ``` 最后,在控制器中调用这个递归方法即可,代码如下: ```java @GetMapping("/departments") public List<Department> getDepartments() { List<Department> departments = departmentMapper.getDepartments(); return buildTree(departments); } ``` 这样就可以返回树状结构的数据了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值