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 姓名 密码 年龄"); for(Student tempStu:grade.getStu_list()){ System.out.println(tempStu.getId()+" "+tempStu.getName()+" "+tempStu.getPassword()+ " "+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 姓名 密码 年龄"); for(Student tempStu:grade2.getStu_list()){ System.out.println(tempStu.getId()+" "+tempStu.getName()+" "+tempStu.getPassword()+ " "+tempStu.getAge()); } }*/ } }