mybatis 一对多,多对一配置
一. 简介:
本实例使用顾客和订单的例子做说明: 一个顾客可以有多个订单, 一个订单只对应一个顾客
二. 例子:
1. 代码结构图:
2. 建表语句:
- CREATE DATABASE test;
- USE test;
- CREATE TABLE person(
- personId VARCHAR(36) PRIMARY KEY,
- personName VARCHAR(64),
- personAddress VARCHAR(128),
- personTel VARCHAR(11)
- );
- CREATE TABLE orders(
- orderId VARCHAR(36) PRIMARY KEY,
- orderNumber VARCHAR(20),
- orderPrice INT,
- pid VARCHAR(36)
- );
- INSERT INTO person VALUES('001', 'Jack', 'Wuhan', '1234567');
- INSERT INTO orders VALUES('O_00001', '00001', 100, '001');
- INSERT INTO orders VALUES('O_00002', '00002', 200, '001');
- SELECT p.*, o.*
- FROM person p
- JOIN orders o ON (p.personId=o.pid)
- WHERE p.personId = '001'
3. 顾客实体:
- /**
- * 客户实体
- */
- public class Person {
- private String id;
- private String name;
- private String address;
- private String tel;
- private List<Order> orders;
- @Override
- public String toString() {
- return "{id: " + id + ", name: " + name + ", address: " + address + ", tel: " + tel + "}";
- }
- }
4. 订单实体:
- /**
- * 订单实体
- */
- public class Order {
- private String id;
- private String number;
- private int price;
- private Person person;
- @Override
- public String toString() {
- return "{id: " + id + ", number: " + number + ", price: " + price + "}";
- }
- }
5. 一对多实体配置: Person.xml
- <?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>
6. 多对一实体配置:
- <?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>
7. 总配置: 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>
- <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>
8. 测试文件:
- /**
- * 测试一对多和多对一
- */
- public class MybatisTest {
- private SqlSessionFactory ssf;
- @Before
- public void initSF() throws Exception {
- String resource = "sqlMapConfig.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- ssf = new SqlSessionFactoryBuilder().build(inputStream);
- }
- @Test//一对多关联查询
- public void selectPersonById()throws Exception{
- SqlSession session = ssf.openSession();
- Person person = session.selectOne("com.zdp.domain.Person.selectPersonById", "001");
- System.out.println(person.getOrders());
- }
- @Test//多对一关联查询
- public void selectOrderById()throws Exception{
- SqlSession session = ssf.openSession();
- Order order = session.selectOne("com.zdp.domain.Order.selectOrderById", "O_00001");
- System.out.println(order.getPerson().getName());
- }
- }
转自:http://blog.csdn.net/jkxiaoxing/article/details/52199386
mybatis 一对多,多对一配置的更多相关文章
- mybatis的执行流程 #{}和${} Mysql自增主键返回 resultMap 一对多 多对一配置
n Mybatis配置 全局配置文件SqlMapConfig.xml,配置了Mybatis的运行环境等信息. Mapper.xml文件即Sql映射文件,文件中配置了操作数据库的Sql语句.此文件需要在 ...
- mybatis一对多 多对一 多对多
https://blog.csdn.net/AdminGuan/article/details/98952484 Mybatis的Mapper该如何编写多对一? 很简单,就是在resultMap标 ...
- EntityFrameworkCore 一对一 && 一对多 && 多对多配置
基本数据结构 表设计如下: 入学记录 public class AdmissionRecord { [Key] public long Id { get; set; } public DateTime ...
- Mybatis 一对多 简单映射配置
只需在一对多的 “一” Model中定义一个list集合: public class SelectQuestion{ // 主键ID private Integer id; private Strin ...
- MyBatis 一对多,多对一关联查询的时候Mapper的顺序
要先写association,然后写collection:这是由DTD决定的: <resultMap ...> <association ...> </associati ...
- Mybatis一对多/多对多查询时只查出了一条数据
问题描述: 如果三表(包括了关系表)级联查询,主表和明细表的主键都是id的话,明细表的多条数据只能查询出来第一条/最后一条数据. 三个表,权限表(Permission),权限组表(Permission ...
- Mybatis 一对多 多对1
http://blog.csdn.net/z69183787/article/details/46833565 http://blog.csdn.net/rain097790/article/deta ...
- Mybatis一对多或多对多只能查出一条数据解决策略
原文:https://blog.csdn.net/ren814/article/details/81742242 <resultMap id="menuModelMap" t ...
- MyBatis一对多和多对多xml配置
MyBatis一对多和多对多xml配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ma ...
- JAVA日记之mybatis-3一对一,一对多,多对多xml与注解配置
1.Mybatis多表查询1.1 一对一查询1.1.1 一对一查询的模型用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一查询的需求:查询一个订单,与此同时查询出该订单所属的 ...
随机推荐
- 073_使用 shell 脚本打印如下图形
#!/bin/bash #打印第一组图片#for(())为类 C 语言的语法格式,也可以使用 for i in;do;done 的格式替换#for((i=1;i<=9;i++))循环会执行 9 ...
- 062_判断用户输入的是 Yes 或 NO
#!/bin/bashread -p "Are you sure?[y/n]:" surecase $sure iny|Y|Yes|YES) echo "you ...
- 十九.部署LNMP环境、构建LNMP平台、地址重写
proxy client web1 web2 1.部署LNMP环境 1.1 部署nginx(前面已部署过) 1.2 部署mariadb ]# yum -y install mariadb mari ...
- linux 安装Apache服务器
这篇文章先别看,,有些地方我不是很明白,写错了一些地方,正在咨询会linux的大神 安装好Apache就可以用Http访问或者下载电脑的文件了 我还是用 连接我的linux电脑 咱把Apache安装到 ...
- Java 中的 SimpleDateFormat 【 parse 和 format 】【转换时间格式】
在 Java 里面有很多特别方便的函数(尽管术语可能不这么说)可以供我们使用,让一些本来要写好长好多的代码的事情变得仅仅几行就解决了. 在 SimpleDateFormat 中,有以下特定的规则: G ...
- Java监听器listener的介绍
Java监听器listener的介绍 listener 能做什么 当web中某些动作发生之后,服务器就调用listener中对应的方法. 内部机制 接口回调 Web监听器 步骤 创建需要的监听器类,实 ...
- manjaro xfce4 使用super+D快捷键显示桌面(以及使用super+方向键调整窗口)设置无效
xfce4 有两个地方设置快捷键:Keyboard -> application shortcuts 和 window manager -> keyboard. window manage ...
- scala 递归读取文件夹下所有的指定后缀的文件
def getFile(file:File): Array[File] ={ val files = file.listFiles().filter(! _.isDirectory) .filter( ...
- MyBatis 与 Hibernate
MyBatis 是一个优秀的基于 Java 的持久层框架,它内部封装了 JDBC,使开发者只需关注 SQL 语句本身,而不用再花费精力去处理诸如注册驱动.创建 Connection.配置 Statem ...
- Alpha项目冲刺! Day4-产出
各个成员今日完成的任务 林恩:任务分工,博客撰写,了解安卓环境搭建 杨长元:安卓本地数据库基本建立 李震:学习 胡彤:完善服务端 寇永明:学习 王浩:学习 李杰:学习 各个成员遇到的问题 林恩:为自己 ...