Mybatis实现多表联查

一、Mybatis实现多表联查询

1、Mybatis实现多表联查询方式

  • 业务装配对两个表写单独的sql语句,在业务(service)把查询结果进行联合。
  • 使用Auto Mapping特性,在实现两个表联合查询时通过别名完成自动映射。
  • 使用Mybatis的<resultMap>标签进行实现

2、多表查询时类中包含另一个对象的分类

  • 单个对象
  • 集合对象

二、resultMap标签

1、<resultMap>标签单表中的映射

写在<select>标签中,不用谢resultType属性,可以单独的在<resultMap>中将数据库字段与java属性不匹配进行映射。

2、使用<resultMap>标签在两个表中关联单个对象(N+1方式)

  • N+1查询的方式,先查询出一个表的全部信息,根据这个表的信息查询另一个表的信息。
  • 实现步骤在Student类中包含一个Teacher对象 
  • 在StudentMapper.xml文件中写上查询学生的sql,然后通过<resultMap>来完成Teacher对象的查询

具体代码:

java实体类:

public class Student {
	private int id;
	private String name;
	private int age;
	private int tid;
	private Teacher teacher;

TeacherMapper.xml文件中提供一个查询老师对象的sql:

<mapper namespace="com.test.mapper.TeacherMapper">
	<select id="selById" parameterType="int" resultType="Teacher">
	select * from teacher where id =#{0}
	</select>
</mapper>

在StudentMapper.xml中写上查询学生信息:

<mapper namespace="com.test.mapper.StudentMapper">
	<resultMap type="Student" id="stuMap">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="age" property="age"/>
		<result column="tid" property="tid"/>
		<association property="teacher" select="com.test.mapper.TeacherMapper.selById" 
        column="tid"></association>
	</resultMap>
	<select id="selAll" resultMap="stuMap" >
		select * from student
	</select>
</mapper>

几个属性标签说明:

  • <id>  主键
  • <result> 其它字段
  • <association>装配对象时使用,其中  property  表示类中的属性名  select  表示要执行的sql语句(写完整的sql语句) column 要传过去的字段参数 

解释:(N+1指的是每个学生类中有一个老师对象,因此一条sql查询出所有的学生,然后在查每个学生中的老师,即N+1).看日志信息:

补充说明:当其它字段一样时,可以在<resultMap>中不用写,但对于要传递的参数字段mybatia只装配一次因此要写

3、使用<resultMap>实现关联单个对象(联合查询方式)

只需要写一条SQL,在StudentMapper.xml中完成,对于学生属性直接用<id>或<result>进行装配(将字段别名与属性匹配),对于Teacher对象直接用<association>标签来映射,其中 property还是代表在类中该对象属性的名称   另外要设置javaType表示返回值类型,其它的还一次对应匹配即可。具体代码实例:

<!-- 在mapper中实现联合查询 -->
	<resultMap type="Student" id="stuMap1">
		<id column="sid" property="id"/>
		<result column="sname" property="name"/>
		<result column="sage" property="age"/>
		<result column="tid" property="tid"/>
		<association property="teacher" javaType="teacher">
			<id column="tid" property="id"/>
			<result column="tname" property="name"/>
		</association>
	</resultMap>
	
	<select id="selAll1" resultMap="stuMap1">
	select s.id sid,s.name sname,s.age sage,t.id tid,t.name tname 
	from student s left join teacher t on s.tid = t.id;
	</select>

4、使用<resultMap>查询关联集合对象(N+1)

  • 在实体类Teacher中加上含有多个Student的属性list,代码:
public class Teacher {
	private int id;
	private String name;
	private List<Student> list;
  • 在StudentMapper.xml中添加通过tid查询学生的sql语句,代码:
<mapper namespace="com.test.mapper.StudentMapper">
	<select id="selByTid" resultType="student" parameterType="int" >
		select * from student where tid = #{0}
	</select>
</mapper>
  • 在TeacherMapper.xml中添加查询全部,然后通过<resultMap>来装配其它的,代码:
<mapper namespace="com.test.mapper.TeacherMapper">
    <resultMap type="Teacher" id="teacMap">
	    <id column="id" property="id"/>
	    <result column="name" property="name"/>
	    <collection property="list" select="com.test.mapper.StudentMapper.selByTid" column="id"/>
    </resultMap>
    <select id="selAll" resultMap="teacMap">
	    select * from teacher
    </select>
</mapper>
	

还是和查询单个对象一样这里<resultMap>中使用<collection>来匹配集合,其中property还是类中的属性名,select是要执行的sql语句,column为要传递的参数字段。

5、使用<resultMap>实现加载集合数据(联合查询方式)

只需要写一条SQL,在TeacherMapper.xml中完成,对于老师的属性在<resultMap>中直接用<id>或<result>进行装配(将字段别名与属性匹配),对于Student对象集合用<collection>标签来映射,其中 property还是代表在类中该对象集合属性的名称   另外要设置ofType表示返回集合的泛型,其它的还一次对应匹配即可。具体代码实例:

<!-- 使用联合查询 -->
	<resultMap type="Teacher" id="teacMap1">
		<id column="tid" property="id"/>
		<result column="tname" property="name"/>
		<collection property="list" ofType="Student">
			<id column="sid" property="id"/>
			<result column="sname" property="name"/>
			<result column="age" property="age"/>
			<result column="tid1" property="tid"/>
		</collection>
	</resultMap>
	<select id="selAll1" resultMap="teacMap1">
		select t.id tid, t.name tname, s.id sid, s.name sname,age ,s.tid tid1
		from teacher t left join student s on t.id = s.tid
	</select>

三、使用Auto Mapping结合别名实现多表查询

  • 只能使用多表联合查询方式.
  • 要求:查询出的列别和属性名相同.
  • 实现方式,在 SQL 是关键字符,两侧添加反单引号
  • 只能适用于单个对象

代码实例:

<select id="selAll" resultType="student">
select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid
from student s LEFT JOIN teacher t on t.id=s.tid
</select>

四、Mybatis注解

  • 注解:为了简化配置文件.
  •  Mybatis 的注解简化 mapper.xml 文件.
  • 如果涉及动态 SQL 依然使用 mapper.xml
  • mapper.xml 和注解可以共存.
  • 使用注解时 mybatis.xml 中<mappers>使用
  • <package/>或 <mapper class=””/>

一般实例:

  • 实现查询:
@Select("select * from teacher")
List<Teacher> selAll();
  • 实现新增
@Insert("insert into teacher values(default,#{name})")
int insTeacher(Teacher teacher);

将主键带回来的方式:设置@Options注解并配置(useGeneratedKeys=true,keyProperty="id")

    @Insert("insert into log values(default,#{accOut},#{accIn},#{money})")
	@Options(useGeneratedKeys=true,keyProperty="id")
	int insLog(Log log);
  • 实现删除(主要看基本类型参数的取法)
@Delete("delete from teacher where id=#{0}")
int delById(int id);

 

  • 35
    点赞
  • 170
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: MyBatis Plus是一款针对MyBatis框架的增强工具, 它提供了很多方便的方法来实现多表联查. 你可以使用MyBatis Plus的`selectPage`方法来实现多表联查, 该方法接收一个`QueryWrapper`参数, 你可以在`QueryWrapper`中指定你要连接的表, 以及连接条件. 例如, 假设你有两张表: `t_user`和`t_order`, 并且你希望查询出每个用户的订单信息, 那么你可以使用如下代码来实现: ```java QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("u.status", 1) .leftJoin("t_order o", "u.id = o.user_id"); IPage<User> page = new Page<>(1, 10); IPage<User> result = userMapper.selectPage(page, queryWrapper); ``` 在这段代码中, `leftJoin`方法表示进行左连接, 你也可以使用`rightJoin`方法进行右连接, 或者不使用连接方法直接写连接条件. 另外, MyBatis Plus还提供了很多其他方法来实现多表联查, 例如`selectList`, `selectMaps`, `selectObjs`等, 这些方法的使用方法与`selectPage`类似. 希望这能帮到你! 如果你有其他问题, 请随时联系我. ### 回答2: MyBatis Plus提供了多种方法实现多表联查。以下是其中几种常见的方法: 1. 使用@TableName注解和实体类中的关联注解,例如@OneToOne、@OneToMany、@ManyToOne等注解来定义表之间的关系,然后使用Mapper的selectList方法进行联查。这种方式比较灵活,可以自定义查询条件。 2. 使用Wrapper进行多表联查。Wrapper是MyBatis Plus提供的一个查询条件构造器,可以通过指定条件来进行多表联查。例如,可以使用Wrapper的join方法来关联不同的表,然后使用selectList方法进行查询。 3. 使用自定义的SQL语句进行多表联查。可以编写自定义的SQL语句来实现多表联查,然后通过MyBatis Plus提供的方法进行查询。可以使用@Select注解或者XML文件配置进行SQL语句编写。 4. 使用MyBatis Plus提供的关联查询方法。MyBatis Plus提供了一些特定的关联查询方法,例如selectJoinPage、selectJoinList等方法,可以直接进行多表联查,省去了手动编写SQL语句的步骤。 总之,无论是通过注解还是通过Wrapper或者自定义SQL语句,MyBatis Plus都提供了多种方法来实现多表联查,开发者可以根据自己的需求和习惯进行选择和使用。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值