一对一,多对多映射配置

一对一,多对多映射配置

嵌套ResultMap方式

studentMapper.xml

<?xml version="1.0" encoding="utf8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.abc.mapper.StudentMapper">
<select id="getById" parameterType="int" resultMap="studentResultMap">
select s.id s_id,s.name s_name,s.gender s_gender,s.major s_major,
s.grade s_grade,t.id t_id,t.name t_name,t.gender t_gender,
t.title t_title,t.research_area t_research_area
from student s left join teacher t on s.supervisor_id = t.id
where s.id=#{id}
</select>
<resultMap id="studentResultMap" type="Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
<result property="gender" column="s_gender"/>
<result property="major"  column="s_major"/>
<result property="grade"  column="s_grade"/>
<!--一对一属性映射,使用resultMap属性引用下面的教师实体映射-->
<association property="supervisor" javaType="Teacher"
resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap"/>
</resultMap>
</mapper>
teacherMapper.xml

<?xml version="1.0" encoding="utf8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace的值是对应的映射器接口的完整名称-->
<mapper namespace="com.abc.mapper.TeacherMapper">
<!--TeacherMapper接口中getById方法对应的SQL语句。查询教师及其指导的学生的信息。由于教师、学生都有id、name、gender等属性,因此给教师的字段都起了别名-->
<select id="getById" parameterType="int" resultMap="supervisorResultMap">
select t.id t_id, t.name t_name, t.gender t_gender,
t.research_area t_research_area, t.title t_title,
s.id,s.name, s.gender,s.major,s.grade
from teacher t,student s where t.id=#{id}
and s.supervisor_id = t.id
</select>
<!--教师实体映射-->
<resultMap id="supervisorResultMap" type="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
<result property="gender" column="t_gender"/>
<result property="researchArea" column="t_research_area"/>
<result property="title" column="t_title"/>
<!--collection元素映射教师的指导学生集合的属性。resultMap
以命名空间名.resultMap的id的形式,引用studentResultMap。
需要注意的是,上面的select语句中学生的字段名/别名应与
studentResultMap中的column属性一致-->
<collection property="supStudents"
resultMap="com.abc.mapper.StudentMapper.studentResultMap"/>
</resultMap>
</mapper>

嵌套select方式(产生n+1问题)

studentMapper.xml

<?xml version="1.0" encoding="utf8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.abc.mapper.StudentMapper">
<select id="getById" parameterType="int" resultMap="studentResultMap">
select id,name,gender,major,grade,supervisor_id 
from student where id=#{id}
</select>
<select id="getStudents" parameterType="int" resultMap="studentResultMap">
select * from student where supervisor_id = #{id}
</select>
<resultMap id="studentResultMap" type="Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gender" column="gender"/>
<result property="major"  column="major"/>
<result property="grade"  column="grade"/>
<!--column="supervisor_id"不能少。此列的值作为参数
传递给要引用的select语句,用来查询相应学生的指导教师
的信息。select属性指定要引用的select语句-->
<association property="supervisor" javaType="Teacher"
column="supervisor_id" select="om.abc.mapper.TeacherMapper.getById"/>
</resultMap>
</mapper>
teacherMapper.xml

<?xml version="1.0" encoding="utf8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--与以前一样,namespace的值是对应的映射器接口的完整名称-->
<mapper namespace="com.abc.mapper.TeacherMapper">
<!--TeacherMapper接口中getById方法对应的SQL语句。
查询教师的信息。-->
<select id="getById" parameterType="int" resultMap="supervisorResultMap">
select * from teacher where id=#{id}
</select>
<!--教师实体映射-->
<resultMap id="supervisorResultMap" type="Teacher">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gender" column="gender"/>
<result property="researchArea" column="research_area"/>
<result property="title" column="title"/>
<!--ofType指collection包含的元素的类型,此属性不可少。
column属性指把上述的getById的select语句中的教师id列的值作为参数
传递给将要引用到的下述的getStudents的select语句,此属性不可少。
引用的形式为:命名空间.select语句id-->
<collection property="supStudents" column="id" ofType="Student"
select="com.abc.mapper.StudentMapper.getStudents"/>
</resultMap>
</mapper>

mybatis核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--表明重用预编译的SQL语句-->
<settings>
<setting name="defaultExecutorType" value="REUSE"/>
</settings>
<!--类型别名定义。今后可只用Student来代替它冗长的全限定名-->
<typeAliases>
<typeAlias alias="Student" type="com.abc.domain.Student"/>
<typeAlias alias="Teacher" type="com.abc.domain.Teacher"/>
</typeAliases>
<!--environments可包含多个environment元素。每个environment配置与数据库交互的细节,这里只需要配置一个。default属性是指在创建SqlSessionFactory时,若没有明确指定要用哪个environment,则使用此属性指定的-->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"/>
<!--使用连接池的数据源配置-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost/courseman"/>
<property name="username" value="courseman"/>
<property name="password" value="abc123"/>
</dataSource>
</environment>
</environments>
<!--指定要用到的mapper文件。以下的resource属性告诉MyBatis要在类路径下的resources目录下找StudentMapper.xml文件。我们将把mapper文件存放在src目录下的resources目录中-->
<mappers>
<mapper resource="resources/StudentMapper.xml"/>
<mapper resource="resources/TeacherMapper.xml"/>
</mappers>
</configuration>

Mapper接口

StudentMapper.java

package com.abc.mapper;
import com.abc.domain.Student;
public interface StudentMapper {
//根据学生ID查询学生实体
public Student getById(int id);
//增加一名学生
public void add(Student student);
//修改学生信息
public void update(Student student);
//删除学生信息
public void delete(int id);
//根据学生id查询学生实体(包含教师信息)
public Student getSudentTeacherById(int id);

}
TeacherMapper.java

package com.abc.mapper;
import com.abc.domain.Teacher;
public interface TeacherMapper {
public Teacher getById(int id);
//分页查询教师信息
//以“#{sort}”的方式引用此方法的sort参数值。
//当然也可以在@Param中使用其他名称,如@Param("mysort")
public List<Teacher> findTeacherByPage(
@Param("sort") String sort,//排序字段
@Param("dir") String dir,  //排序方向
@Param("start") int start, //起始记录
@Param("limit") int limit  //记录条数
}
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值