内容简介:实际的开发中,对数据库的操作常常会涉及到多张表,这在面向对象中就涉及到了对象与对象之间的关联关系。针对多表之间的操作,MyBatis提供了关联映射,通过关联映射就可以很好的处理对象与对象之间的关联关系。
实际的开发中,对数据库的操作常常会涉及到多张表,这在面向对象中就涉及到了对象与对象之间的关联关系。
针对多表之间的操作,MyBatis提供了关联映射,通过关联映射就可以很好的处理对象与对象之间的关联关系。
目录
你需要了解的知识点
1、关联关系种类
数据库:
在关系型数据库中,多表之间存在着三种关联关系,分别为 一对一 、 一对多 和 多对多
- 一对一:在任意一方引入对方主键作为外键;
- 一对多:在“多”的一方,添加“一”的一方的主键作为外键;
- 多对多:产生中间关系表,引入两张表的主键作为外键,两个主键成为联合主键或使用新的字段作为主键。
java
-
一对一:在本类中定义对方类型的对象,如A类中定义B类类型的属性b,B类中定义A类类型的属性a;(双向一对一)
-
一对多:一个A类类型对应多个B类类型的情况,需要在A类中以集合的方式引入B类类型的对象,在B类中定义A类类型的属性a;
-
多对多:在A类中定义B类类型的集合,在B类中定义A类类型的集合。
2、关联查询方式
MyBatis加载关联关系对象主要通过两种方式: 嵌套查询 和 嵌套结果 。
使用
1、创建实体类
创建实体类:在 com.lomtom.mybaris.entity 包中创建实体类 AdminDetail.java 和 AdminInfo.java
1、AdminDetail.java
/**
* @Author: LOMTOM
* @Date: 2020/4/26
* @Time: 16:43
* @Email: lomtom@qq.com
*/
@Data
public class AdminInfo {
private int id;
private String name;
private String pwd;
private AdminDetail ad;
public AdminInfo() {
}
public AdminInfo(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
}
2、AdminInfo.java
/**
* @Author: LOMTOM
* @Date: 2020/4/26
* @Time: 16:42
* @Email: lomtom@qq.com
*/
@Data
public class AdminDetail {
private int id;
private String address;
private String realName;
public AdminDetail() {
}
public AdminDetail(String address, String realName) {
this.address = address;
this.realName = realName;
}
}
2、创建 SQL 映射的XML文件
在 com.lomtom.mybaris.mapper 包中创建SQL映射的XML文件 adminDetailMapper.xml 及 adminInfoMapper.xml ,
<insert> <select>
1、adminDetailMapper.xml
<mapper namespace="com.lomtom.mybatis.mapper.adminDetailMapper">
<insert id="addAdminDetail" parameterType="AdminDetail"
keyProperty="id" useGeneratedKeys="true">
insert into admin_detail(address ,realName) values (#{address},#{realName})
</insert>
</mapper>
2、adminInfoMapper.xml
<mapper namespace="com.lomtom.mybatis.mapper.adminInfoMapper">
<insert id="addAdminInfo" parameterType="AdminInfo">
insert into admin_info(id, name, pwd) values( #{ad.id}, #{name}, #{pwd})
</insert>
<select id="getAdminInfo" parameterType="int" resultMap="getAdminInfoMap">
select ai.id, name, pwd, address, realName from admin_info ai, admin_detail ad where ai.id=ad.id and ai.id=#{id}
</select>
<!--嵌套结果映射,最后返回的结果,见 select元素的 resultMap属性-->
<resultMap type="com.lomtom.mybatis.entity.AdminInfo" id="getAdminInfoMap">
<id property="id" column="id" />
<result property="name" column="name"/>
<result property="pwd" column="pwd"/>
<association property="ad" javaType="com.lomtom.mybatis.entity.AdminDetail">
<id property="id" column="id"/>
<result property="address" column="address"/>
<result property="realName" column="realName"/>
</association>
</resultMap>
<select id="getAdminInfo2" parameterType="int"
resultMap="getAdminInfo2Map">
select * from admin_info where id=#{id}
</select>
<select id="getAdminDetail" parameterType="int" resultType="AdminDetail">
select * from admin_detail where id=#{id}
</select>
<resultMap type="AdminInfo" id="getAdminInfo2Map">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="pwd" column="pwd"/>
<association property="ad" column="id" select="getAdminDetail"/>
</resultMap>
</mapper>
其中,插入 keyProperty="id" useGeneratedKeys="true" 实现自增,以上有两种查询方式分别为嵌套查询与嵌套结果
3、注册SQL映射的XML文件
在XML配置文件 mybatis-config.xml 中注册 adminDetailMapper.xml 和 adminInfoMapper.xml。
<mappers>
<mapper resource="com/lomtom/mybatis/mapper/adminDetailMapper.xml"/>
<mapper resource="com/lomtom/mybatis/mapper/adminInfoMapper.xml"/>
</mappers>
4、创建表格
创建两个表admin_detail和admin_info,注意两个表之间的依赖关系,可以先创建好两个表,在关联两个表
1、admin_detail SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for admin_detail -- ---------------------------- DROP TABLE IF EXISTS `admin_detail`; CREATE TABLE `admin_detail` ( `id` int(0) NOT NULL AUTO_INCREMENT, `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, `realName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1; 2、admin_info SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for admin_info -- ---------------------------- DROP TABLE IF EXISTS `admin_info`; CREATE TABLE `admin_info` ( `id` int(0) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, `pwd` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, CONSTRAINT `admin_info_ibfk_1` FOREIGN KEY (`id`) REFERENCES `admin_detail` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
5、测试一对一关联映射
在测试类里加入以下,用于测试我们刚刚写的插入与查询
@Test
public void testAddAdminInfo() {
SqlSession sqlSession= MybatisUtils.getSession();
//创建 AdminDetail对象
AdminDetail adminDetail = new AdminDetail("湖南长沙", "杨二蛋子");
//创建 AdminInfo对象
AdminInfo adminInfo = new AdminInfo("杨二蛋子", "123456");
//向数据表 admin_detail插入记录
sqlSession.insert("addAdminDetail", adminDetail);
//设置 AdminInfo对象关联的 AdminDetail对象
adminInfo.setAd(adminDetail);
//向数据表 admin_info插入记录
sqlSession.insert("addAdminInfo", adminInfo);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testGetAdminInfoById(){
SqlSession sqlSession= MybatisUtils.getSession();
AdminInfo adminInfo = sqlSession.selectOne("getAdminInfo",4);
System.out.println(adminInfo);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testGetAdminInfoById2(){
SqlSession sqlSession= MybatisUtils.getSession();
AdminInfo adminInfo = sqlSession.selectOne("getAdminInfo2",4);
System.out.println(adminInfo);
sqlSession.commit();
sqlSession.close();
}
你可能会出现的问题
问题一:提示缺少构造函数
描述:
Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in com.lomtom.mybatis.entity.AdminInfo matching [java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.String]
分析:我们明明使用了lombok,为啥还会提示缺少构造函数,那是因为我们这里使用复杂的bean,lombok自己识别不出来
解决:为 AdminDetail.java 和 AdminInfo.java 创造空的构造函数
写在最后
关注公众号: 博奥思园 ,精彩内容不错失
你的支持是作者最大的动力
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Hibernate 关系映射整理
- Hibernate 关系映射整理
- hibernate教程--关联关系的映射详解
- hibernate教程--关联关系的映射详解
- MyBatis学习笔记(2)—映射关系篇
- Entity Framework 一对多关系映射
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Geometric Folding Algorithms
Erik D Demaine / Cambridge University Press / 2008-8-21 / GBP 35.99
Did you know that any straight-line drawing on paper can be folded so that the complete drawing can be cut out with one straight scissors cut? That there is a planar linkage that can trace out any alg......一起来看看 《Geometric Folding Algorithms》 这本书的介绍吧!