association的使用
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的使用相关推荐
- 如何用ABAP代码读取CDS view association的数据
我有如下一个CDS view, 这个view的数据来自CRMD_ORDERADM_H, 定义了一个名称为_statushelp的association, 指向了另一个CDS view Z_C_Stat ...
- 微生物相关网络构建教程中文Microbial association network construction tutorial
原文为自Microbial association network construction tutorial http://psbweb05.psb.ugent.be/conet/microbial ...
- Association Rules 关联规则
Association Rules 关联规则 除了apriori和FPGrowth目前还有那些方法用来发现关联规则? 关键词: 频繁项集,apriori算法,FPGrowth,关联规则, 频繁项集评估 ...
- 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 ...
- Windows Phone本地数据库(SQLCE):5、[Association]attribute(翻译)(转)
这是"windows phone mango本地数据库(sqlce)"系列短片文章的第五篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆 ...
- mybaits十一:使用association分步查询
DepartmentMapper.xml sql配置文件 <?xml version="1.0" encoding="UTF-8" ?> <! ...
- LINQ那些事儿(2)- 简单对象的CRUD操作和Association的级联操作
从(1)我们看到,当生成entity class定义时,entity class或xml mapping文件中都已经完整的包含了entity和关系数据库的映射信息了,LINQ2SQL会根据这些信息来把 ...
- mybatis association表关联与rowbounds共同使用时的异常及其解决方案
按照mybatis手册中所说的,association有两种实现方式,嵌套查询和嵌套结果映射.如手册中所述,select方式会带来N+1次查询的问题,考虑到效率问题的话建议使用嵌套结果映射.但是在结合 ...
- 论文笔记:Integrating Classification and Association Rule Mining (即,CBA算法介绍)
1998 KDD 0 摘要 分类规则挖掘旨在发现数据库中的一小组规则,形成一个准确的分类器. 关联规则挖掘发现数据库中存在的所有满足最小支持度和最小置信度约束的规则. 对于关联规则挖掘,发现的目标不是 ...
- mybatis元素类型为 “resultMap“ 的内容必须匹配 “(constructor?,id *,result*,association报错解决
1.前言 太久没写这种套娃式的sql语句了,导致今天一写,直接给我整了个报错. 原因其实蛮简单的,mybatis的xml中的resultMap标签规定了内标签的顺序,写错了就会直接解析不出来,从而报错 ...
最新文章
- mysql delete 注意
- 朱俊彦团队提出GAN压缩算法:计算量减少20倍,生成效果不变,GPU、CPU统统能加速...
- android使用okthtp
- kalman滤波(二)---扩展kalman滤波[EKF]的推导
- RabbitMQ通配符模式
- 2020年信息系统项目管理师真题讲解:基础知识3/3
- ESP32彩屏成为HMI这条GAI最靓的仔--8月27日启明云端携手乐鑫为你共述ESP32时下最IN进阶玩法--以简驭繁,AI语音、彩屏尽显锋芒
- c语言浮点乘法 溢出,浮点加减乘除运算各在什么情况下会发生溢出?
- 最佳编码hdu_如果–否则为编码风格最佳实践
- 输入一个数3256,将他从小到大输出,就是2356
- atomikosdatasourcebean mysql_SpringBoot2整合JTA组件实现多数据源事务管理
- SharePointChina.com上线 《MOSS 2007 前瞻技术指南》第一章完整版预览申请
- aspose.words读取html,Aspose.Words for .NET HTML代码直接生成WORD | 学步园
- java事件监听机制 概述
- 国内三大知名开源B2B2C多用户商城系统对比
- .chm文件是什么怎么打开?
- Roaring Bitmap 原理及实践
- js 计算两个时间的之间的天数
- Vue创建app及App挂载和渲染
- aws mysql 费用_AWS 免费一年套餐详解