现在有很多电商平台,就拿这个来说吧。顾客跟订单的关系,一个顾客可以有多张订单,但是一个订单只能对应一个顾客。

  一对多的顾客

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zdp.domain.Person"> <resultMap type="Person" id="personBean">
<id column="personId" property="id"/>
<result column="personName" property="name"/>
<result column="personAddress" property="address"/>
<result column="personTel" property="tel"/> <!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="orders" ofType="Order">
<id column="orderId" property="id"/>
<result column="orderNumber" property="number"/>
<result column="orderPrice" property="price"/>
</collection>
</resultMap> <!-- 根据id查询Person, 关联将Orders查询出来 -->
<select id="selectPersonById" parameterType="string" resultMap="personBean">
select p.*, o.* from person p, orders o where p.personId = o.pid and p.personId = #{id}
</select> </mapper> 多对一的订单
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zdp.domain.Order">
<resultMap type="Order" id="orderBean">
<id column="orderId" property="id"/>
<result column="orderNumber" property="number"/>
<result column="orderPrice" property="price"/> <!-- 多对一的关系 -->
<!-- property: 指的是属性的值, javaType:指的是属性的类型-->
<association property="person" javaType="Person">
<id column="personId" property="id"/>
<result column="personName" property="name"/>
<result column="personAddress" property="address"/>
<result column="personTel" property="tel"/>
</association>
</resultMap>
<!-- 根据id查询Order, 关联将Person查询出来 -->
<select id="selectOrderById" parameterType="string" resultMap="orderBean">
select p.*, o.* from person p, orders o where p.personId = o.pid and o.orderId = #{id}
</select>
</mapper> 总配置: sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
     //这块儿做了别名的配置所以上面两个配置文件中可以使用Person和Order直接去代表类的全名
<typeAliases>
  <typeAlias type="com.zdp.domain.Person" alias="Person"/>
  <typeAlias type="com.zdp.domain.Order" alias="Order"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 映射文件的位置 -->
<mapper resource="com/zdp/domain/Person.xml" />
<mapper resource="com/zdp/domain/Order.xml" />
</mappers>
</configuration>

Mybatis 中一对多,多对一的配置的更多相关文章

  1. mybatis 中一对多、多对一、多对多、父子继承关系

    mybatis 中处理一对多.多对一.多对多.父子继承关系的有关键词:association .collection .discriminator id – 一个 ID 结果:标记出作为 ID 的结果 ...

  2. SSM-MyBatis-18:Mybatis中二级缓存和第三方Ehcache配置

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 二级缓存 Mybatis中,默认二级缓存是开启的.可以关闭. 一级缓存开启的.可以被卸载吗?不可以的.一级缓存 ...

  3. MyBatis 中 sqlmapconfig核心标签说明以及配置

    文件介绍 对于 MyBatis 最核心的全局配置文件是 sqlmapConfig.xml 文件,其中包含了数据库的连接配置信息.Mapper 映射文件的加载路径.全局参数.类型别名等. 配置项详解 标 ...

  4. 2018.11.4 Hibernate中一对、多对多的关系

    简单总结一下 多表关系 一对多/多对一 O 对象 一的一方使用集合. 多的一方直接引用一的一方. R 关系型数据库 多的一方使用外键引用一的一方主键. M 映射文件 一: 多: 操作: 操作管理级别属 ...

  5. mybatis 一对一 一对多 多对多

    一对一 一对多 多对多

  6. hibernate中一对多多对一关系设计的理解

    1.单向多对一和双向多对一的区别? 只需要从一方获取另一方的数据时 就使用单向关联双方都需要获取对方数据时 就使用双向关系 部门--人员 使用人员时如果只需要获取对应部门信息(user.getdept ...

  7. hibernate中一对多 多对多 inverse cascade

    ----------------------------一对多------------------------------------------- inverse属性:是在维护关联关系的时候起作用的 ...

  8. [原创]PostgreSQL Plus Advince Server在 HA环境中一对多的Stream Replication配置(四)

    八.HA环境下配置多节点的sshVIP(s1):[root@s1 ~]# mkdir /opt/PostgresPlus/9.2AS/.ssh[root@s1 ~]# chown enterprise ...

  9. [原创]PostgreSQL Plus Advince Server在 HA环境中一对多的Stream Replication配置(三)

    五.准备HA环境1.准备yum源a.安装vsftp服务,将光盘镜像copy到本地ftp目录作为yum源.[root@s1 ~]# mount 可以看到cdrom已经挂载了,首先安装vsftp服务[ro ...

随机推荐

  1. java 文件操作 写入和读取(小结一)

    参考了这篇博客并优化,谢谢:http://blog.sina.com.cn/s/blog_99201d890101b4le.html 功能:  实现通过两个类完成先写入文件,再读取数据计算显示 pac ...

  2. PrintWriter用法简析

    public class PrintWriterextends Writer 向文本输出流打印对象的格式化表示形式.此类实现在 PrintStream 中的所有 print 方法.它不包含用于写入原始 ...

  3. 使用fliter实现ie下css中rgba的效果

    直接举例吧background-color: rgba(255,255,255,0.8); 设置背景颜色为白色,不透明度设为80% 在ie下可以用fliter这一属性进行设置: filter:prog ...

  4. js字符串转日期,js字符串解析成日期,js日期解析, Date.parse小时是8点,Date.parse时间多了8小时

    js字符串转日期,js字符串解析成日期,js日期解析, Date.parse小时是8点,Date.parse时间多了8小时 >>>>>>>>>&g ...

  5. Codeforces Round #396.D

    D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...

  6. 配置NFS服务与tftp服务

    在VMware在安装ubuntu的图解 链接:http://pan.baidu.com/s/1jIofvYu 密码:da72 图解里已经解压安装了VMware Tools,接下来必须要安装的就是NFS ...

  7. PHP基础学习(函数一)

    PHP(Hypertext Preprocessor):超文本预处理器,一种嵌入在HTML中并且运行在服务器端的脚本语言. var_dump--打印变量相关信息 说明:  <?php var_d ...

  8. linux下apache,php的安装

    apache的安装 1.下载httpd-2.4.16.tar.gz, apr-1.5.2.tar.gz,apr-util-1.5.4.tar.gz,pcre-8.37.zip,解压 2.注意看apac ...

  9. C++ traits技术浅谈

    前言 traits,又被叫做特性萃取技术,说得简单点就是提取"被传进的对象"对应的返回类型,让同一个接口实现对应的功能.因为STL的算法和容器是分离的,两者通过迭代器链接.算法的实 ...

  10. 阶乘运算——ACM

    大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?   输入 输入一个整数 ...