一直以来对这块都比较模糊,每次用都是自己百度过来,决定还是自己试一下,记录下来。
数据表:
student表:
class_info表:
一、先写个一对一的
Student.java模型
public class Student {
private Integer id;
private String name;
private Integer age;
private ClassInfo classInfo;
public ClassInfo getClassInfo() {
return classInfo;
}
public void setClassInfo(ClassInfo classInfo) {
this.classInfo = classInfo;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", classInfo=" + classInfo + "]";
}
}
ClassInfo模型:
public class ClassInfo {
private Integer id;
private String classDesc;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getClassDesc() {
return classDesc;
}
public void setClassDesc(String classDesc) {
this.classDesc = classDesc == null ? null : classDesc.trim();
}
@Override
public String toString() {
return "ClassInfo [id=" + id + ", classDesc=" + classDesc + "]";
}
}
StudentMapper.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="com.sadoshi.SpringMyBatis.Mapper.StudentMapper">
<!-- 方法一 -->
<resultMap id="BaseResultMap1" type="com.sadoshi.SpringMyBatis.model.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
<association property="classInfo" column="class_id"
javaType="com.sadoshi.SpringMyBatis.model.ClassInfo">
<id property="id" column="class_info_id" />
<result property="classDesc" column="class_desc" />
</association>
</resultMap>
<!-- 方法二 -->
<resultMap id="BaseResultMap2" type="com.sadoshi.SpringMyBatis.model.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="id" jdbcType="INTEGER" property="classInfo.id" />
<result column="class_desc" jdbcType="VARCHAR" property="classInfo.classDesc" />
</resultMap>
<!-- 方法三 -->
<resultMap id="classinfoResult" type="com.sadoshi.SpringMyBatis.model.ClassInfo">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="class_desc" jdbcType="VARCHAR" property="classDesc" />
</resultMap>
<resultMap id="BaseResultMap3" type="com.sadoshi.SpringMyBatis.model.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
<association property="classInfo" resultMap="classinfoResult" />
</resultMap>
<!-- 方法四 -->
<resultMap id="BaseResultMap4" type="com.sadoshi.SpringMyBatis.model.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
<association property="classInfo" column="class_id"
select="com.sadoshi.SpringMyBatis.Mapper.ClassInfoMapper.selectByPrimaryKey" />
</resultMap>
<!-- 方法一 -->
<select id="selectByPrimaryKey1" parameterType="java.lang.Integer"
resultMap="BaseResultMap1">
select a.id, a.name, a.age, b.id class_info_id, class_desc
from student a,class_info b
where a.id = #{id,jdbcType=INTEGER} and a.class_id=b.id
</select>
<!-- 方法二 -->
<select id="selectByPrimaryKey2" parameterType="java.lang.Integer"
resultMap="BaseResultMap2">
select a.id, a.name, a.age, b.id classinfo_id, class_desc
from student a,class_info b
where a.id = #{id,jdbcType=INTEGER} and a.class_id=b.id
</select>
<!-- 方法三 -->
<select id="selectByPrimaryKey3" parameterType="java.lang.Integer"
resultMap="BaseResultMap3">
select a.id, a.name, a.age, b.id classinfo_id, class_desc
from student a,class_info b
where a.id = #{id,jdbcType=INTEGER} and a.class_id=b.id
</select>
<!-- 方法四 -->
<select id="selectByPrimaryKey4" parameterType="java.lang.Integer"
resultMap="BaseResultMap4">
select id, name, age, class_id
from student
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
ClassInfoMapper.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="com.sadoshi.SpringMyBatis.Mapper.ClassInfoMapper">
<resultMap id="BaseResultMap" type="com.sadoshi.SpringMyBatis.model.ClassInfo">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="class_desc" jdbcType="VARCHAR" property="classDesc" />
</resultMap>
<sql id="Base_Column_List">
id, class_desc
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from class_info
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
总结了一下,xml有四种方法实现一对一,主要是ResultMap的不同写法写法,Select语句是一样的。据说通常开发用方法四比较多。