mybatis 一对一映射
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 一对一映射的更多相关文章
- Mybatis一对一映射
一.Mybatis一对一映射 本例讲述使用mybatis开发过程中常见的一对一映射查询案例.只抽取关键代码和mapper文件中的关键sql和配置,详细的工程搭建和Mybatis详细的流程代码可参见&l ...
- mybatis一对一映射配置详解
听说mybatis一对一有三种写法,今天我试了一下. 数据库表准备 为了偷懒,我直接就拿用户权限菜单里的菜单表和菜单与权限的中间表做实现,他们原来是多对多的关系,这边我假设这两张表是一对一. 表 g ...
- mybatis 一对一 映射实体类、嵌套查询
一对一 在SysUser 类中增加SysRole字段.1.sql语句将role.role_name映射到role.roleName上. 2.还可以在XML 映射文件中配置结果映射.<result ...
- Mybatis一对一映射resultMap子标签中顺序问题
直接上图 鼠标点上红线出现如下提示 The content of element type "resultMap" must match "(constructor?, ...
- 【Mybatis高级映射】一对一映射、一对多映射、多对多映射
前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...
- mybatis高级映射(一对一,一对多)
mybatis高级映射 一对一关联映射 需求:查询订单信息,关联查询用户信息(一个订单对应一个用户) (1)通过resultType实现 sql语句: select orders.* , USER.u ...
- Mybatis输入输出映射_动态sql_关联关系(一对一、一对多、多对多)
Mybatis输入输出映射_动态sql_关联关系(一对一.一对多.多对多)输入输出映射parameterType完成输入映射parameterType可以传入的参数有,基本数据类型(根据id查询用户的 ...
- MyBatis高级查询 一对一映射
drop database if exists simple; create database simple; use simple; drop table if exists sys_user; c ...
- MyBatis从入门到精通(九):MyBatis高级结果映射之一对一映射
最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解MyBatis中实现查 ...
随机推荐
- 【SaltStack】SaltStack研究心得
基础篇 ------------------------------------------------------------------------------------------------ ...
- goland 快键键整理及注册
https://my.oschina.net/lemos/blog/1358731 http://idea.lanyus.com/
- VC调试入门
概述调试是一个程序员最基本的技能,其重要性甚至超过学习一门语言.不会调试的程序员就意味着他即使会一门语言,却不能编制出任何好的软件.这里我简要的根据自己的经验列出调试中比较常用的技巧,希望对大家有用. ...
- nginx报错 too many open files in system
系统进不去了,用ssh连接服务器也非常慢,负载均衡显示后端连接异常,重启mysql数据库,发现经常重启,或者直接关机,访问页面也访问不到. http://www.51testing.com/html/ ...
- 11g自动分区超过最大限制
公司业务系统一张表按时间每天分区 写入数据时报错:ORA-14300: 分区关键字映射到超出允许的最大分区数的分区 ORA-14300: partitioning key maps to a part ...
- C/C++ 命令行参数的实现方法
解析从命令行提供的参数可以使用 getopt函数. To use this facility, your program must include the header file unistd.h u ...
- gevent 使用踩坑
简单介绍 gevent 基本概念: 调度器: hub 上下文切换管理: switch 主循环: loop 协程: greenlet gevent 特性: ...
- selenium 与 firefox版本不兼容报错
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 ...
- MysqL5.7在使用mysqldump命令备份数据库报错:mysqldump: [Warning] Using a password on the command line interface can be insecure.
在阿里云服务器增加一个shell脚本定时备份数据库脚本执行任务时,测试性的执行了备份命令,如下 [root@iZ2ze503xw2q1fftv5rhboZ mysql_bak]# /usr/local ...
- 【黑科技】读写优化 orz bdd
转自 bdd :http://www.cnblogs.com/kevince/p/3924688.html 读入优化: inline int read() { char ch; bool flag = ...