【Mybatis】一对一实例
①创建数据库和表,数据库为mytest,表为father和child
DROP TABLE IF EXISTS child;
DROP TABLE IF EXISTS father; CREATE TABLE child(
child_id INT PRIMARY KEY auto_increment NOT NULL,
child_name VARCHAR(20)
); CREATE TABLE father(
father_id INT PRIMARY KEY auto_increment NOT NULL,
father_name VARCHAR(20),
child_id INT
); ALTER TABLE father ADD CONSTRAINT fk_child_id FOREIGN KEY (child_id) REFERENCES child(child_id); INSERT INTO child(child_name) VALUES ("Guo Xiang");
INSERT INTO child(child_name) VALUES ("Zhang Wuji");
INSERT INTO father(father_name, child_id) VALUES ("Guo Jing", 1);
INSERT INTO father(father_name, child_id) VALUES ("Zhang Cuishan", 2);
father&child
②创建Java工程,导入相应的jar包

③创建配置文件conf.xml和数据库配置文件db.properties
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${name}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="org/mybatis/mapping/fatherMapper.xml"/>
</mappers>
</configuration>
conf.xml
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mytest
name = root
password = root
db.properties
④创建实体类Father和Child
package org.mybatis.domain;
public class Child {
private int child_id;
private String child_name;
public int getChild_id() {
return child_id;
}
public void setChild_id(int child_id) {
this.child_id = child_id;
}
public String getChild_name() {
return child_name;
}
public void setChild_name(String child_name) {
this.child_name = child_name;
}
@Override
public String toString() {
return "[Child = child_id:" + child_id + ", child_name:" + child_name + "]";
}
}
Child
package org.mybatis.domain;
public class Father {
private int father_id;
private String father_name;
private Child child;
public int getFather_id() {
return father_id;
}
public void setFather_id(int father_id) {
this.father_id = father_id;
}
public String getFather_name() {
return father_name;
}
public void setFather_name(String father_name) {
this.father_name = father_name;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
@Override
public String toString() {
return "[Father = father_id:" + father_id + ", father_name:" + father_name
+ ", child:" + child + "]";
}
}
Father
⑤创建sql映射文件fatherMapper.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="org.mybatis.mapping.fatherMapper"> <!-- 方式一:嵌套结果 -->
<!-- <select id="getChild" parameterType="int" resultMap="ChildMap"> -->
<!-- select * from child c, father f where c.child_id = f.child_id and f.father_id=#{id} -->
<!-- </select> --> <!-- <resultMap type="org.mybatis.domain.Father" id="ChildMap"> -->
<!-- <id property="father_id" column="father_id"/> -->
<!-- <result property="father_name" column="father_name" /> -->
<!-- <association property="child" javaType="org.mybatis.domain.Child"> -->
<!-- <id property="child_id" column="child_id"/> -->
<!-- <result property="child_name" column="child_name"/> -->
<!-- </association> -->
<!-- </resultMap> --> <!-- 方式二:嵌套查询 -->
<select id="getChild" parameterType="int" resultMap="ChildMap">
select * from father where father_id=#{id}
</select> <resultMap type="org.mybatis.domain.Father" id="ChildMap">
<id property="father_id" column="father_id"/>
<result property="father_name" column="father_name" />
<collection property="child" column="child_id" select="getChildCollection">
</collection>
</resultMap> <select id="getChildCollection" resultType="org.mybatis.domain.Child">
select * from child where child_id=#{child_id}
</select> </mapper>
fatherMapper.xml
⑥向配置文件中注册 fatherMapper.xml 文件【已添加,查看第③步】
⑦测试类
package org.mybatis.app; import java.io.InputStream; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.mybatis.domain.Father; public class TestOneToOne { SqlSession session; @Before
public void beforeLoad() {
InputStream inputStream =
TestOneToOne.class.getClassLoader().getResourceAsStream("conf.xml");
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
} @Test
public void testOneToOne() {
// 根据fatherID得到child
String statement = "org.mybatis.mapping.fatherMapper.getChild";
Father father = session.selectOne(statement,2);
session.close();
System.out.println(father.getFather_name() + "'s child is "
+ father.getChild().getChild_name());
}
}
TestOneToOne
⑧结构图

fatherMapper.xml中有两种方式,两种方式的结果一样
【Mybatis】一对一实例的更多相关文章
- mybatis一对一映射配置详解
听说mybatis一对一有三种写法,今天我试了一下. 数据库表准备 为了偷懒,我直接就拿用户权限菜单里的菜单表和菜单与权限的中间表做实现,他们原来是多对多的关系,这边我假设这两张表是一对一. 表 g ...
- mybatis 一对一关联 association 返回空值
mybatis 一对一关联 association 返回空值 最近学习spring mvc + mybatis开发,看的书是<Spring MVC+Mybatis开发 从入门到精通>,在学 ...
- Java基础-SSM之mybatis一对一关联
Java基础-SSM之mybatis一对一关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表) 1>.创建husbands和wifes表并建 ...
- Java基础-SSM之mybatis一对一外键关联
Java基础-SSM之mybatis一对一外键关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表) 1>.创建husbandsfk和wife ...
- MyBatis一对一查询
---------------------siwuxie095 MyBatis 一对一查询 以订单和用户为例,即 相对订 ...
- Mybatis一对一映射
一.Mybatis一对一映射 本例讲述使用mybatis开发过程中常见的一对一映射查询案例.只抽取关键代码和mapper文件中的关键sql和配置,详细的工程搭建和Mybatis详细的流程代码可参见&l ...
- spring+mybatis结合实例
1.通过上两篇文章的学习,大致了解了spring和mybatis的架构和使用原理,下面这篇文章就将给出两者结合的一个小实例,通过该实例进一步探索这两个框架的魅力,工程所需要的所有jar包都在链接:ht ...
- mybatis一对一关联关系映射
mybatis一对一关联关系映射 在关联关系中,有一对一,一对多,多对多三种关联关系. 一对一关系:在操作上,任意一方引入对方的主键作为外键. 一对多关系:在"多"的一方添加&qu ...
- mybatis 一对一关联映射实例
在实际项目开发中,经常存在一对一的关系,如一个人对应一张身份证信息,这就是一对一的关系.下面是一个简单的实例: 1.建表过程我就省略了,主要是一张Person表,一张IDCard表,其相关属性见步骤2 ...
随机推荐
- 20155326刘美岑 《网络对抗》Exp2 后门原理与实践
20155326刘美岑 <网络对抗>Exp2 后门原理与实践 实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划启 ...
- PHP查看内存使用
第一想法:memory_get_usage() 用microtime函数就可以分析程序执行时间memory_get_usage可以分析内存占用空间 SQL的效率可以使用打开慢查询查看日志分析SQL 找 ...
- SQL SERVER占用CPU过高优化
操作系统是Windows2008R2 ,数据库是SQL2014 64位. 近阶段服务器出现过几次死机,管理员反馈机器内存使用率100%导致机器卡死.于是做了个监测服务器的软件实时记录CPU数据,几日观 ...
- 进程控制(Note for apue and csapp)
1. Introduction We now turn to the process control provided by the UNIX System. This includes the cr ...
- 浅谈ESB中的DataRow、DataSet、DataBag 、DataBox
1 背景概述 笔者在学习公司产品AEAI ESB 的时候经常需要从数据库获取信息并将数据信息保存到一个结果变量中,为统计分析提供特定格式的数据以及跨数据库同步数据时通常会用到DataRow.DataS ...
- Android网络请求与数据解析,使用Gson和GsonFormat解析复杂Json数据
版权声明:未经博主允许不得转载 一:简介 [达叔有道]软件技术人员,时代作者,从 Android 到全栈之路,我相信你也可以!阅读他的文章,会上瘾!You and me, we are family ...
- jQuery应用实例2:表格隔行换色
这里是用JS实现的:http://www.cnblogs.com/xuyiqing/p/8376312.html 接下来利用上一篇提到的选择器利用jQuery实现: 发现原来多行代码这里只需要两行: ...
- Python+Excel+Unittest+HTMLTestRunner实现数据驱动接口自动化测试(二)
因为小白,这2天研究了好久才算是搞好.先附上一个测试完成后邮件的截图: 上一篇有提到: unittest中实际运行了一个接口的很多条用例,而报告中只会有一条记录.这是因为unittest test c ...
- BonnMotion支持的几种移动模型
BonnMotion是一款基于java的移动场景产生和分析工具,常用来研究mobile ad hoc network的特征.其产生的移动场景可以导入到几款网络模拟器中进行模拟分析,例如:NS2,NS3 ...
- UFLDL 教程学习笔记(二)反向传导算法
UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...