Mybatis入门(六)联查之多对一
Mysql可以联查,但Mybatis也可以联查只是没有MySQL联查的舒服需要配置很多文件。
开始搭建环境:
MySQL新建两个表一个Student一个Teacher表:
Teacher表:
CREATE TABLE teacher(
tid int primary key,
tname varchar(30) not null
)
Student表:
CREATE TABLE student(
sid int primary key,
sname varchar(30) not null,
tid int not null,
foreign key(tid) references teacher(tid)
)
目录:

文件很多但也不复杂静下心来慢慢想就能捋清楚。
pom.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>MybatisTest04</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--导入MySQL包/核心-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency> <!--导入Mybatis包/核心-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency> <!--导入junit测试包-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!--偷懒专用包-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency> </dependencies> <!--maven由于他的约定大于配置,我们之后可以能遇到我们写的配置文件,无法被处理或生效问题,解决方案-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build> </project>
Mybatis-config.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/> <typeAliases>
<typeAlias type="com.hdlf.pojo.student" alias="student"></typeAlias>
<typeAlias type="com.hdlf.pojo.teacher" alias="teacher"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="com/hdlf/dao/StudnetMapper.xml"></mapper>
<mapper class="com.hdlf.dao.TeacherMapper"></mapper>
</mappers>
</configuration>
utils工具类:
package com.hdlf.utlis; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException;
import java.io.InputStream; public class MybatisUtlis { private static SqlSessionFactory sqlSessionFactory;
static{
try {
String resource = "Mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
} public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
StudentMapper接口:
package com.hdlf.dao;
import com.hdlf.pojo.student;
import java.util.List;
public interface StudentMapper {
student getStudent(int tid);
List<student> getallstudent();
}
TeacherMapper接口:
package com.hdlf.dao;
import com.hdlf.pojo.teacher;
public interface TeacherMapper {
teacher getteacher(int tid);
}
student实体类:
package com.hdlf.pojo;
public class student {
private int sid;
private String sname;
private teacher teacher;
public student() {
}
public student(int sid, String sname, com.hdlf.pojo.teacher teacher) {
this.sid = sid;
this.sname = sname;
this.teacher = teacher;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public com.hdlf.pojo.teacher getTeacher() {
return teacher;
}
public void setTeacher(com.hdlf.pojo.teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", teacher=" + teacher +
'}';
}
}
teacher实体类:
package com.hdlf.pojo;
public class teacher {
private int tid;
private String tname;
public teacher() {
}
public teacher(int tid, String tname) {
this.tid = tid;
this.tname = tname;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "teacher{" +
"tid=" + tid +
", tname='" + tname + '\'' +
'}';
}
}
搭建完环境就该操作了,接下来很重要:
StudnetMapper.xml配置文件:
<association>装配对象时使用,其中 property 表示类中的属性名 select 表示要执行的sql语句(写完整的sql语句) column 要传过去的字段参数 javaType指定这个实体类
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hdlf.dao.StudentMapper"> <select id="getStudent" parameterType="int">
select * from mybatis.student where sid = #{tid}
</select> <select id="getallstudent" resultMap="getallstudentmap">
select * from mybatis.student
</select> <resultMap id="getallstudentmap" type="student">
<result property="sid" column="sid"></result>
<result property="sname" column="sname"></result>
<!--<association>装配对象时使用,其中 property 表示类中的属性名
select 表示要执行的sql语句(写完整的sql语句) column 要传过去的字段参数
javaType指定这个实体类-->
<association property="teacher" column="tid" javaType="teacher" select="getteacher"></association>
</resultMap> <select id="getteacher" resultType="teacher">
select * from mybatis.teacher where tid = #{tid}
</select>
</mapper>
测试类:
MyTest:
public class MyTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtlis.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<student> s = mapper.getallstudent();
for (student s2 : s){
System.out.println(s2);
}
sqlSession.close();
}
}
结果:

下一章说一对多的处理
Mybatis入门(六)联查之多对一的更多相关文章
- Mybatis入门(六)联查之一对多
上一章说了多对一,很多学生被一个老师教,这一章是一个老师教很多学生 目录基本没有变化只是改了配置文件: 2.配置文件: TeacherMapper接口类: package com.hdlf.dao; ...
- mybatis入门_一对多,多对多映射以及整合spring框架
一.一对多映射. 1.1 一对多映射之根据多的一方关联查询一的一方 示例:查询出具体的订单信息,同时也查询出来订单的用户信息. 引入的订单表如下所示: 框选出来的为具体的外键. 订单的Pojo类如下所 ...
- <MyBatis>入门六 动态sql
package org.maple.mapper; import org.apache.ibatis.annotations.Param; import org.maple.pojo.Employee ...
- Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...
- Mybatis(一) mybatis入门
学习了hibernate这个持久层框架之后,在来学习Mybatis简直是无压力,因为Mybatis入门门栏很低,如果学习过了hibernate的话,对于Mybatis的学习很简单了,如果没学习过hib ...
- Mybatis学习(一)—————mybatis入门
学习了hibernate这个持久层框架之后,在来学习Mybatis简直是无压力,因为Mybatis入门门栏很低,如果学习过了hibernate的话,对于Mybatis的学习很简单了,如果没学习过hib ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
- MyBatis入门(五)---延时加载、缓存
一.创建数据库 1.1.建立数据库 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.7.9-log : Database - mybatis ****** ...
- MyBatis入门(二)---一对一,一对多
一.创建数据库表 1.1.创建数据表同时插入数据 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.6.27-log : Database - mybati ...
随机推荐
- C:数值溢出问题
当超过一个数据类型能够存放最大的范围时,数值会溢出. 有符号位最高位溢出的区别:符号位溢出会导致数的正负发生改变,但最高位的溢出会导致最高位丢失. #include <stdio.h> i ...
- SPOJ QTREE Query on a Tree【树链剖分模板题】
树链剖分,线段树维护~ #include <cstdio> #include <cstring> #include <iostream> #include < ...
- Cisco AP-胖瘦AP的转换
一.瘦AP到胖AP的转换:1.登录到LAP协商的WLC2.从LAP模式恢复到胖AP,输入下面命令:config ap tftp tftp_server_ip filename ap_name注意:需要 ...
- mqtt开源服务器 EMQX ,客户端MQTTX5.0,使用指南
服务器 EMQX 官网: https://docs.emqx.io/broker/v3/cn/getstarted.html#mqtt-clients 一.安装启动 # 各平台下载https://ww ...
- XCOJ 1249: 全自动奖学金计算系统
1249: 全自动奖学金计算系统 时间限制: 1 Sec 内存限制: 64 MB提交: 305 解决: 54 标签提交统计讨论版 题目描述 宣城校区从今年开始,在原有奖学金制度上,设立专项奖学金. ...
- 【NGINX】LINUX安装NGINX
安装依赖() · yum install gcc · yum install pcre-devel · yum install zlib zlib-devel · yum install openss ...
- mysql操作之密码的那点小事
mysql 修改密码的2种方式: 进入mysql库的user表中修改 update mysql.user password = password("新密码") where 条件; ...
- Vue——解决报错 Computed property "****" was assigned to but it has no setter.
在最近的项目中遇到了如下的警告信息: [Vue warn]: Computed property " currentStep" was assigned to but it has ...
- Java 并发锁
Java 中的锁 阻塞锁.可重入锁.读写锁.互斥锁.悲观锁.乐观锁.公平锁.偏向锁.对象锁.线程锁.锁粗化.锁消除.轻量级锁.重量级锁.信号量.独享锁.共享锁.分段锁 一.常见的锁 synchroni ...
- windows下pycharm连接vagrant的python环境