CREATE table teacher(
id int(10) not null,
name varchar(30) default null,
primary key(id)
)engine=InnoDB default charset=utf8mb3;
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),
key fktid (tid),
constraint fktid foreign key (tid) references teacher (id)
)engine=InnoDB default charset=utf8mb4;
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);
insert into student(id,name,tid) values(6,'小青',1);
package com.heerlin.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;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//创建数据库会话sqlSessionFactory
String resource = "mybatis-config.xml";
//输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// 定义一个公共的静态方法,用于获取 SqlSession 对象
public static SqlSession getSqlSession() {
// 打开一个新的 SqlSession 会话
return sqlSessionFactory.openSession();
}
}
package com.heerlin.dao;
public interface StudentMapper {
}
package com.heerlin.dao;
public interface TeacherMapper {
}
package com.heerlin.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
//学生需要关联一个老师
private Teacher teacher;
}
package com.heerlin.pojo;
import lombok.Data;
@Data
public class Teacher {
private int id;
private String name;
}
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
写sql
方式一子查询
方式二按照结果嵌套处理:
测试1:
@Test
public void testStudent()
{
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List studentList=mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
测试2:
一个老师多个学生
方法一:嵌套查询
sql
测试:
@Test
public void test()
{
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
结果:
方法二:子查询
加接口:
写sql
测试:
@Test
public void test2()
{
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher2(1);
System.out.println(teacher);
sqlSession.close();
}
结果:
小结:
1.关联 association 多对一
2.集合 collection 一对多
javaType 用来指定实体类中属性的类型
ofType 用来指定映射到List或集合中的pojo类型,泛型中的约束类型