• 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;
  • 相关阅读:
    java的泛型
    JAVA集合类--(一闪而过)
    java面向对象- -一闪而过
    进制转换之二进制与十进制转换
    跳跃表-原理及Java实现
    Excel二次开发相关代码
    程序员学炒股(7) 股市心得
    程序员学炒股(7) 股指期货收盘价对第二天开盘价有影响吗?
    程序员学炒股(6) 让我们来看一下7月份A股的表现
    程序员学炒股(5) 股指交割日效应是否存在?
  • 原文地址:https://www.cnblogs.com/Damon-Luo/p/5662831.html
Copyright © 2020-2023  润新知