MyBatis联表查询
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联表查询的更多相关文章
- mybatis 联表查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- MyBatis联表查询——别名方式
在使用MyBatis你想工程时,单表操作其实是非常完美的,涉及到多表联合查询时,需要我们自己写联表的SQL语句. 我拿出项目中的部分代码作为示例, EmployeeMapper.xml: <?x ...
- Mybatis入门(四)------联表查询
Mybatis联表查询 一.1对1查询 1.数据库建表 假设一个老师带一个学生 CREATE TABLE teacher( t_id INT PRIMARY KEY, t_name VARCHAR(3 ...
- MyBatis学习存档(5)——联表查询
之前的数据库操作都是基于一张表进行操作的,若一次查询涉及到多张表,那该如何进行操作呢? 首先明确联表查询的几个关系,大体可以分为一对一和一对多这两种情况,接下来对这两种情况进行分析: 一.建立表.添加 ...
- mybatis之联表查询
今天碰到了一个问题,就是要在三张表里面各取一部分数据然后组成一个list传到前台页面显示.但是并不想在后台做太多判断,(因为涉及到for循环)会拉慢运行速度.正好用的框架是spring+springM ...
- Mybatis框架-联表查询显示问题解决
需求:查询结果要求显示用户名,用户密码,用户的角色 因为在用户表中只有用户角色码值,没有对应的名称,角色名称是在码表smbms_role表中,这时我们就需要联表查询了. 这里需要在User实体类中添加 ...
- Spring Hibernate JPA 联表查询 复杂查询(转)
今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...
- mybatis 关联表查询
这段时间由于项目上的需求:需要将数据库中两表关联的数据查询出来展示到前端(包含一对一,一对多): (1)一对一: 在实体类中维护了另一个类的对象: 这里我以用户(User)和产品(Product)为例 ...
- Spring Hibernate JPA 联表查询 复杂查询
今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...
随机推荐
- 自学提高:JVM点滴
写在前面 这年头就是得不断地学习. 学什么东西就看需要了. 不学习很难进步. 同时别人也会超过你. 东西都是网上有的.图片也好,文字也好.基本都可以在网上找到. JAVA运行原理 JVM包括字节码解释 ...
- [转帖]CPU Cache 机制以及 Cache miss
CPU Cache 机制以及 Cache miss https://www.cnblogs.com/jokerjason/p/10711022.html CPU体系结构之cache小结 1.What ...
- Scanner和BufferReader的效率问题
先给出一道题,测试平台是Acwing, 这道题是腾讯2019年春招提前批笔试第二题.题目不难,但是如果不注意细节,很容易TLE(超时) https://www.acwing.com/problem/c ...
- Servlet生命周期和注解配置
Servlet的生命周期和注解配置问题 /* Servlet? 运行在服务器上的小程序 定义浏览器访问到Tomcat的规则 一.生命周期? 1.创建 2.提供服务 3.被销毁 二.servlet3.0 ...
- Djangon
2.怎么样从浏览器获得用户输入的数据? request.浏览器的八种申请方式.get(条件) request.浏览器的八种申请方式[] request.浏览器的八种申请方式(这里什么也不要写)> ...
- Spring MVC 使用介绍(六)—— 注解式控制器(二):请求映射与参数绑定
一.概述 注解式控制器支持: 请求的映射和限定 参数的自动绑定 参数的注解绑定 二.请求的映射和限定 http请求信息包含六部分信息: ①请求方法: ②URL: ③协议及版本: ④请求头信息(包括Co ...
- 2.3 os 模块
目录 2.3.1 功能 2.3.2 常用方法 2.3.2.1 创建相关 2.3.2.2 切换相关 2.3.2.3 查看相关 2.3.2.4 编辑相关 2.3.2.5 删除相关 2.3.1 功能 2.3 ...
- Ubuntu最常见的包问题
工作环境换成Ubuntu18.04小记:https://www.cnblogs.com/dunitian/p/9773214.html Ubuntu不得不说的就是这个apt出问题的处理 :(换源就不说 ...
- golang与python多线程的并发速度
一.golang的代码 package main import ( "fmt" "time" ) func Text_goroute(a int, b int) ...
- python Django cookie和session
在一个会话的多个请求中共享数据,这就是会话跟踪技术.例如在一个会话中的请求如下: 请求银行主页: 请求登录(请求参数是用户名和密码): 请求转账(请求参数与转账相关的数据): 请求信誉卡还款(请求参 ...