myBatis 基礎測試 表關聯關係配置 集合 測試
myBatis 基礎測試 表關聯關係配置 集合 測試
測試myelipse專案原始碼 sql 下載 http://download.csdn.net/detail/liangrui1988/5993881
在上一篇做了簡單 增刪改查 的測試,基本程式碼+api 下載 可以看上一遍博文 myBatis 基礎測試 增 刪 改 查 用過hibrenate 之後,感覺很好理解
動行效果:
sql :
CREATE TABLE `student` (
`grade_id` int(11) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_sutids` (`grade_id`),
CONSTRAINT `fk_sutids` FOREIGN KEY (`grade_id`) REFERENCES `grade` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `student` VALUES (1,1,'admin',30,'123'),(1,2,'hello',100,'world'),(1,3,'林沖',45,'aaa'),(1,4,'宋江',55,'123456'),(2,5,'吳用',46,'123456'),(2,6,'武松',30,'3333');
sql:
-- MySQL dump 10.13 Distrib 5.5.20, for Win32 (x86)
--
-- Host: localhost Database: ruiabc
-- ------------------------------------------------------
-- Server version 5.5.20
CREATE TABLE `grade` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`grade_name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `grade` VALUES (1,'157'),(2,'158');
-- Dump completed on 2013-08-22 16:10:48
在裡演示表關聯的查詢方試 注要程式碼
班級 xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.2//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="accp.dao">
<!-- 班集物件 型別 resultMap-->
<resultMap type="Grade" id="gradeRsultMap">
<id property="id" column="id"/>
<result property="grade_name" column="grade_name"/>
<!-- 學生集合 -->
<collection property="stu_list" column="id" javaType="ArrayList"
select="selectStudent" ofType="Student">
</collection>
</resultMap>
<!-- 查詢班級 並且和班級裡的學生 條件-->
<select id="selectGradeAndStudentById" resultMap="gradeRsultMap" parameterType="int">
select * from grade where id=#{id}
</select>
<!-- 查詢班級下的學生 給集合引用 -->
<select id="selectStudent" resultType="Student" parameterType="int">
select * from student where grade_id=#{id}
</select>
<!-- 方式二 -->
<select id="sqlSelectGradeAndStdeentById" resultMap="gradeMapbySql" parameterType="int">
select * from student s inner join grade g on s.grade_id= g.id where g.id=#{id}
</select>
<!-- select g.id,g.grade_name from student s inner join grade g on s.grade_id= g.id where g.id=#{id} -->
<resultMap type="Grade" id="gradeMapbySql">
<id property="id" column="id"/>
<result property="grade_name" column="grade_name"/>
<!-- 學生集合 型別的 引用 column="id" -->
<collection property="stu_list" ofType="Student" resultMap="StudentRsultMap" />
</resultMap>
<!-- 學生集合 型別的 -->
<resultMap type="Student" id="StudentRsultMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="password" column="password"/>
</resultMap>
<!-- select grade by id select="selectGradeById" -->
<select id="selectGradeById" parameterType="int" resultType="Grade">
select * from grade where id=#{id}
</select>
<!-- select All grade -->
<select id="selectAllGradeAndStudents" resultType="Grade">
select * from grade
</select>
</mapper>
sqlSession工具程式碼在上一篇裡有,或都下載原始碼
測試程式碼:
package accp.test;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.ibatis.session.SqlSession;
import accp.bean.Grade;
import accp.bean.Student;
import accp.dao.Dao;
import accp.dao.imp.DaoImp;
import accp.util.GetSession;
public class TestRsultMap {
public static void main(String[] args) {
SqlSession sqlSession=GetSession.getInstans().getSqlSession();
/*查詢學生資訊 包括所在班級*/
/*Student stu=sqlSession.selectOne("accp.dao.selectStuAndGrade",2);
System.out.println("學生id: "+stu.getId()+" "+stu.getName()+" "+stu.getPassword()+
" "+stu.getAge()+" 所在班級名稱: "+stu.getGrade_id().getGrade_name()+
" 班級id: "+stu.getGrade_id().getId());*/
//查詢班級資訊 包括班級裡的所有學生
Grade grade=sqlSession.selectOne("accp.dao.selectGradeAndStudentById",1);
System.out.println("班級ID:"+grade.getId()+" name: "+grade.getGrade_name());
System.out.println(" 班級裡的學生: "+grade.getStu_list().size()+" 個 -------------");
System.out.println("id \t 姓名 \t 密碼 \t 年齡");
for(Student tempStu:grade.getStu_list()){
System.out.println(tempStu.getId()+" \t "+tempStu.getName()+" \t "+tempStu.getPassword()+
" \t "+tempStu.getAge());
}
//查詢班級資訊 包括班級裡的所有學生 通過sql關聯查詢方式
/*直接取對應取不出來????沒有第一種方式好用
* Map<String,Grade> map=sqlSession.selectMap("accp.dao.sqlSelectGradeAndStdeentById","id");
System.out.println(map.size());
for(Entry entry:map.entrySet()){
Grade gr=(Grade)entry.getValue();
System.out.println("MapKey "+entry.getKey() +
" mapValue Gr Name:"+ gr.getGrade_name()+
"gr Stu 共幾個:"+gr.getStu_list().size());
}*/
//list-------- test
/* List<Grade> list=sqlSession.selectList("accp.dao.sqlSelectGradeAndStdeentById",1);
for(Grade grade2:list){
System.out.println("班級ID:"+grade2.getId()+" name: "+grade2.getGrade_name());
System.out.println(" 班級裡的學生: "+grade2.getStu_list().size()+" 個 -------------");
System.out.println("id \t 姓名 \t 密碼 \t 年齡");
for(Student tempStu:grade2.getStu_list()){
System.out.println(tempStu.getId()+" \t "+tempStu.getName()+" \t "+tempStu.getPassword()+
" \t "+tempStu.getAge());
}
}*/
}
}
相關文章
- 黑盒測試和白盒測試的關係
- 如何處理多個集合關聯關係時,試試這個方法?
- 測試表的空間壓縮與表空間的關係
- mybatis關聯關係對映MyBatis
- MyBatis基礎:MyBatis關聯查詢(4)MyBatis
- 軟體確認測試、系統測試和驗收測試有什麼區別和關係?
- 軟體測試基礎 (一): 單元測試
- 軟體測試基礎 (一):單元測試
- 效能測試各個指標之間關係指標
- 測試漫談:QA與涉眾的關係
- 關於IOS測試iOS
- 滲透測試基礎知識----MySQL 配置MySql
- 測試工具集合
- 表的關聯關係
- 移動 APP 測試之基礎功能測試流程APP
- 軟體測試基礎
- 單元測試基礎
- laravel 基礎教程 —— 測試Laravel
- mysql~關於mysql分割槽表的測試MySql
- ORACLE包和過程依賴關係測試(轉)Oracle
- 好文分享:軟體測試與世界盃的關係
- jmeter 單介面測試方案(介面無業務關聯)JMeter
- c# throw及try_catch關聯測試C#
- 測試基礎(四)Jmeter基礎使用JMeter
- 滲透測試基礎知識---nginx安全配置Nginx
- 軟體測試黑馬工程師--測試基礎工程師
- 關於安卓 sdk 測試安卓
- 20分鐘理清Maven構建中的測試相關工具的關係Maven
- 物理滲透測試基礎
- 現代語文與軟體測試學的關係
- rman_restore controlfile_and_autobackup之關係測試REST
- 介面測試用例編寫和測試關注點
- 軟考評測師/中級軟考/測試基礎相關思維導圖
- Web測試基礎-Html基礎知識WebHTML
- 移動測試基礎 Android 應用測試總結Android
- 黑盒測試、白盒測試、單元測試、整合測試、系統測試、驗收測試的區別與聯絡...
- 敏捷測試關鍵成功因素敏捷測試
- 關於測試流程的思考