xml

<mapper namespace="com.oracle.dao.one2oneDao">
<sql id="personColum"> id,name,gender,age</sql>
<sql id="passportColum"> id ,bh,person_id</sql>
<sql id="passportColum2"> id ,bh</sql> <resultMap type="com.oracle.pojo.Passport" id="passportResultMap">
<id column="id" property="id" javaType="int" jdbcType="INTEGER"/>
<result column="bh" property="bh" javaType="string" jdbcType="VARCHAR"/>
<association property="person" column="person_id" javaType="com.oracle.pojo.Person" select="getPerson"></association>
</resultMap> <select id="getPerson" parameterType="int" resultType="com.oracle.pojo.Person">
select <include refid="personColum"></include>
from t_person_fk
where id=#{id}
</select> <select id="getPassport" parameterType="int" resultMap="passportResultMap">
select <include refid="passportColum"></include>
from t_passport_fk
where person_id=#{id}
</select>
<!-- ||||||||||||||||||||||||||||||||||| --> <resultMap type="com.oracle.pojo.Passport" id="passportResult">
<id column="id" property="id" javaType="int" jdbcType="INTEGER"/>
<result column="bh" property="bh" javaType="string" jdbcType="VARCHAR"/>
<association property="person" column="person_id" javaType="com.oracle.pojo.Person" resultMap="personResult"></association>
</resultMap> <resultMap type="com.oracle.pojo.Person" id="personResult">
<id column="id" property="id" javaType="int" jdbcType="INTEGER"/>
<result column="name" property="name" javaType="string" jdbcType="VARCHAR"/>
<result column="gender" property="gender" javaType="string" jdbcType="VARCHAR"/>
<result column="age" property="age" javaType="int" jdbcType="INTEGER"/>
</resultMap> <select id="getPersonAndPassport" parameterType="int" resultMap="passportResult">
select person.id,person.name,person.age,person.gender,
passport.id,passport.bh
from t_passport_fk passport left join
t_person_fk person on passport.person_id = person_id
where person.id=#{id}
</select>
<!-- |||||||||||||主键映射||||||||||||||||||| --> <select id="getPersonPK" parameterType="int" resultType="com.oracle.pojo.Person">
select <include refid="personColum"></include>
from t_person_pk
where id=#{id}
</select> <select id="getPassportPk" parameterType="int" resultMap="passportResultMapPK">
select <include refid="passportColum2"></include>
from t_passport_pk
where id=#{id}
</select> <resultMap type="com.oracle.pojo.Passport" id="passportResultMapPK">
<id column="id" property="id" javaType="int" jdbcType="INTEGER"/>
<result column="bh" property="bh" javaType="string" jdbcType="VARCHAR"/>
<association property="person" column="id" javaType="com.oracle.pojo.Person" select="getPersonPK"></association>
</resultMap>

Dao

public interface one2oneDao {
//////////////////外键映射
public Passport getPassport(int id); public Passport getPersonAndPassport(int id); ///////////////////////主键映射 public Passport getPassportPk(int id); }

Test

    public static void main(String[] args) {
//获取数据源
String resource = "mybatis.xml";
InputStream inputStream=null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); one2oneDao one2onedao = session.getMapper(one2oneDao.class); // Passport passport = one2onedao.getPassport(2);
// System.out.println(passport); // Passport passport = one2onedao.getPersonAndPassport(2);
// System.out.println(passport); // Passport passportPk = one2onedao.getPassportPk(1);
// System.out.println(passportPk); session.commit();
} }

sql

/*基于外键映射*/
CREATE TABLE t_person_fk
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR2(20) NOT NULL,
gender VARCHAR2(10),
age NUMBER(3) CHECK(age BETWEEN 1 AND 100)
); CREATE TABLE t_passport_fk
(
id NUMBER(10) PRIMARY KEY,
bh VARCHAR2(30) NOT NULL UNIQUE,
person_id NUMBER(10) REFERENCES t_person_fk(id) UNIQUE
); CREATE SEQUENCE t_person_fk_seq
START WITH 1
INCREMENT BY 1; CREATE SEQUENCE t_passport_fk_seq
START WITH 1
INCREMENT BY 1; /*基于主键映射*/
CREATE TABLE t_person_pk
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR2(20) NOT NULL,
gender VARCHAR2(10),
age NUMBER(3) CHECK(age BETWEEN 1 AND 100)
); CREATE TABLE t_passport_pk
(
id NUMBER(10) PRIMARY KEY REFERENCES t_person_pk(id),
bh VARCHAR2(30) NOT NULL UNIQUE
); CREATE SEQUENCE t_person_pk_seq
START WITH 1
INCREMENT BY 1;

mybatis 一对一映射的更多相关文章

  1. Mybatis一对一映射

    一.Mybatis一对一映射 本例讲述使用mybatis开发过程中常见的一对一映射查询案例.只抽取关键代码和mapper文件中的关键sql和配置,详细的工程搭建和Mybatis详细的流程代码可参见&l ...

  2. mybatis一对一映射配置详解

    听说mybatis一对一有三种写法,今天我试了一下. 数据库表准备 为了偷懒,我直接就拿用户权限菜单里的菜单表和菜单与权限的中间表做实现,他们原来是多对多的关系,这边我假设这两张表是一对一. 表  g ...

  3. mybatis 一对一 映射实体类、嵌套查询

    一对一 在SysUser 类中增加SysRole字段.1.sql语句将role.role_name映射到role.roleName上. 2.还可以在XML 映射文件中配置结果映射.<result ...

  4. Mybatis一对一映射resultMap子标签中顺序问题

    直接上图 鼠标点上红线出现如下提示 The content of element type "resultMap" must match  "(constructor?, ...

  5. 【Mybatis高级映射】一对一映射、一对多映射、多对多映射

    前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...

  6. mybatis高级映射(一对一,一对多)

    mybatis高级映射 一对一关联映射 需求:查询订单信息,关联查询用户信息(一个订单对应一个用户) (1)通过resultType实现 sql语句: select orders.* , USER.u ...

  7. Mybatis输入输出映射_动态sql_关联关系(一对一、一对多、多对多)

    Mybatis输入输出映射_动态sql_关联关系(一对一.一对多.多对多)输入输出映射parameterType完成输入映射parameterType可以传入的参数有,基本数据类型(根据id查询用户的 ...

  8. MyBatis高级查询 一对一映射

    drop database if exists simple; create database simple; use simple; drop table if exists sys_user; c ...

  9. MyBatis从入门到精通(九):MyBatis高级结果映射之一对一映射

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解MyBatis中实现查 ...

随机推荐

  1. 算法学习记录-图——最小生成树之Kruskal算法

    之前的Prim算法是基于顶点查找的算法,而Kruskal则是从边入手. 通俗的讲:就是希望通过 边的权值大小 来寻找最小生成树.(所有的边称为边集合,最小生成树形成的过程中的顶点集合称为W) 选取边集 ...

  2. Arthas诊断工具使用资料

    1.https://github.com/alibaba/arthas/issues/327 2.https://alibaba.github.io/arthas/jad.html 3.https:/ ...

  3. Python内置函数7

    Python内置函数7 1.propertypython内置的一个装饰器可参考https://blog.csdn.net/u013205877/article/details/77804137 2.q ...

  4. 86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

    在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好.在这里我们简单介绍怎么使用,本节主 ...

  5. apache kafka系列之客户端开发-java

    1.依赖包 <dependency>            <groupId>org.apache.kafka</groupId>            <a ...

  6. BZOJ 2190 [SDOI2008]仪仗队 ——Dirichlet积

    [题目分析] 考虑斜率为0和斜率不存在的两条线上只能看到3人. 其余的人能被看见,当且仅当gcd(x,y)=1 ,然后拿卷积算一算 发现就是欧拉函数的前缀和的二倍. 注意2的情况要特判. [代码] # ...

  7. 【扫描线或树状数组】CSU 1335 高桥和低桥

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1335 [题意] 给定n座桥的高度,给定m次洪水每次的涨水水位ai和退水水位bi 询问有多少座桥 ...

  8. IBM内存三技术:Chipkill、MPX、MM

    转自:解析IBM内存三技术:Chipkill.MPX.MM 内存作为服务器中的又一个重要的组成部分,对于企业的应用起着十分重要的作用.如今,企业用户对于服务器的要求逐渐提升,使得在提高内存密度.增大内 ...

  9. LA 3263 平面划分

    Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...

  10. *LOJ#2322. 「清华集训 2017」Hello world!

    $n \leq 50000$的树,有点权$\leq 1e13$,$q \leq 400000$次操作,有两种操作:从$s$跳到$t$每次$k$步,不到$k$步直接跳到$t$,每次把经过的点取根号:同样 ...