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. Hive 将本地数据导入hive表中

    # 导入 load data local inpath '/root/mr/The_Man_of_Property.txt' insert into table article; # 提示 FAILE ...

  2. SQL Server数据库和MySQL数据库有什么区别?

    SQL Server数据库和MySQL数据库有什么区别呢?详细很多初入IT行业的朋友对于SQL Server数据库和MySQL数据库经常搞混,认为这两种数据库是同一种,其实不然,今天我们来分析一下这两 ...

  3. Python基础数据类型之集合

    Python基础数据类型之集合 集合(set)是Python基本数据类型之一,它具有天生的去重能力,即集合中的元素不能重复.集合也是无序的,且集合中的元素必须是不可变类型. 一.如何创建一个集合 #1 ...

  4. A. Test for Job

    A. Test for Job Time Limit: 5000ms Case Time Limit: 5000ms Memory Limit: 65536KB   64-bit integer IO ...

  5. Clickomania(区间DP)

    描述 Clickomania is a puzzle in which one starts with a rectangular grid of cells of different colours ...

  6. C语言的那些秘密之---函数返回局部变量[转]

    来源:http://blog.csdn.net/haiwil/article/details/6691854/ 一般的来说,函数是可以返回局部变量的. 局部变量的作用域只在函数内部,在函数返回后,局部 ...

  7. Axure:从单一评价方式到用户自由选择

    导读: 亲,还记得淘宝对货物的评价方式吗?还记得对快递哥的评价方式吗? 1,经典五星评:                                                         ...

  8. C/C++ 位操作

    C/C++对位操作有如下方法: <1>位操作运算符(注意:下面几个运算符不改变原来的变量的值,只是获得运算的结果即一个新值) 按位取反:~ 位与:& 位或:| 位异或:^ 左移位运 ...

  9. Terracotta

    Terracotta 3.2.1简介 (一) 博客分类: 企业应用面临的问题 Java&Socket 开源组件的应用 hibernatejava集群服务器EhcacheQuartzTerrac ...

  10. BZOJ 4033 [HAOI2015]树上染色 ——树形DP

    可以去UOJ看出题人的题解. 这样的合并,每一个点对只在lca处被考虑到,复杂度$O(n^2)$ #include <map> #include <ctime> #includ ...