多对一的处理
大约 3 分钟数据库技术Mybatis
1、需求:查询所有的学生信息,以及对应的老师的信息
接口StudentMapper.java新增方法
/**
* @Description TODO
* @Author Administrator
* @Date 2020/11/28 13:42
*/
public interface StudentMapper {
/**
* 查询所有的学生信息,以及对应的老师的信息
* 按照查询嵌套
* @return 学生信息
*/
public List<Student> getStudent();
/**
* 查询学生所有信息
* 按照结果嵌套
* @return
*/
public List<Student> getStudent2();
}
mapper文件StudentMapper.xml修改
(1)按照查询嵌套
<?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.kuang.dao.StudentMapper">
<!--1、按照查询嵌套-->
<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>
(2)按照结果嵌套处理
<?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.kuang.dao.StudentMapper">
<!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.id tid,t.name tname
from student s, teacher t
where t.id = s.tid
</select>
<resultMap id="StudentTeacher2" type="student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
mybatis-config.xml注册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>
<!--引入外部配置文件-->
<properties resource="db.properties">
<property name="username" value="root"/>
<property name="password" value="13256"/>
</properties>
<!--设置-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--类型别名-->
<typeAliases>
<package name="com.kuang.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>
<!--注册mapper-->
<mappers>
<!--teacher对应的mapper-->
<mapper resource="com/kuang/dao/TeacherMapper.xml"/>
<!--student对应的mapper-->
<mapper class="com.kuang.dao.StudentMapper"/>
</mappers>
</configuration>
测试
@Test
public void test02(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentMapper.getStudent();
students.forEach(System.out::println);
sqlSession.close();
}
结果
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Class not found: org.jboss.vfs.VFS
JBoss 6 VFS API is not available in this environment.
Class not found: org.jboss.vfs.VirtualFile
VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
Using VFS adapter org.apache.ibatis.io.DefaultVFS
Find JAR URL: file:/E:/code/Java/mybatis-study/mybatis-05/target/classes/com/kuang/pojo
Not a JAR: file:/E:/code/Java/mybatis-study/mybatis-05/target/classes/com/kuang/pojo
Reader entry: Student.class
Reader entry: Teacher.class
Listing file:/E:/code/Java/mybatis-study/mybatis-05/target/classes/com/kuang/pojo
Find JAR URL: file:/E:/code/Java/mybatis-study/mybatis-05/target/classes/com/kuang/pojo/Student.class
Not a JAR: file:/E:/code/Java/mybatis-study/mybatis-05/target/classes/com/kuang/pojo/Student.class
Reader entry: ���� 4 ^
Find JAR URL: file:/E:/code/Java/mybatis-study/mybatis-05/target/classes/com/kuang/pojo/Teacher.class
Not a JAR: file:/E:/code/Java/mybatis-study/mybatis-05/target/classes/com/kuang/pojo/Teacher.class
Reader entry: ���� 4 L
Checking to see if class com.kuang.pojo.Student matches criteria [is assignable to Object]
Checking to see if class com.kuang.pojo.Teacher matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 525968792.
==> Preparing: select * from student
==> Parameters:
<== Columns: id, name, tid
<== Row: 1, 小明, 1
====> Preparing: select * from teacher where id = ?
====> Parameters: 1(Integer)
<==== Columns: id, name
<==== Row: 1, 秦老师
<==== Total: 1
<== Row: 2, 小红, 1
<== Row: 3, 小张, 1
<== Row: 4, 小李, 1
<== Row: 5, 小王, 1
<== Total: 5
Student(id=1, name=小明, teacher=Teacher(id=1, name=秦老师))
Student(id=2, name=小红, teacher=Teacher(id=1, name=秦老师))
Student(id=3, name=小张, teacher=Teacher(id=1, name=秦老师))
Student(id=4, name=小李, teacher=Teacher(id=1, name=秦老师))
Student(id=5, name=小王, teacher=Teacher(id=1, name=秦老师))
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1f59a598]
Returned connection 525968792 to pool.
Process finished with exit code 0