MyBatis联表查询

时间:2023-03-08 21:58:22

MyBatis逆向工程主要用于单表操作,那么需要进行联表操作时,往往需要我们自己去写sql语句。

写sql语句之前,我们先修改一下实体类

Course.java:

 public class Course {
private Integer id; private String cNum; private String cName; private String remark; private Integer status; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getcNum() {
return cNum;
} public void setcNum(String cNum) {
this.cNum = cNum == null ? null : cNum.trim();
} public String getcName() {
return cName;
} public void setcName(String cName) {
this.cName = cName == null ? null : cName.trim();
} public String getRemark() {
return remark;
} public void setRemark(String remark) {
this.remark = remark == null ? null : remark.trim();
} public Integer getStatus() {
return status;
} public void setStatus(Integer status) {
this.status = status;
} @Override
public String toString() {
return "Course{" +
"id=" + id +
", cNum='" + cNum + '\'' +
", cName='" + cName + '\'' +
", remark='" + remark + '\'' +
", status=" + status +
'}';
}
}

Task.java:

 import java.util.Date;

 public class Task {
private Integer id; private String cid; private Integer uid; private String filename; private String fileUrl; private Date created; private Date updated; private String remark; private Integer status; //自定义
private Course course;//联表查询使用 public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getCid() {
return cid;
} public void setCid(String cid) {
this.cid = cid == null ? null : cid.trim();
} public Integer getUid() {
return uid;
} public void setUid(Integer uid) {
this.uid = uid;
} public String getFilename() {
return filename;
} public void setFilename(String filename) {
this.filename = filename == null ? null : filename.trim();
} public String getFileUrl() {
return fileUrl;
} public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl == null ? null : fileUrl.trim();
} public Date getCreated() {
return created;
} public void setCreated(Date created) {
this.created = created;
} public Date getUpdated() {
return updated;
} public void setUpdated(Date updated) {
this.updated = updated;
} public String getRemark() {
return remark;
} public void setRemark(String remark) {
this.remark = remark == null ? null : remark.trim();
} public Integer getStatus() {
return status;
} public void setStatus(Integer status) {
this.status = status;
} //自定义
public Course getCourse() {
return course;
} public void setCourse(Course course) {
this.course = course;
} @Override
public String toString() {
return "Task{" +
"id=" + id +
", cid='" + cid + '\'' +
", uid=" + uid +
", filename='" + filename + '\'' +
", fileUrl='" + fileUrl + '\'' +
", created=" + created +
", updated=" + updated +
", remark='" + remark + '\'' +
", status=" + status +
", course=" + course +
'}';
}
}

TaskMapper.java:

 import com.sun123.springboot.entity.Task;
import com.sun123.springboot.entity.TaskExample;
import org.apache.ibatis.annotations.Param; import java.util.List; public interface TaskMapper {
int countByExample(TaskExample example); int deleteByExample(TaskExample example); int deleteByPrimaryKey(Integer id); int insert(Task record); int insertSelective(Task record); List<Task> selectByExample(TaskExample example); Task selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") Task record, @Param("example") TaskExample example); int updateByExample(@Param("record") Task record, @Param("example") TaskExample example); int updateByPrimaryKeySelective(Task record); int updateByPrimaryKey(Task record); List<Task> taskList();//联表查询
}

TaskMapper.xml:(MyBatis逆向工程的基础上进行修改)

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sun123.springboot.mapper.TaskMapper" >
<resultMap id="BaseResultMap" type="com.sun123.springboot.entity.Task" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="cid" property="cid" jdbcType="VARCHAR" />
<result column="uid" property="uid" jdbcType="INTEGER" />
<result column="filename" property="filename" jdbcType="VARCHAR" />
<result column="file_url" property="fileUrl" jdbcType="VARCHAR" />
<result column="created" property="created" jdbcType="TIMESTAMP" />
<result column="updated" property="updated" jdbcType="TIMESTAMP" />
<result column="remark" property="remark" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="INTEGER" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause" >
<where >
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List" >
id, cid, uid, filename, file_url, created, updated, remark, status
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.sun123.springboot.entity.TaskExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from task
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from task
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from task
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.sun123.springboot.entity.TaskExample" >
delete from task
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.sun123.springboot.entity.Task" >
insert into task (id, cid, uid,
filename, file_url, created,
updated, remark, status
)
values (#{id,jdbcType=INTEGER}, #{cid,jdbcType=VARCHAR}, #{uid,jdbcType=INTEGER},
#{filename,jdbcType=VARCHAR}, #{fileUrl,jdbcType=VARCHAR}, #{created,jdbcType=TIMESTAMP},
#{updated,jdbcType=TIMESTAMP}, #{remark,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.sun123.springboot.entity.Task" >
insert into task
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="cid != null" >
cid,
</if>
<if test="uid != null" >
uid,
</if>
<if test="filename != null" >
filename,
</if>
<if test="fileUrl != null" >
file_url,
</if>
<if test="created != null" >
created,
</if>
<if test="updated != null" >
updated,
</if>
<if test="remark != null" >
remark,
</if>
<if test="status != null" >
status,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="cid != null" >
#{cid,jdbcType=VARCHAR},
</if>
<if test="uid != null" >
#{uid,jdbcType=INTEGER},
</if>
<if test="filename != null" >
#{filename,jdbcType=VARCHAR},
</if>
<if test="fileUrl != null" >
#{fileUrl,jdbcType=VARCHAR},
</if>
<if test="created != null" >
#{created,jdbcType=TIMESTAMP},
</if>
<if test="updated != null" >
#{updated,jdbcType=TIMESTAMP},
</if>
<if test="remark != null" >
#{remark,jdbcType=VARCHAR},
</if>
<if test="status != null" >
#{status,jdbcType=INTEGER},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.sun123.springboot.entity.TaskExample" resultType="java.lang.Integer" >
select count(*) from task
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update task
<set >
<if test="record.id != null" >
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.cid != null" >
cid = #{record.cid,jdbcType=VARCHAR},
</if>
<if test="record.uid != null" >
uid = #{record.uid,jdbcType=INTEGER},
</if>
<if test="record.filename != null" >
filename = #{record.filename,jdbcType=VARCHAR},
</if>
<if test="record.fileUrl != null" >
file_url = #{record.fileUrl,jdbcType=VARCHAR},
</if>
<if test="record.created != null" >
created = #{record.created,jdbcType=TIMESTAMP},
</if>
<if test="record.updated != null" >
updated = #{record.updated,jdbcType=TIMESTAMP},
</if>
<if test="record.remark != null" >
remark = #{record.remark,jdbcType=VARCHAR},
</if>
<if test="record.status != null" >
status = #{record.status,jdbcType=INTEGER},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update task
set id = #{record.id,jdbcType=INTEGER},
cid = #{record.cid,jdbcType=VARCHAR},
uid = #{record.uid,jdbcType=INTEGER},
filename = #{record.filename,jdbcType=VARCHAR},
file_url = #{record.fileUrl,jdbcType=VARCHAR},
created = #{record.created,jdbcType=TIMESTAMP},
updated = #{record.updated,jdbcType=TIMESTAMP},
remark = #{record.remark,jdbcType=VARCHAR},
status = #{record.status,jdbcType=INTEGER}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.sun123.springboot.entity.Task" >
update task
<set >
<if test="cid != null" >
cid = #{cid,jdbcType=VARCHAR},
</if>
<if test="uid != null" >
uid = #{uid,jdbcType=INTEGER},
</if>
<if test="filename != null" >
filename = #{filename,jdbcType=VARCHAR},
</if>
<if test="fileUrl != null" >
file_url = #{fileUrl,jdbcType=VARCHAR},
</if>
<if test="created != null" >
created = #{created,jdbcType=TIMESTAMP},
</if>
<if test="updated != null" >
updated = #{updated,jdbcType=TIMESTAMP},
</if>
<if test="remark != null" >
remark = #{remark,jdbcType=VARCHAR},
</if>
<if test="status != null" >
status = #{status,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.sun123.springboot.entity.Task" >
update task
set cid = #{cid,jdbcType=VARCHAR},
uid = #{uid,jdbcType=INTEGER},
filename = #{filename,jdbcType=VARCHAR},
file_url = #{fileUrl,jdbcType=VARCHAR},
created = #{created,jdbcType=TIMESTAMP},
updated = #{updated,jdbcType=TIMESTAMP},
remark = #{remark,jdbcType=VARCHAR},
status = #{status,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update> <resultMap id="TaskResultMap" type="com.sun123.springboot.entity.Task" extends="BaseResultMap">
<association property="course" resultMap="com.sun123.springboot.mapper.CourseMapper.BaseResultMap"></association>
</resultMap>
<select id="taskList" resultMap="TaskResultMap">
SELECT t.*,c.* FROM task t LEFT JOIN course c ON t.cid=c.c_num
</select> </mapper>
<association property="course" resultMap="com.sun123.springboot.mapper.CourseMapper.BaseResultMap"></association>这种写法直接引入了CourseMApper.xml中的字段信息,不需要再次定义,比较简洁。

接下来以Bootstrap table分页插件为例,实现完整的调用

StuService.java:

 //bootstrap table分页插件,数据返回
BootstrapPage showTask(int offset, int limit,String search);

StuServiceImpl.java:

     /**
* @Description //bootstrap table分页插件,数据返回
* @Date 2019-04-04 19:54
* @Param [limit, offset]
* @return com.sun123.springboot.entity.bootstrap.PageHelper
**/
@Override
public BootstrapPage showTask(int offset, int limit,String search) {
BootstrapPage bootstrapPage = new BootstrapPage();
//pageNumber pageSize
Page pages = PageHelper.startPage(offset, limit); List<Task> taskList = taskMapper.taskList();
bootstrapPage.setRows(taskList);
bootstrapPage.setTotal((int)pages.getTotal());
return bootstrapPage;
}

StuController.java:

     @GetMapping("pageInfo")
@ResponseBody
public BootstrapPage pageInfo(int offset, int limit, String search){
System.out.println("======"+offset+"==="+limit+"====="+search+"=====");
BootstrapPage page = stuService.showTask(offset, limit,search);
return page;
}

后台查询结果:

 Task{id=25, cid='04021611', uid=3, filename='呵呵呵', fileUrl='http://192.168.83.133/images/2019/03/24/1 - 副本1553391128920311.jpg', created=Sun Mar 24 09:32:11 CST 2019, updated=Sun Mar 24 09:32:11 CST 2019, remark='5263', status=0,
course=Course{id=25, cNum='04021611', cName='Hadoop数据分析平台Ⅰ', remark='5263', status=0}}

表格展示时,操作如下:

 $(function() {
$('#table').bootstrapTable({
url: 'pageInfo',
pagination: true, //是否显示分页(*)
sortable: false, //是否启用排序
sortOrder: "asc", //排序方式
//toolbar: '#toolbar', //工具按钮用哪个容器
//method:'post',
//sortable: true,//排序
showColumns: true,//是否显示 内容列下拉框
//clickToSelect: true,//点击选中checkbox
sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*)
pageNumber: 1, //初始化加载第一页,默认第一页
pageSize: 5, //每页的记录行数(*)
pageList: [5, 10, 50, 100], //可供选择的每页的行数(*)
showRefresh: true,//是否显示刷新按钮
showToggle: true,//是否显示详细视图和列表视图的切换按钮
//search: true, //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大
//queryParamsType: "", //默认值为 'limit' ,在默认情况下 传给服务端的参数为:offset,limit,sort
// 设置为 '' 在这种情况下传给服务器的参数为:pageSize,pageNumber
showExport: true,//是否显示导出
columns: [{
field: 'course.cName',
title: '课程名称'
}, {
field: 'filename',
title: '文件名称'
}, {
field: 'remark',
title: '说明'
},{
field: 'created',
title: '上传时间'
},{
field: 'fileUrl',
title: '下载地址'
}, ]
});
})
说明:course对象属性的使用需要多写一层,例如:course.cName