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

MyBatis联表查询的更多相关文章

  1. mybatis 联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  2. MyBatis联表查询——别名方式

    在使用MyBatis你想工程时,单表操作其实是非常完美的,涉及到多表联合查询时,需要我们自己写联表的SQL语句. 我拿出项目中的部分代码作为示例, EmployeeMapper.xml: <?x ...

  3. Mybatis入门(四)------联表查询

    Mybatis联表查询 一.1对1查询 1.数据库建表 假设一个老师带一个学生 CREATE TABLE teacher( t_id INT PRIMARY KEY, t_name VARCHAR(3 ...

  4. MyBatis学习存档(5)——联表查询

    之前的数据库操作都是基于一张表进行操作的,若一次查询涉及到多张表,那该如何进行操作呢? 首先明确联表查询的几个关系,大体可以分为一对一和一对多这两种情况,接下来对这两种情况进行分析: 一.建立表.添加 ...

  5. mybatis之联表查询

    今天碰到了一个问题,就是要在三张表里面各取一部分数据然后组成一个list传到前台页面显示.但是并不想在后台做太多判断,(因为涉及到for循环)会拉慢运行速度.正好用的框架是spring+springM ...

  6. Mybatis框架-联表查询显示问题解决

    需求:查询结果要求显示用户名,用户密码,用户的角色 因为在用户表中只有用户角色码值,没有对应的名称,角色名称是在码表smbms_role表中,这时我们就需要联表查询了. 这里需要在User实体类中添加 ...

  7. Spring Hibernate JPA 联表查询 复杂查询(转)

    今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...

  8. mybatis 关联表查询

    这段时间由于项目上的需求:需要将数据库中两表关联的数据查询出来展示到前端(包含一对一,一对多): (1)一对一: 在实体类中维护了另一个类的对象: 这里我以用户(User)和产品(Product)为例 ...

  9. Spring Hibernate JPA 联表查询 复杂查询

    今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...

随机推荐

  1. let声明

    <script> /** * es6 let 练习 * 生效范围:块级代码代码内. */ // { // let a=2; // var c=2; // } // console.log( ...

  2. Mongo字符串类型的数值查询---$Where查询介绍

    ​        在Mongo中都知道字符串类型大小比较都是以ASCII进行比较的,所以无法真实比较字符串类型的数值大小 ​      比如查询age大于3的: db.getCollection(&q ...

  3. 《通过C#学Proto.Actor模型》之Behaviors

    Behaviors就是Actor接收到消息后可以改变处理的方法,相同的Actor,每次调用,转到不同的Actor内方法执行,非常适合按流程进行的场景.Behaviors就通过在Actor内部实例化一个 ...

  4. 我用Python实现了一个小说网站雏形

    前言 前段时间做了一个爬取妹子套图的小功能,小伙伴们似乎很有兴趣,为了还特意组建了一个Python兴趣学习小组,来一起学习.十个python九个爬,在大家的印象中好像Python只能做爬虫.然而并非如 ...

  5. SpringCloud(3)服务消费者(Feign)

    上一篇文章,讲述了如何通过 RestTemplate+Ribbon 去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 1.Feign简介 Feign是一个声明式的伪Http客户端,它使得写H ...

  6. Leetcode 226. Invert Binary Tree(easy)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  7. ES7

    本文是自己所学的ES7的一些常用的新特性: 一.padStart()方法,padEnd()方法: 如果某个字符串不够指定长度,有两个方法可以在头部或尾部补全.padStart()用于头部补全,padE ...

  8. JVM内存区域详解

    本文分为两部分:一是JVM内存区域的讲解:二是常见的内存溢出异常分析. 1.JVM内存区域 java虚拟机在执行java程序的过程中会把它管理的内存划分为若干个不同的数据区域,这些区域都有各自的用途, ...

  9. Bean之间的关系

    Bean之间主要有继承和依赖的关系,这里的继承并不是我们面向对象里面所提到的继承. 继承 我们先来创建一个新的配置文件beans-relation.xml <bean id="addr ...

  10. 读取Excel2003、Excel2007或更高级的兼容性问题 workbook 下载中文名称文件

    xls 使用HSSFWorkbook xlsx使用XSSFWorkbook 但是我使用XSSFWorkbook时没找到nuget包,引用不了,只能重新找办法,幸好workbook解决了我这个问题 // ...