ORM 一对一实验的相关代码

9.2 一对一

建立两张表

image-20210414230334303

设置外键,联系起来

image-20210414230300484

导包

image-20210414230650271

建立两个持久化,两个类

image-20210418093119815

package com.xiucai.po;

public class IdCard {

private Integer id;
private String code;
public Integer getId() {
	return id;
}
public String getCode() {
	return code;
}
public void setId(Integer id) {
	this.id = id;
}

public void setCode(String code) {
	this.code = code;
}

@Override
public String toString() {
	return "IdCard [id=" + id + ", code=" + code + "]";
}

}

package com.xiucai.po;

public class Person {
private Integer id;
private String name;
private Integer age;
private String sex;

public String getSex() {
	return sex;
}

public void setSex(String sex) {
	this.sex = sex;
}

private IdCard card;//注入关系,把两者联系起来了,你要使用里边的东西

public Integer getId() {
	return id;
}

public String getName() {
	return name;
}

public Integer getAge() {
	return age;
}

public IdCard getCard() {
	return card;
}

public void setId(Integer id) {
	this.id = id;
}

public void setName(String name) {
	this.name = name;
}

public void setAge(Integer age) {
	this.age = age;
}

public void setCard(IdCard card) {
	this.card = card;
}

@Override
public String toString() {
	return "Person [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", card=" + card + "]";
}


}

建立两个xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.xiucai.mapper.PersonMapper">
    <!-- 嵌套 -->
    <select id="findPersonById" parameterType="Integer" resultMap="IdCardWithPersonResult">
    select * from tb_person where id=#{id}
    </select>
    <!-- 即将查询的结果映射过来 -->  
    <resultMap type="Person" id="IdCardWithPersonResult">
    
    <id property="id" column="id"/>
    <result property="name" column="name"/>
      <result property="age" column="age"/>
      <result property="sex" column="sex"/>
      <!-- 关于外键我们嵌套需要写的内容 -->
      <association 
      property="card" 
      column="card_id" 
      javaType="IdCard"
      select="com.xiucai.mapper.IdCardMapper.findById"
      />
  
    </resultMap>
    <!-- 嵌套结果: 直接使用结果映射-->
    <select id="findPersonById2" parameterType="Integer" resultMap="IdCardWithPersonResult2">
   <!-- 丰富了查询语句 -->
    select p.*,idcard.code
    from tb_person p,tb_idcard idcard
    where p.card_id=idcard.id
    and p.id=#{id}
    
    </select>
      <resultMap type="Person" id="IdCardWithPersonResult2">
    
    <id property="id" column="id"/>
    <result property="name" column="name"/>
      <result property="age" column="age"/>
      <result property="sex" column="sex"/>
      <!-- 关于外键我们嵌套需要写的内容 -->
      <association 
      property="card" 
      javaType="IdCard">
      <id property="id" column="IdCard"/>
      <result property="code" column="code"/>
 </association>
    </resultMap>
    </mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.xiucai.mapper.IdCardMapper">
    <select id="findById" parameterType="Integer" resultType="IdCard">
    select * from tb_idcard where id=#{id}
    </select>
    
    </mapper>

编写工具类

package com.xiucai.Utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

public  class MybatisUtils {

public static SqlSession getSession() throws IOException {
	  String resource = "mybatis-config.xml";
	    InputStream inputStream = Resources.getResourceAsStream(resource);
	
	    SqlSessionFactory sqlSessionFactory = 
	            new SqlSessionFactoryBuilder().build(inputStream);

	    SqlSession sqlSession = sqlSessionFactory.openSession();
		return sqlSession;
}
	
	
	
}

编写测试类

package com.xiucai.Utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

public  class MybatisUtils {

public static SqlSession getSession() throws IOException {
	  String resource = "mybatis-config.xml";
	    InputStream inputStream = Resources.getResourceAsStream(resource);
	
	    SqlSessionFactory sqlSessionFactory = 
	            new SqlSessionFactoryBuilder().build(inputStream);

	    SqlSession sqlSession = sqlSessionFactory.openSession();
		return sqlSession;
}
	
	
	
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<typeAliases>
<package name="com.xiucai.po"/>
</typeAliases>
    <!--1.配置环境 ,默认的环境id为mysql-->
    <environments default="mysql">
        <!--1.2.配置id为mysql的数据库环境 -->
        <environment id="mysql">
            <!-- 使用JDBC的事务管理 -->
            <transactionManager type="JDBC" />
            <!--数据库连接池 -->
            <dataSource type="POOLED">
			  <property name="driver" value="${jdbc.driver}" />
			  <property name="url"  value="${jdbc.url}" />
			  <property name="username" value="${jdbc.username}" />
			  <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
    </environments>
    <!--2.配置Mapper的位置 -->
    <mappers>
		<mapper resource="com/xiucai/mapper/IdCardMapper.xml" />
		<mapper resource="com/xiucai/mapper/PersonMapper.xml" />
    </mappers>
</configuration>

上边引入config文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秀才大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值