association----------第6条使用

1.目录结构

2.Mybatis-config.xml配置

<!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd">  <!-- xml核心配置 -->
<configuration>
<!-- 日志的具体实现 --><settings><setting name="logImpl" value="LOG4J"/></settings><!-- 环境配置 --><environments default="mysql"><environment id="mysql"><!-- 指定事务回滚 --><transactionManager type="JDBC"></transactionManager><!-- 指数据源配置 --><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/user"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments>
<!-- 告诉mybatis去哪里找映射文件 --><mappers><!-- <mapper resource="org/mapper/UserMapper.xml"/> --><mapper resource="org/mapper/studentMapper.xml"/></mappers>
</configuration>

3.班级类

package org.domain;public class ClassGrade {private int id;           private String code;   //班级编号public ClassGrade() {super();
}
public ClassGrade(int id, String code) {super();this.id = id;this.code = code;
}
public int getId() {return id;
}
public void setId(int id) {this.id = id;
}
public String getCode() {return code;
}
public void setCode(String code) {this.code = code;
}}

4.学生类

package org.domain;
public class student {private int id;private String name;private String sex;private int age;private int class_id;private ClassGrade classGrade;public student() {super();}public student(int id, String name, String sex, int age) {super();this.id = id;this.name = name;this.sex = sex;this.age = age;}public student(int id, String name, String sex, int age, int class_id) {super();this.id = id;this.name = name;this.sex = sex;this.age = age;this.class_id = class_id;}public student(int kid, String name, String sex, int age,ClassGrade classGrade) {super();this.name = name;this.sex = sex;this.age = age;this.classGrade = classGrade;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getClass_id() {return class_id;}public void setClass_id(int class_id) {this.class_id = class_id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public ClassGrade getClassGrade() {return classGrade;}public void setClassGrade(ClassGrade classGrade) {this.classGrade = classGrade;}
}

5.

sqlSessionFactory类
package org.factory;import java.io.IOException;
import java.io.InputStream;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class sqlSessionFactory {//SqlSessionFactory:单个数据库的映射关系编译之后的内存镜像//功能:是创建SqlSession 的工厂private static SqlSessionFactory factory=null;/** 获取 SqlSessionFactory对象的方法*/static {//读取mybatis-config.xml文件InputStream input = null;try {input = Resources.getResourceAsStream("mybatis-config.xml");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//初始化mybatisfactory=new SqlSessionFactoryBuilder().build(input);}/** 获取sqlSession对象*/public static SqlSession getSqlSession() {return  factory.openSession(); }/** 获取sqlSessionFactory*/public static SqlSessionFactory getSqlSessionFactory() {return factory;}}

6.studentMapper.xml配置

(配置文件中每一个select针对各自的表)

第一种方式select嵌套:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mapper.studentMapper"><!-- 对象result  tMap学生映射--><resultMap id="studentResultMap" type="org.domain.student" ><id property="id" column="id"/><result property="name" column="name"/><result property="sex" column="sex"/><result property="age" column="age"/><association property="classGrade"  column="class_id" javaType="org.domain.ClassGrade" select="selectClasses"><result property="id" column="id"/><result property="code" column="code"/></association></resultMap><!--班级查询  --><select id="selectClasses"  resultType="org.domain.ClassGrade">select * from class_tb where id=#{id}</select><!--学生查询语句  --><select id="selectStudents" resultMap="studentResultMap">select * from student</select></mapper>

(配置文件中每一个select针对连接一个以上的表)

第二种方式resultMap嵌套:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mapper.studentMapper"><!-- 对象result  tMap学生映射--><resultMap id="studentResultMap" type="org.domain.student" ><id property="id" column="id"/><result property="name" column="name"/><result property="sex" column="sex"/><result property="age" column="age"/><association property="classGrade"  column="class_id" resultMap="class"/>  </resultMap><resultMap  id="class"  type="org.domain.ClassGrade"><result property="id" column="id"/><result property="code" column="code"/></resultMap><!--关联查询学生及班级  --><select id="aSelect"  resultMap="studentResultMap">select  student.id,student.name,student.sex,student.age,class_tb.id as oid,class_tb.codefrom student left outer join class_tb  on student.class_id=class_tb.id; </select>
</mapper>

第三种方式对象嵌套(跟第二种方式没有太大差别只是对象也不需要再关联查询):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mapper.studentMapper"><!-- 对象result  tMap学生映射--><resultMap id="studentResultMap" type="org.domain.student" ><id property="id" column="id"/><result property="name" column="name"/><result property="sex" column="sex"/><result property="age" column="age"/><association property="classGrade"  column="class_id" javaType="org.domain.ClassGrade">  <result property="id" column="id"/><result property="code" column="code"/></association></resultMap><!--关联查询学生及班级  --><select id="aSelect"  resultMap="studentResultMap">select  student.id,student.name,student.sex,student.age,class_tb.id as oid,class_tb.codefrom student left outer join class_tb  on student.class_id=class_tb.id; </select>
</mapper>

association的使用相关推荐

  1. 如何用ABAP代码读取CDS view association的数据

    我有如下一个CDS view, 这个view的数据来自CRMD_ORDERADM_H, 定义了一个名称为_statushelp的association, 指向了另一个CDS view Z_C_Stat ...

  2. 微生物相关网络构建教程中文Microbial association network construction tutorial

    原文为自Microbial association network construction tutorial http://psbweb05.psb.ugent.be/conet/microbial ...

  3. Association Rules 关联规则

    Association Rules 关联规则 除了apriori和FPGrowth目前还有那些方法用来发现关联规则? 关键词: 频繁项集,apriori算法,FPGrowth,关联规则, 频繁项集评估 ...

  4. LDNFSGB: prediction of long non-coding rna and disease association using network feature similarity

    LDNFSGB: prediction of long non-coding rna and disease association using network feature similarity ...

  5. Windows Phone本地数据库(SQLCE):5、[Association]attribute(翻译)(转)

    这是"windows phone mango本地数据库(sqlce)"系列短片文章的第五篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆 ...

  6. mybaits十一:使用association分步查询

    DepartmentMapper.xml sql配置文件 <?xml version="1.0" encoding="UTF-8" ?> <! ...

  7. LINQ那些事儿(2)- 简单对象的CRUD操作和Association的级联操作

    从(1)我们看到,当生成entity class定义时,entity class或xml mapping文件中都已经完整的包含了entity和关系数据库的映射信息了,LINQ2SQL会根据这些信息来把 ...

  8. mybatis association表关联与rowbounds共同使用时的异常及其解决方案

    按照mybatis手册中所说的,association有两种实现方式,嵌套查询和嵌套结果映射.如手册中所述,select方式会带来N+1次查询的问题,考虑到效率问题的话建议使用嵌套结果映射.但是在结合 ...

  9. 论文笔记:Integrating Classification and Association Rule Mining (即,CBA算法介绍)

    1998 KDD 0 摘要 分类规则挖掘旨在发现数据库中的一小组规则,形成一个准确的分类器. 关联规则挖掘发现数据库中存在的所有满足最小支持度和最小置信度约束的规则. 对于关联规则挖掘,发现的目标不是 ...

  10. mybatis元素类型为 “resultMap“ 的内容必须匹配 “(constructor?,id *,result*,association报错解决

    1.前言 太久没写这种套娃式的sql语句了,导致今天一写,直接给我整了个报错. 原因其实蛮简单的,mybatis的xml中的resultMap标签规定了内标签的顺序,写错了就会直接解析不出来,从而报错 ...

最新文章

  1. mysql delete 注意
  2. 朱俊彦团队提出GAN压缩算法:计算量减少20倍,生成效果不变,GPU、CPU统统能加速...
  3. android使用okthtp
  4. kalman滤波(二)---扩展kalman滤波[EKF]的推导
  5. RabbitMQ通配符模式
  6. 2020年信息系统项目管理师真题讲解:基础知识3/3
  7. ESP32彩屏成为HMI这条GAI最靓的仔--8月27日启明云端携手乐鑫为你共述ESP32时下最IN进阶玩法--以简驭繁,AI语音、彩屏尽显锋芒
  8. c语言浮点乘法 溢出,浮点加减乘除运算各在什么情况下会发生溢出?
  9. 最佳编码hdu_如果–否则为编码风格最佳实践
  10. 输入一个数3256,将他从小到大输出,就是2356
  11. atomikosdatasourcebean mysql_SpringBoot2整合JTA组件实现多数据源事务管理
  12. SharePointChina.com上线 《MOSS 2007 前瞻技术指南》第一章完整版预览申请
  13. aspose.words读取html,Aspose.Words for .NET HTML代码直接生成WORD | 学步园
  14. java事件监听机制 概述
  15. 国内三大知名开源B2B2C多用户商城系统对比
  16. .chm文件是什么怎么打开?
  17. Roaring Bitmap 原理及实践
  18. js 计算两个时间的之间的天数
  19. Vue创建app及App挂载和渲染
  20. aws mysql 费用_AWS 免费一年套餐详解

热门文章

  1. linux 搜狗拼音输入法
  2. CSS齿轮转动加载动画
  3. Mac安装photoshopcs6
  4. 每日一译:上述报盘以我方最后确认为准
  5. 服务器显示蜘蛛,新换服务器后蜘蛛都不来捉取文章链接怎么办?
  6. 计算机科学与技术以为舟,于哲舟-吉林大学计算机科学与技术学院
  7. 2011年1月23日
  8. CAD中用lisp程序实现批量偏移_求一个cad lisp 双向偏移的代码
  9. Tableau 快速表计算 显示百分比 / 累计走势
  10. 区块链行业如何摆脱“圈内自嗨”,真正进入大众市场?