多对一映射(主表和附表)
Mapping映射到JVM中得到两个对象
多个的我们称为主表(一的那个作为多个的属性property存在)
谁是主表,多对一,多是主表,一对多,一在前,一是主表
如果主表是t_student,那么JVM的主对象就是Student对象
内存结构中就会据此生成student的成员变量clazz对象(通过这个引用就找到了这个对象)
反过来如果是研究一对多,那么就是clazz类的对象实例中有一个List<Student>,通过student的cid进行关联
多对一映射(主表和附表)
Mapping映射到JVM中得到两个对象
多个的我们称为主表(一的那个作为多个的属性property存在)
谁是主表,多对一,多是主表,一对多,一在前,一是主表
如果主表是t_student,那么JVM的主对象就是Student对象
内存结构中就会据此生成student的成员变量clazz对象(通过这个引用就找到了这个对象)
反过来如果是研究一对多,那么就是clazz类的对象实例中有一个List<Student>,通过student的cid进行关联
<?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.powernode.mybatis.mappers.StudentMapper">
<resultMap id="studentResultMap" type="student">
<id property="sid" column="sid"></id>
<result property="sname" column="sname"></result>
<result property="clazz.cid" column="cid"></result>
<result property="clazz.cname" column="cname"></result>
</resultMap>
<select id="selectById" resultMap="studentResultMap">
select
s.sid,s.sname,c.cid,c.cname
from
t_stu as s left join t_clazz as c on s.cid = c.cid
where
s.sid = #{sid};
</select>
</mapper>
<?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.powernode.mybatis.mappers.StudentMapper">
<resultMap id="studentResultMap" type="student">
<id property="sid" column="sid"></id>
<result property="sname" column="sname"></result>
<result property="clazz.cid" column="cid"></result>
<result property="clazz.cname" column="cname"></result>
</resultMap>
<select id="selectById" resultMap="studentResultMap">
select
s.sid,s.sname,c.cid,c.cname
from
t_stu as s left join t_clazz as c on s.cid = c.cid
where
s.sid = #{sid};
</select>
</mapper>
package com.powernode.mybatis.mappers;
import com.powernode.mybatis.pojo.Student;
public interface StudentMapper
{
//根据ID获取学生信息,获取学生关联的ID信息
Student selectById(Integer id);
}
package com.powernode.mybatis.mappers;
import com.powernode.mybatis.pojo.Student;
public interface StudentMapper
{
//根据ID获取学生信息,获取学生关联的ID信息
Student selectById(Integer id);
}
package com.powernode.mybatis.Test;
import com.powernode.mybatis.mappers.StudentMapper;
import com.powernode.mybatis.pojo.Student;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StudentMapperTest
{
private static final Logger logger = LoggerFactory.getLogger(StudentMapperTest.class);
@Test
public void selectById()
{
SqlSession sqlSession = SqlSessionUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectById(1);
logger.info(student.toString());
SqlSessionUtils.close(sqlSession);
}
}
package com.powernode.mybatis.Test;
import com.powernode.mybatis.mappers.StudentMapper;
import com.powernode.mybatis.pojo.Student;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StudentMapperTest
{
private static final Logger logger = LoggerFactory.getLogger(StudentMapperTest.class);
@Test
public void selectById()
{
SqlSession sqlSession = SqlSessionUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectById(1);
logger.info(student.toString());
SqlSessionUtils.close(sqlSession);
}
}