c++運算子過載
阿新 • • 發佈:2022-03-20
- 模擬測試:一名老師有多名學生 => 【面向教師】
- 本質:使用<collection>標籤完成一對多的輸出
建立實體類
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int tid;
}
import lombok.Data; import java.util.List; @Data public class Teacher { private int id; private String name; //一個老師擁有多個學生 private List<Student> students; }
建立TeacherMapper方法介面
public interface TeacherMapper {
//獲取指定老師下所有學生及老師資訊(方法一)
Teacher getTeacherS2(@Param("tid") int id);
//獲取指定老師下所有學生及老師資訊(方法二)
Teacher getTeacherS(@Param("tid") int id);
}
建立TeacherMapper.xml Sql查詢及結果對映
<?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.zhang.dao.TeacherMapper"> <!--按查詢巢狀一對多--> <resultMap id="TS2" type="teacher"> <result column="id" property="id" /> <result column="name" property="name" /> <collection property="students" ofType="student" select="getStudentByTeacherId" column="id"> </collection> </resultMap> <select id="getTeacherS2" resultMap="TS2"> select * from mybatis.teacher where id=#{tid} </select> <select id="getStudentByTeacherId" resultType="student"> select * from mybatis.student where tid=#{tid} </select> <!--*********************************************--> <!--按結果巢狀一對多--> <select id="getTeacher" resultType="teacher"> select * from mybatis.teacher </select> <resultMap id="TS" type="teacher"> <result property="id" column="tid" /> <result property="name" column="tname" /> <!--javaType是個指定屬性的型別 集合中的泛型要用ofType獲取 --> <collection property="students" ofType="student"> <result property="id" column="sid" /> <result property="name" column="sname" /> <result property="tid" column="tid" /> </collection> </resultMap> <select id="getTeacherS" resultMap="TS"> select s.id sid,s.name sname,t.id tid ,t.name tname from mybatis.student s,mybatis.teacher t where s.tid=t.id and t.id=#{tid} </select> </mapper>
建立測試方法
@Test public void Test02(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacherS = mapper.getTeacherS(1); System.out.println(teacherS); sqlSession.close(); } @Test public void Test03(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacherS = mapper.getTeacherS2(1); System.out.println(teacherS.toString()); sqlSession.close(); }
03測試結果
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
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 2035070981.
==> Preparing: select * from mybatis.teacher where id=?
==> Parameters: 1(Integer)
<== Columns: id, name
<== Row: 1, 秦老師
====> Preparing: select * from mybatis.student where tid=?
====> Parameters: 1(Integer)
<==== Columns: id, name, tid
<==== Row: 1, 小明, 1
<==== Row: 2, 小紅, 1
<==== Row: 3, 小張, 1
<==== Row: 4, 小李, 1
<==== Row: 5, 小王, 1
<==== Total: 5
<== Total: 1
Teacher(id=1, name=秦老師, students=[Student(id=1, name=小明, tid=1), Student(id=2, name=小紅, tid=1), Student(id=3, name=小張, tid=1), Student(id=4, name=小李, tid=1), Student(id=5, name=小王, tid=1)])
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@794cb805]
Returned connection 2035070981 to pool.
程序已結束,退出程式碼0
02測試結果
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
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 2035070981.
==> Preparing: select s.id sid,s.name sname,t.id tid ,t.name tname from mybatis.student s,mybatis.teacher t where s.tid=t.id and t.id=?
==> Parameters: 1(Integer)
<== Columns: sid, sname, tid, tname
<== Row: 1, 小明, 1, 秦老師
<== Row: 2, 小紅, 1, 秦老師
<== Row: 3, 小張, 1, 秦老師
<== Row: 4, 小李, 1, 秦老師
<== Row: 5, 小王, 1, 秦老師
<== Total: 5
Teacher(id=1, name=秦老師, students=[Student(id=1, name=小明, tid=1), Student(id=2, name=小紅, tid=1), Student(id=3, name=小張, tid=1), Student(id=4, name=小李, tid=1), Student(id=5, name=小王, tid=1)])
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@794cb805]
Returned connection 2035070981 to pool.
程序已結束,退出程式碼0