博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis-关联关系
阅读量:4352 次
发布时间:2019-06-07

本文共 1986 字,大约阅读时间需要 6 分钟。

在实现实列中我们在学生表里面增加了一个地址表用于与学生表的一对一

1.创建地址实体类:

package com.java1234.mappers;

import com.java1234.model.Address;

public interface AddressMapper {

public Address findById(Integer id);

}

2.创建地址实体类关联的映射文件

<?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.java1234.mappers.AddressMapper">

<resultMap type="Address" id="AddressResult">

<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</resultMap>
<select id="findById" parameterType="Integer" resultType="Address">
select * from t_address where id=#{id}
</select>

</mapper>

3.在学生表的映射文件里面关联地址

<?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.java1234.mappers.StudentMapper">

<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="id" select="com.java1234.mappers.AddressMapper.findById"></association>
</resultMap>
<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>

<insert id="add" parameterType="Student" >

insert into t_student values(null,#{name},#{age})
</insert>

<update id="update" parameterType="Student">

update t_student set name=#{name},age=#{age} where id=#{id}
</update>
<delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete>
<select id="findById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>
<select id="find" resultMap="StudentResult">
select * from t_student
</select>
</mapper>

 

转载于:https://www.cnblogs.com/csy666/p/6534723.html

你可能感兴趣的文章
C#调用金数据API
查看>>
Convert Sorted List to Binary Search Tree
查看>>
Leetcode:Unique Binary Search Trees
查看>>
D3.js 绘制散点图
查看>>
《图解HTTP》
查看>>
python之路_面向对象
查看>>
CSS
查看>>
jvm架构以及Tomcat优化
查看>>
数据库の目录
查看>>
vmware安装rhel 7
查看>>
[复合材料] 编织复合材料单胞周期性边界条件编程问题
查看>>
Paxos协议笔记
查看>>
php之快速入门学习-15(php函数)
查看>>
【01背包问题】
查看>>
我所知道的数据库8-DML语言
查看>>
Python学习笔记——面向对象基础
查看>>
SQL Server 2012安装时如何不安装自带的Visual Studio
查看>>
网络传输协议总结(转载)
查看>>
C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 角色权限的配置页面改进优化...
查看>>
如何编写Spring-Boot自动配置
查看>>