1、测试环境搭建
-
导入Lombokjar包;
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.18</version> </dependency>
-
新建实体类Student,Teacher;
package com.beyond.pojo; import lombok.Data; @Data public class Student { private int id; private String name; private int tid; }
package com.beyond.pojo; import lombok.Data; import java.util.List; @Data public class Teacher { private int id; private String name; //一个老师集合很多学生 private List<Student> students; }
-
建立Mapper接口;
package com.beyond.dao; import com.beyond.pojo.Teacher; import java.util.List; public interface TeacherMapper { //查询老师 List<Teacher> getTeacher(); }
-
建立Mapper.xml文件【注意:在resource目录将Mapper.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.beyond.dao.StudentMapper"> </mapper>
<?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.beyond.dao.TeacherMapper"> <select id="getTeacher" resultType="Teacher"> select * from teacher </select> </mapper>
-
在核心配置文件中绑定注册我们的Mapper接口或者文件;
<?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:mybatis核心配置文件--> <configuration> <!--引入外部配置文件--> <properties resource="db.properties"> <property name="username" value="root"/> </properties> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!--可以给实体类起别名--> <typeAliases> <typeAlias type="com.beyond.pojo.Student" alias="Student"/> <typeAlias type="com.beyond.pojo.Teacher" alias="Teacher"/> <!-- <package name="com.beyond.pojo"/>--> </typeAliases> <!--environments:环境--> <environments default="development"> <environment id="development"> <!--transactionManager:事务--> <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> <environment id="test"> <!--transactionManager:事务--> <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.beyond.dao.TeacherMapper"/> <mapper class="com.beyond.dao.StudentMapper"/> </mappers> </configuration>
-
测试查询是否成功。
import com.beyond.dao.TeacherMapper; import com.beyond.pojo.Teacher; import com.beyond.utils.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class MyTest { public static void main(String[] args) { SqlSession sqlSession = MyBatisUtil.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); List<Teacher> teacherList = mapper.getTeacher(); for (Teacher teacher:teacherList) { System.out.println(teacher); } sqlSession.close(); } }
2、按照查询嵌套处理
-
编写Mapper接口;
package com.beyond.dao; import com.beyond.pojo.Teacher; import org.apache.ibatis.annotations.Param; import java.util.List; public interface TeacherMapper { //获取指定老师集合下的所有学生及老师信息 Teacher getTeacher2(@Param("id") int id); }
-
编写Mapper.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.beyond.dao.TeacherMapper"> <!--方法一(按照查询嵌套处理(类似于子查询)) 思路: 1、查询指定的老师的所有信息; 2、根据查询出来的老师的id,寻找对应的学生 --> <select id="getTeacher2" resultMap="StudentTeacher"> select * from teacher t where id=#{id} </select> <resultMap id="StudentTeacher" type="Teacher"> <!--复杂的属性,我们需要单独处理 对象:association 集合:collection javaType:指定实体类中属性的类型 ofType:指定映射到List或者集合中实体类类型,泛型中的约束类型 --> <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/> </resultMap> <select id="getStudentByTeacherId" resultType="Student"> select * from student s where tid=#{id} </select> </mapper>
-
测试查询是否成功。
import com.beyond.dao.TeacherMapper; import com.beyond.pojo.Teacher; import com.beyond.utils.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class MyTest { @Test public void getTeacher2(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher = mapper.getTeacher2(1); System.out.println(teacher); sqlSession.close(); } }
3、按照结果嵌套处理(推荐使用)
-
编写Mapper接口;
package com.beyond.dao; import com.beyond.pojo.Teacher; import org.apache.ibatis.annotations.Param; import java.util.List; public interface TeacherMapper { //获取指定老师集合下的所有学生及老师信息 Teacher getTeacher2(@Param("id") int id); }
-
编写Mapper.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.beyond.dao.TeacherMapper"> <!--方法二:按照结果嵌套处理(类似于联表查询)--> <select id="getTeacher2" resultMap="StudentTeacher2"> select t.id tid,t.name tname,s.id sid,s.name sname from student s,teacher t where s.tid=t.id and t.id=#{id} </select> <resultMap id="StudentTeacher2" type="Teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <!-- javaType:指定实体类中属性的类型 ofType:指定映射到List或者集合中实体类类型,泛型中的约束类型 --> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> </mapper>
-
测试查询是否成功。
import com.beyond.dao.TeacherMapper; import com.beyond.pojo.Teacher; import com.beyond.utils.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class MyTest { @Test public void getTeacher2(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher = mapper.getTeacher2(1); System.out.println(teacher); sqlSession.close(); } }
4、小结
- 对象:association 【多对一】
- 集合:collection 【一对多】
- javaType:指定实体类中属性的类型
- ofType:指定映射到List或者集合中实体类类型,泛型中的约束类型
面试高频:
- MySQL引擎;
- InnoDB底层原理;
- 索引;
- 索引优化…