Mybatis的常用配置-多表关联查询
Mapper.xml常用配置
全局配置文件(数据库,事物管理,Mapper的注册、打印文件SQL、慢性加载、二级缓存)
Mapper配置文件 (定义自定义接口的具体方案;SQL、数据库、数据库与POJO的映射)
多表关联查询:
一对一,一对多、多对多
单表查询:<select id="findById" parameterType="java.lang.Integer" resultType="com.shouthwind.entity.People">
select *from people where id=#{id}
</select>
业务:通过id查询people对象
目标表:test/people
实体类:com.souththwind.entity.Peaple
Mapper.xml设置相关的配置,由Mybatis自动完成查询,生成POJO
statement标签的主要属性由:id、parameterType\resultType
id:对应接口的方法名;parameterType对应定义的参数类型;resultType定义查询的结果的数据类型(要一致)
parameterType
支持基本的数据类型、包装类、String、多参数、POJO
1.基本数据类型:
public People findById(int id);
<select id="findById2" parameterType="int" resultType="com.shouthwind.entity.People">
select *from people where id=#{id}
</select>
2.包装类:
public People findById(Integer id);
<select id="findById" parameterType="java.lang.Integer" resultType="com.shouthwind.entity.People">
select *from people where id=#{id}
</select>
3.String:
public People findByName(String name);
<select id="findByName" parameterType="java.lang.String" resultType="com.shouthwind.entity.People">
select *from people where name=#{name}
</select>
4.多参数:
public People findByIdAndName(Integer id,String name);
<select id="findByIdAndName" resultType="com.shouthwind.entity.People">
select * from people where id=#{param1} and name=#{param2}
</select>
5.POJO:
public int update(People people);
<update id="update" parameterType="com.shouthwind.entity.People">
update people set name=#{name} ,money=#{money} where id=#{id}
</update>
resultType
与parameterType基本一致:
1.基本数据类型
public int count();
<select id="count" resultType="int">
select count(*) from people
</select>
2.包装类
public Integer count();
<select id="count" resultType="java.lang.Integer">
select count(*) from people
</select>
3.String
public String findNameById(Integer id);
<select id="findNameById" parameterType="java.lang.Integer" resultType="java.lang.String">
select name from people where id=#{id}
</select>
4.POJO
public People findById(Integer id);
<select id="findById" parameterType="java.lang.Integer" resultType="com.shouthwind.entity.People">
select *from people where id=#{id}
</select>
多表关联查询
实际开发中最常用的一对多和多对多
一对多
1.建表
2.Sql数据库基础
3.创建实体类
package com.shouthwind.entity;
import lombok.Data;
import java.util.List;
@Data
public class Sc {
private Integer Sno;
private Integer Cno;
private Integer Grade;
private List<Student> students;
}
package com.shouthwind.entity;
import lombok.Data;
@Data
public class Student {
private Double Sno;
private String Sname;
private String Ssex;
private Integer Sage;
private String Sdept;
private Sc sc;
}
4.实际
package com.shouthwind.repository;
import com.shouthwind.entity.Student;
public interface StudentRepository {
public Student findById( Integer id);
}
resultType是直接将结果集与实体类映射的,名字一样就映射。
resultMap对结果集二次封装,根据需求来对结果集合分装。
<mapper namespace="com.shouthwind.repository.StudentRepository">
<resultMap id="studentMap" type="com.shouthwind.entity.Student">
<result column="sSno" property="Sno"></result>
<result column="sSname" property="Sname"></result>
<result column="sSsex" property="Ssex"></result>
<result column="sSage" property="Sage"></result>
<result column="sSdept" property="Sdept"></result>
<association property="sc" javaType="com.shouthwind.entity.Sc">
<result column="cSno" property= "Sno"></result>
<result column="cCno" property="Cno"></result>
<result column="cGrade" property="Grade"></result>
</association>
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="studentMap">
select s.Sno sSno,s.Sname sSname,s.Ssex sSsex,s.Sage sSage,s.Sdept sSdept,c.Sno cSno,c.Cno cCno,c.Grade cGrade from student s,sc c where s.Sno=c.Sno and s.Sno=#{Sno}
</select>
</mapper>
Class:
package com.shouthwind.repository;
import com.shouthwind.entity.Sc;
public interface ClassRepository {
public Sc finById(Integer Cno);
}
<?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.shouthwind.repository.ClassRepository">
<resultMap id="classMapper" type="com.shouthwind.entity.Sc">
<id property="Cno" column="cCno"></id>
<result property="Sno" column="cSno"></result>
<result property="Grade" column="cGrade"></result>
<collection property="students" ofType="com.shouthwind.entity.Student">
<result column="sSno" property="Sno"></result>
<result column="sSname" property="Sname"></result>
<result column="sSsex" property="Ssex"></result>
<result column="sSage" property="Sage"></result>
<result column="sSdept" property="Sdept"></result>
</collection>
</resultMap>
<select id="finById" resultMap="classMapper">
select c.Sno cSno,c.Cno cCno,c.Grade cGrade,s.Sno sSno,s.Sname sSname,s.Ssex sSsex,s.Sage sSage,s.Sdept sSdept from sc c,student s where c.Cno=s.Cno and c.Cno=#{Cno}
</select>
</mapper>
注意:
1.association是封装成一个实体类:javaType设置数据类型
2.collection是封装成一个集合:ofType设置数据类型
多对多
多对多是一种双向的关系
1.建表
mysql> create table account_course(
-> id int(11) not null primary key auto_increment,
-> aid int(11) default null,
-> cid int(11) default null,
-> key aid(aid),
-> key cid(cid),
-> constraint account_course_ibfk_1 foreign key(aid) references account(id),
-> constraint account_course_ibfk_2 foreign key(aid) references t_course(id)
-> );
mysql> create table t_course(
-> id int(11) not null primary key auto_increment,
-> name varchar(11) default null
-> );
2.实体类
package com.shouthwind.entity;
import lombok.Data;
import java.util.List;
@Data
public class Account {
private Integer id;
private String name;
private List<Course> courses;
}
package com.shouthwind.entity;
import java.util.List;
public class Course {
private Integer id;
private String name;
private List<Student> students;
}
3.学生选课:
学生------》课
package com.shouthwind.repository;
import com.shouthwind.entity.Account;
public interface AccountRepository {
public Account findById(Integer id);
}
<?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.shouthwind.repository.AccountRepository">
<resultMap id="accoutMap" type="com.shouthwind.entity.Account">
<id column="aid" property="id"></id>
<result column="aname" property="name"></result>
<collection property="courses" ofType="com.shouthwind.entity.Course">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
</collection>
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="accoutMap">
select a.id aid,a.name aname,c.id cid,c.name cname from account a,t_course c,account_course ac where a.id=#{id} and a.id=ac.aid and c.id=ac.cid
</select>
</mapper>
课程-------》学生
package com.shouthwind.repository;
import com.shouthwind.entity.Course;
public interface CourseRepository {
public Course findById(Integer id);
}
<?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.shouthwind.repository.CourseRepository">
<resultMap id="courseMap" type="com.shouthwind.entity.Course">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
<collection property="accounts" ofType="com.shouthwind.entity.Account">
<id column="aid" property="id"></id>
<result column="aname" property="name"></result>
</collection>
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="courseMap">
select a.id aid,a.name aname,c.id cid,c.name cname from account a,t_course c,account_course ac where c.id=#{id} and a.id=ac.aid and c.id=ac.cid
</select>
</mapper>
Mybatis的常用配置-多表关联查询的更多相关文章
- JAVA入门[9]-mybatis多表关联查询
概要 本节要实现的是多表关联查询的简单demo.场景是根据id查询某商品分类信息,并展示该分类下的商品列表. 一.Mysql测试数据 新建表Category(商品分类)和Product(商品),并插入 ...
- MyBatis学习总结(三)——多表关联查询与动态SQL
在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...
- mybatis多表关联查询之resultMap单个对象
resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...
- Spring Boot入门系列(十七)整合Mybatis,创建自定义mapper 实现多表关联查询!
之前讲了Springboot整合Mybatis,介绍了如何自动生成pojo实体类.mapper类和对应的mapper.xml 文件,并实现最基本的增删改查功能.mybatis 插件自动生成的mappe ...
- 三、mybatis多表关联查询和分布查询
前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...
- MyBatis 中两表关联查询MYSQL (14)
MyBatis 中两表关联查询MYSQL 1.创建数据库表语句 2.插入测试数据 3.pom文件内容 <?xml version="1.0" encoding="U ...
- MyBatis 多表关联查询
多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...
- Mybatis多表关联查询字段值覆盖问题
一.错误展示 1.首先向大家展示多表关联查询的返回结果集 <resultMap id="specialdayAndWorktimeMap type="com.hierway. ...
- Mybatis学习系列(五)关联查询
前面几节的示例基本都是一些单表查询,实际项目中,经常用到关联表的查询,比如一对一,一对多等情况.在Java实体对象中,一对一和一对多可是使用包装对象解决,属性使用List或者Set来实现,在mybat ...
- Mybatis-plus多表关联查询,多表分页查询
学习plus真的觉得写代码真的越来越舒服了.昨天开始接触吧,只要学会了多表关联查询.plus就能随意搭配使用了. 关于怎么搭建的就自行了去研究了哦.这里直接进入主题. 我用的是springboot+m ...
随机推荐
- MySQL进阶实战2,那些年学过的事务
@ 目录 一.MySQL服务器逻辑架构 二.并发控制 1.读写锁 2.锁粒度 3.表锁 4.行级锁 三.事务 1.原子性(atomicity) 2.一致性(consistency) 3.隔离性(iso ...
- Kubernetes(K8S) 配置管理-ConfigMap 介绍
作用:存储不加密数据到 etcd,让 Pod 以变量或者 Volume 挂载到容器中 场景:配置文件 创建配置文件 redis.properties redis.host=127.0.0.1 redi ...
- 【PostgreSQL/PGSQL】创建分区表与临时表
一.分区表 1.链接 https://blog.csdn.net/zhangyupeng0528/article/details/119423234 2.分类 列(值)分区表:partition by ...
- 看完这篇,还不懂JAVA内存模型(JMM)算我输
欢迎关注专栏[JAVA并发] 更多技术干活尽在个人公众号--JAVA旭阳 前言 开篇一个例子,我看看都有谁会?如果不会的,或者不知道原理的,还是老老实实看完这篇文章吧. @Slf4j(topic = ...
- 网络基础与osi七层与TCP/IP协议
一 什么是网络 网络:计算机网络是一组计算机或网络设备通过有形 的线缆或无形的媒介如无线,连接起来,按照一定的 规则,进行通信的集合. 通信,是指人与人.人与物.物与物之间通过某种媒 介和行为进行的 ...
- MSTN CE和MSTN SDK安装经验及技巧
MSTN CE和MSTN SDK安装经验及技巧 本文介绍了MSTN CE及MSTN CE SDK安装的流程以及在安装时可能会遇到的问题 一.MSTN CE安装 1.MSTN CE即Microstati ...
- easygui的简单使用——实现猜字谜小游戏
游戏:随机生成个谜底,每轮有3次机会,猜对了结束本轮游戏,猜错了提示猜大了还是猜小了,并显示剩余次数,3次用完后本轮字谜游戏结束,可重新开始猜字谜,也可结束游戏 # 使用 easygui 实现猜字谜游 ...
- Apache HttpClient 5 笔记: SSL, Proxy 和 Multipart Upload
Apache HttpClient 5 最近要在非SpringBoot环境调用OpenFeign接口, 需要用到httpclient, 注意到现在 HttpClient 版本已经到 5.2.1 了. ...
- 《Effective C++》定制new和delete
Item49:了解new_handler的行为 当operator new抛出异常以反映出一个未获得满足的内存需求之前,它会先调用一个用户制定的错误处理函数,一个所谓的new-handler,为了制定 ...
- Java连接Zookeeper以及书写简单增删改查的方法
Java连接Zookeeper以及书写简单增删改查的方法 摘要:本笔记主要记录了使用IDEA创建一个Maven项目并使用Maven配置文件下载Zookeeper连接驱动,连接云服务器的Zookee ...