【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 ...
随机推荐
- IIS 设置文件传输大小限制
IIS默认传输文件大小为30M,最大允许传输为2G. 1.通过webconfig配置节点设置 在IIS 6.0 设置如下配置节点: 但是IIS 7.0-8.0还要做添加如下配置节点才能正确,否则还是默 ...
- 使用Python对Twitter进行数据挖掘(Mining Twitter Data with Python)
目录 1.Collecting data 1.1 Register Your App 1.2 Accessing the Data 1.3 Streaming 2.Text Pre-processin ...
- 五、MongoDB的索引
一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 四.MongoDB的查询 五.MongoDB的索引 1.简介 它就像是一本书的目录,如果没 ...
- Linux Ubuntu部署web环境及项目tomcat+jdk+mysql
1,下载文件 在官网下载好 tomcat.jdk.mysql的linux压缩包 后缀名为.tar.gz 并通过xftp上传到服务器 或者直接通过linux命令 下在wget文件的下载地址 例如: wg ...
- 干货—MySQL常见的面试题+索引原理分析!
目录 MySQL索引的本质 MySQL索引的底层原理 MySQL索引的实战经验 面试 问:数据库中最常见的慢查询优化方式是什么? 同学A:加索引. 问:为什么加索引能优化慢查询? 同学A:...不知道 ...
- Dispatch Queue 之 Invoke 当前队列

- OC学习3——C语言特性之指针
1.指针是C语言中的一个非常重要的概念,实际上,OC系统类的变量.自定义类的变量等都是指针.定义指针变量的语法格式如下,其中*代表一个指针变量,整个语法代表定义一个指向特定类型的变量的指针变量.注意: ...
- tomcat容器是如何创建servlet类实例?用到了什么原理?
当容器启动时,会读取在webapps目录下所有的web应用中的web.xml文件,然后对 xml文件进行解析,并读取servlet注册信息.然后,将每个应用中注册的servlet类都进行加载,并通过 ...
- 超级详细使用Webpack4.X 搭建H5开发环境
超级详细使用Webpack4.X 搭建H5开发环境 会撸码的小马 关注 2018.05.29 17:17* 字数 603 阅读 6453评论 0喜欢 5 很久没弄博客了,这两天有点时间来搞一下最近在弄 ...
- 为什么(2.55).toFixed(1)等于2.5?
上次遇到了一个奇怪的问题:JS的(2.55).toFixed(1)输出是2.5,而不是四舍五入的2.6,这是为什么呢? 进一步观察: 发现,并不是所有的都不正常,1.55的四舍五入还是对的,为什么2. ...