zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    (4.25)Sqlserver中 登录用户只能看到自己拥有权限的库
    【查阅】mysql配置文件/参数文件重要参数笔录(my.cnf)
    【监控笔记】【2.5】DML(CDC)、DDL(DDL触发器)跟踪数据更改,数据库审计
    SQL Server 2008中的CDC(Change Data Capture)功能使用及释疑
    【监控笔记】【2.4】SQL Server中的 Ring Buffer 诊断各种系统资源压力情况
    【监控笔记】【2.3】扩展事件——慢查询SQL(执行超过3S的SQL)
    【监控笔记】【2.2】扩展事件——死锁监控
    最小配置启动SQL SERVER,更改SQL Server最大内存大小导致不能启动的解决方法
    【监控笔记】【2.1】扩展事件
    【扩展事件】跟踪超过3秒的SQL
  • 原文地址:https://www.cnblogs.com/Damon-Luo/p/5662831.html
Copyright © 2011-2022 走看看