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. UVA - 11478 - Halum(二分+差分约束系统)

    Problem  UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...

  2. Linux下添加windows字体

    在Linux下使用wqy字体,在视觉效果上就已近很好了,其实没有必要添加windows字体.但是显然有些人(比如领导,^..^)就喜欢宋体.楷体,所以添加windows字体有时还是需要的,幸运的是这件 ...

  3. java 服务端I/O非阻塞实现05

    /** * 非阻塞IO多线线程服务端 * 当一个任务进入多线程,这个任务线程需要处理接收信息.发送信息.因而发生I/O阻塞问题 * 利用selector可以实现异步 * */ public class ...

  4. Scrapy框架基本用法讲解

    目标站点:http://quotes.toscrape.com/ (scrape官方练习站点) 这边为了区别Python3.5 和 Python3.7 我修改了scrapy的可执行文件 创建项目文件: ...

  5. Yesterday when I was young

    Somehow, it seems the love I knew was always the most destructive kind 不知为何,我经历的爱情总是最具毁灭性的的那种 Yester ...

  6. webservice异常

    webservice的一个常见异常: [SOAPException: faultCode=SOAP-ENV:Client; msg=Error parsing HTTP status line &qu ...

  7. redis优化

    一.配置文件优化 bind 127.0.0.1 //允许连接的ip,如果就本机连接最后127.0.0.1 protected-mode yes //是否开启保护模式.默认开启,如果没有设置bind项的 ...

  8. java.lang.NullPointerException 错误原因

    [http-nio-8081-exec-1] ERROR o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] - Servlet.service() for ...

  9. MySQL中 in和exists的区别

    A表: 100条数据 , B: 10条数据 select * from A where id in ( select aid from B ) 先执行括号里面的查询,然后执行外面,总共需要查询的次数的 ...

  10. H5的段落标签、图片标签、列表标签与链接标签

    段落标签 (1)<p>段落标签</p> (2)<nobr>强制不换行标签,会出现滚动条</nobr> (3)<pre>保留原始排版标签< ...