Mybatis入門-04-多對一
一、 前言
一切當以官方文件為基準。
參考視訊:
在我學習MySQL的時候,老師告訴我儘量不要使用外來鍵,而是在JDBC程式碼中用邏輯程式碼去替代他。
至於原話,似乎是下面這句:
不得使用外來鍵與級聯,一切外來鍵概念必須在應用層解決。
秉承這樣的概念,在下面的演示中資料庫只有一個“概念上”的外來鍵,實質上是多表查詢。
二、準備工作
2.0 依賴、路徑
為了方便,這裡使用前面留下來的配置:
本文之前的操作步驟:
上面是我喜歡的一些配置,一般來說按自己配置來並不會太影響。
2.1 建立資料庫
create database if not exists `mybatis`; use `mybatis`; create table if not exists `User`( `id` INT(20) not null primary key, `name` VARCHAR(20) default null, `pwd` varchar(20) default null )ENGINE=INNODB default CHARSET = UTF8; insert into `User`(`id`,`name`,`pwd`) values (1,'admin','123456'), (2,'Jax','123456'), (3,'Jinx','123455'), (4,'Query','123456'), (5,'biubiu','123456'); select `id`,`name` from mybatis.user; CREATE TABLE `teacher` ( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO teacher(`id`, `name`) VALUES (1, '秦老師'); CREATE TABLE `student` ( `id` INT(10) NOT NULL, `name` VARCHAR(30) DEFAULT NULL, `tid` INT(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1', '小明', '1'); INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('2', '小紅', '1'); INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('3', '小張', '1'); INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('4', '小李', '1'); INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('5', '小王', '1');
可以看到,沒有定義外來鍵。
大致效果:
2.2 建立實體類
Student.java
package com.duzhuan.pojo; /** * @Autord: HuangDekai * @Date: 2020/9/16 14:45 * @Version: 1.0 * @since: jdk11 */ public class Student { private int id; private String name; private Teacher teacher; public Student() { } public Student(int id, String name, Teacher teacher) { this.id = id; this.name = name; this.teacher = teacher; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", teacher=" + teacher + '}'; } } //省略空間,省略Getter和Setter方法
Teacher.java
package com.duzhuan.pojo;
/**
* @Autord: HuangDekai
* @Date: 2020/9/16 14:42
* @Version: 1.0
* @since: jdk11
*/
public class Teacher {
private int id;
private String name;
public Teacher() {
}
public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
2.3 MyBatis配置檔案
db.properties:
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC
username = root
password = qq123456
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"></properties>
<settings>
<setting name="logImpl" value="SLF4J"/>
</settings>
<typeAliases>
<package name="com.duzhuan.pojo"/>
</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 class="com.duzhuan.dao.TeacherMapper"></mapper>
<mapper class="com.duzhuan.dao.StudentMapper"></mapper>
</mappers>
</configuration>
注意:
-
此處使用了日誌,不使用日誌
刪除
,或修改為相應日誌,我這裡的配置具體可看Mybatis入門-03-日誌工廠:<settings> <setting name="logImpl" value="SLF4J"/> </settings>
-
此處命名了別名,可以看出是使用了pojo包裡的所有實體類的名稱作為別名。按照規定別名應為全小寫,但是由於mybatis不區分大小寫,之後再Mapper.xml中一般寫時候都是完全使用實體類的類名,如
Student
:<typeAliases> <package name="com.duzhuan.pojo"/> </typeAliases>
-
此處為寫Mapper之後新增:
<mappers> <mapper class="com.duzhuan.dao.TeacherMapper"></mapper> <mapper class="com.duzhuan.dao.StudentMapper"></mapper> </mappers>
2.4 工具類
在一整個程式裡,只需要有一個sqlSessionFactory。
MybatisUtils.java:
package com.duzhuan.utils;
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;
/**
* @Autord: HuangDekai
* @Date: 2020/9/16 12:32
* @Version: 1.0
* @since: jdk11
*/
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String configuration = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(configuration);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
三、Mapper
其中TeacherMapper和TeacherMapper.xml並未用到,只是出於慣例隨便寫的。
TeacherMapper:
package com.duzhuan.dao;
import com.duzhuan.pojo.Teacher;
/**
* @Autord: HuangDekai
* @Date: 2020/9/16 14:56
* @Version: 1.0
* @since: jdk11
*/
public interface TeacherMapper {
Teacher getTeacherById(int id);
}
TeacherMapper.xml
<?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.duzhuan.dao.TeacherMapper">
<resultMap id="StudentMap" type="Student">
</resultMap>
<select id="getTeacherById" resultType="Teacher">
select * from mybatis.teacher where id = #{id}
</select>
</mapper>
StudentMapper:
package com.duzhuan.dao;
import com.duzhuan.pojo.Student;
import java.util.List;
/**
* @Autord: HuangDekai
* @Date: 2020/9/16 14:55
* @Version: 1.0
* @since: jdk11
*/
public interface StudentMapper {
List<Student> getStudentList();
}
StudentMapper.xml(關鍵):
<?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.duzhuan.dao.StudentMapper">
<select id="getStudentList" resultMap="StudentTeacher">
select s.id id,s.name sname,t.id tid,t.name tname
from mybatis.student s, mybatis.teacher t
where s.tid = t.id
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
-
由於Student與Teacher都有id,且欄位名都為
id
,因此不得不使用別名。select s.id id,s.name sname,t.id tid,t.name tname from mybatis.student s, mybatis.teacher t where s.tid = t.id
這也方便MyBatis對映
-
<resultMap id="StudentTeacher" type="Student">
顯然,Student
就是使用了之前在mybatis-config.xml
中設定好的別名,而不是使用全限定名 -
在這裡,
property
的屬性是實體類中對應的屬性名,column
對應的是SQL查詢語句查詢的欄位名(應該是資料庫中的欄位名,由於有重名,因此使用了查詢語句中設好的別名,Mybatis會自動對映)由於type
的值是Student
,即可以視為返回一個(多個)Student
物件。<result property="id" column="id"/> <result property="name" column="sname"/>
-
這裡由於返回的不是一個屬性,而是一個物件,因此使用
association
,依舊得,在<association>
中property
的值依舊是對應的類的名稱,javaType
表示返回一個Teacher
的例項(物件),裡面的小標籤是Teacher
這個實體類裡面的屬性(顯然,如果Teacher
裡面聚合有物件的話,需要再在<association>
裡面巢狀一個<association>
):<association property="teacher" javaType="Teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> </association>
四、測試類
其中TeacherMapperTest為空。
StudentMapperTest:
package com.duzhuan.dao;
import com.duzhuan.pojo.Student;
import com.duzhuan.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
/**
* @Autord: HuangDekai
* @Date: 2020/9/16 23:14
* @Version: 1.0
* @since: jdk11
*/
public class StudentMapperTest {
@Test
public void getStudentListTest(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudentList();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
}