在实现实列中我们在学生表里面增加了一个地址表用于与学生表的一对一
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>