mybatis-关联关系
在实现实列中我们在学生表里面增加了一个地址表用于与学生表的一对一
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>
最新文章
- PHP如何获取二个日期的相差天数?
- bug--service--Caused by java.lang.SecurityException: Unable to start service Intent { }:user 0 is restricted
- mysql闪退或者can not connect 127.0.0.1
- linux更改文件所有者命令chown命令的使用困惑
- Struts2应用的开发流程
- 通用权限底层研究:Web应用限制IP访问的功能实现
- Jeally Bean中MonekyRunner 帮助文件
- 事务Isolation Level 例子详解
- Structs2中Action返回json到前台方法
- hdu1042(大数模板)
- NSA永恒之蓝病毒,如何通过360工具修复?
- jquery实现导航栏效果
- JavaScript 第一课
- 第二次项目冲刺(Beta阶段)--第四天
- canvas将图片转为base64
- es6变量声明和解构赋值
- 大型网站架构演进(6)使用NoSQL和搜索引擎
- kubernetes之ingress及ingress controller
- Django之BBS博客项目
- 解决git中upstream丢失问题Your branch is based on &#39;origin/xxxx&#39;, but the upstream is gone.