前言
MySQL多表查询是项目中常用到的操作,今天在这里进行一次记录,方便以后使用`
当前的项目多使用springboot框架,其中与mysql的交互使用mybatis,mybatis多表查询比较重要,其实也很简单,这里进行记录。
一、Mapper文件更改
1.不使用多表查询的mapper.xml写法
1.1 定义一个resultMap,其中是数据库表字段(column)和Java对象字段的映射(property),id为主键
<resultMap type="ProcessFlow" id="ProcessFlowResult">
<id property="id" column="id_" />
<result property="code" column="code_" />
<result property="name" column="name_" />
<result property="description" column="description_" />
<result property="createBy" column="create_by_" />
<result property="createTime" column="create_time_" />
<result property="updateBy" column="update_by_" />
<result property="updateTime" column="update_time_" />
<result property="status" column="status_" />
<result property="tenantId" column="tenant_id_" />
<result property="jsonStr" column="json_str_" />
<result property="graphLib" column="graph_lib_" />
<result property="graphLibName" column="graphLibName" />
<result property="canvasStyle" column="canvas_style_" />
<result property="chart" column="chart_" />
</resultMap>
1.2 定义一个sql,其中内容为要查询的字段,方便后续查询调用
<sql id="selectProcessFlowVo">
select a.id_, a.code_, a.name_, a.description_, a.create_by_, a.create_time_, a.update_by_, a.update_time_,
a.status_, a.tenant_id_, a.json_str_, a.graph_lib_, a.canvas_style_, a.chart_, c.name_ as graphLibName from bv_process_flow a left join bv_comp_categ c on a.graph_lib_= c.id_
</sql>
1.3 定义具体的批量/单个查询语句
<select id="list" parameterType="ProcessFlow" resultMap="ProcessFlowResult">
<include refid="selectProcessFlowVo"></include>
<where>
<if test="id != null and id != ''">and a.id_ = #{
id}</if>
<if test="code != null and code != ''">and a.code_ like concat('%', #{
code}, '%')</if>
<if test="name != null and name != ''">and a.name_ like concat('%', #{
name}, '%')</if>
<if test="status != null and status != ''">and a.status_ = #{
status}</if>
<if test="tenantId != null and tenantId != ''">and a.tenant_id_ = #{
tenantId}</if>
</where>
</select>
2.多表查询的mapper.xml写法
2.1 定义resultMap,和不使用多表查询的resultMap相比,添加了collection, 其property表示Java对象中,保存多表查询结果的list字段,如下面的charList,ofType为domain类型,即Chart
<resultMap type="ProcessFlowWithCharts" id="ProcessFlowWithChartsResult">
<id property="id" column="id_" />
<result property="code" column="code_" />
<result property="name" column="name_" />
<result property="description" column="description_" />
<result property="createBy" column="create_by_" />
<result property="createTime" column="create_time_" />
<result property="updateBy" column="update_by_" />
<result property="updateTime" column="update_time_" />
<result property="status" column="status_" />
<result property="tenantId" column="tenant_id_" />
<result property="jsonStr" column="json_str_" />
<result property="graphLib" column="graph_lib_" />
<result property="graphLibName" column="graphLibName" />
<result property="canvasStyle" column="canvas_style_" />
<result property="chart" column="chart_" />
<collection property="chartList" ofType="Chart">
<id property="id" column="cId"/>
<result property="code" column="cCode"/>
<result property="name" column="cName" />
<result property="description" column="cDescription" />
<result property="createBy" column="cCreateBy" />
<result property="createTime" column="cCreateTime" />
<result property="left" column="left_" />
<result property="top" column="top_" />
<result property="height" column="height_" />
<result property="width" column="width_" />
<result property="zindex" column="zindex_" />
<result property="dataSource" column="dataSource_" />
<result property="processFlowId" column="process_flow_id_" />
</collection>
</resultMap>
2.2 多表查询sql
<select id="get" parameterType="String" resultMap="ProcessFlowWithChartsResult">
select a.id_, a.code_, a.name_, a.description_, a.create_by_, a.create_time_, a.update_by_, a.update_time_,
a.status_, a.tenant_id_, a.json_str_, a.graph_lib_, a.canvas_style_, a.chart_, b.name_ as graphLibName,
c.id_ as cId, c.name_ as cName, c.code_ as cCode, c.description_ as cDescription, c.create_by_ as cCreateBy, c.create_time_ as cCreateTime,
c.left_, c.top_, c.height_, c.width_, c.zindex_, c.dataSource_, c.process_flow_id_
from bv_process_flow a left join bv_comp_categ b on a.graph_lib_= b.id_ left join bv_chart c on a.id_=c.process_flow_id_ where a.id_=#{
id}
</select>
2.3 多表查询Java对象,即domain定义
public class ProcessFlow {
private String id;
private String code;
private String name;
private String description;
private String createBy;
@JsonFormat(
pattern = "yyyy-MM-dd HH:mm:ss"
)
private Date createTime;
private String updateBy;
@JsonFormat(
pattern = "yyyy-MM-dd HH:mm:ss"
)
private Date updateTime;
private String status;
private String tenantId;
private String jsonStr;
private String graphLib;
private String graphLibName;
private String canvasStyle;
private String chart;
private List<Chart> chartList;
}
总结
其实多表查询在mybatis中很好实现,主要是resultMap的设置添加collection,其后是Java对象添加List类型的对象列表即可。