复杂环境搭建:
1.1建表:
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);
1.2工具类等基本设置:
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();
}
}
1.3实体类和接口:
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;
}
1.4 编写xml配置文件:(添加到resource里但保证包结构相同)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.heerlin.dao.StudentMapper">
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.heerlin.dao.TeacherMapper">
</mapper>
1.5绑定:
1.6初步测试:
ok初步测试成功:
多对一的处理:
需求:查询所有学生信息,以及对应的老师信息
2.1写接口:
写sql
方式一子查询
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.heerlin.dao.StudentMapper">
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--复杂属性需要单独处理 对象: association 集合: collection-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{id}
</select>
</mapper>
方式二按照结果嵌套处理:
<!-- 按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
测试1:
@Test
public void testStudent()
{
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList=mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
测试2:
一对多的处理:
一个老师多个学生
方法一:嵌套查询
sql
<!-- 案结果嵌套查询-->
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname,t.id tid
from student s,teacher t
where s.tid=t.id and t.id=#{tid}
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!--复杂属性需要单独处理 对象: association 集合: collection javaType="" 指定属性的类型
集合中的泛型信息,我们使用ofType获取
-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="snmae"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
测试:
@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
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from mybatis.teacher where id=#{tid}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select *from mybatis.student where tid=#{tid}
</select>
测试:
@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类型,泛型中的约束类型