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.0modelVersion> <groupId>org.examplegroupId> <artifactId>MybatisTest04artifactId> <version>1.0-SNAPSHOTversion> <dependencies> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.18version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.5.3version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.13version> <scope>testscope> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.18.10version> <scope>providedscope> dependency> dependencies> <build> <resources> <resource> <directory>src/main/javadirectory> <includes> <include>**/*.propertiesinclude> <include>**/*.xmlinclude> includes> <filtering>falsefiltering> resource> <resource> <directory>src/main/resourcesdirectory> <includes> <include>**/*.propertiesinclude> <include>**/*.xmlinclude> includes> <filtering>falsefiltering> 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); Listgetallstudent(); }
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配置文件:
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="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); Lists = mapper.getallstudent(); for (student s2 : s){ System.out.println(s2); } sqlSession.close(); } }
结果:
下一章说一对多的处理