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)
)

目录:

Mybatis入门(六)联查之多对一-LMLPHP

文件很多但也不复杂静下心来慢慢想就能捋清楚。

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入门(六)联查之多对一-LMLPHP

下一章说一对多的处理

05-11 18:39