myBatis 基础测试 表关联关系配置 集合 测试
来源:互联网 发布:淘宝扣48分会怎么样 编辑:程序博客网 时间:2024/06/10 02:17
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 version5.5.20CREATE 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配置测试
- dapper关联关系查询小测试
- MyBatis基本配置与测试
- mybatis 多表关联配置
- mybatis一对一关联关系
- mybatis 实现关联关系
- MyBatis--关联关系查询
- MyBatis关系关联映射
- mybatis的关联关系
- 多对多关联xml配置测试
- Mybatis关联、集合查询
- 创建MyBatis基础框架和测试
- freeRadius 基础配置及测试
- SSH_Hibernate,基础配置和测试
- 黑马入学测试—基础—集合
- Mybatis的配置以及实例测试
- 用ATL建立轻量级的COM对象(三)
- 移植一个开源点餐网到SAE平台上
- 数据结构 之 索引技术(线性、倒排、动态检索B+、位索引、红黑树)
- ios 三种随机数生成
- Android应用开发学习笔记之绘图
- myBatis 基础测试 表关联关系配置 集合 测试
- html5笔记2 canvas
- 用 imports 改变行为
- 手把手教你修改pcduino系统默认的音频输出
- 《Effective STL中文版》译序
- VS2010+QT5.1+opencv2.4.5图像界面第一个程序
- 用ATL建立轻量级的COM对象(四)
- STM32 的并口总线的所有接口类型解决方法
- C# for Android