Mybatis-Plus多表联查
2024-09-04 15:55:49
表格结构:
CREATE TABLE `ssmpdemo`.`person_test` (
`id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`type` int(4) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `type`(`type`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `ssmpdemo`.`type_test` (
`id` int(4) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
使用Mapper文件
使用Mapper映射进行多表查询,通过定义字段的映射关系:
- 定义resultMap 定义当前实体的属性以及子对象的属性。
- 和数据库字段名称相同的属性也要定义。
- 外键可用
<association>
或者<collection>
。指定对应的类用javaType="com.example.ssmpdemo.entity.MyType"
字段。
实体类:
package com.example.ssmpdemo.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("person_test")
public class Person {
@TableId
@TableField("id")
private String id;
@TableField("name")
private String name;
@TableField(value = "type", exist = false)
private MyType myType;
}
Mapper
接口
package com.example.ssmpdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ssmpdemo.entity.Person;
import java.util.List;
public interface PersonMapper extends BaseMapper<Person> {
public List<Person> getFullData();
}
Mapper.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.example.ssmpdemo.mapper.PersonMapper">
<!--定义结果类型-->
<resultMap id="BaseResultMap" type="com.example.ssmpdemo.entity.Person">
<id property="id" column="id" jdbcType="VARCHAR"/>
<result property="name" column="NAME" jdbcType="VARCHAR"/>
<!--写成 association 和 collection 都可-->
<association property="myType" javaType="com.example.ssmpdemo.entity.MyType">
<id property="id" column="typeid" jdbcType="INTEGER" />
<result property="name" column="typename" jdbcType="VARCHAR" />
</association>
</resultMap>
<!--定义查询语句,注意字段名不要相同,不然在结果类型中有同名字段无法匹配-->
<select id="getFullData" resultMap="BaseResultMap">
select person_test.*, type_test.id as typeid, type_test.name as typename from person_test, type_test where person_test.type=type_test.id
</select>
</mapper>
Mapper.xml 文件也可以简化。<collection>
中可以使用Mapper
接口中已有的查询方法,避免重复定义子对象。
<resultMap id="BaseResultMap" type="com.example.ssmpdemo.entity.Person">
<id property="id" column="id" jdbcType="VARCHAR"/>
<result property="name" column="NAME" jdbcType="VARCHAR"/>
<!--typeid表示外键字段-->
<collection property="myType" column="typeid"
select="com.example.ssmpdemo.mapper.TypeMapper.selectById" />
</resultMap>
使用 VO
建立VO
import lombok.Data;
@Data
public class PersonVO {
String id;
String name;
Integer typeid;
String typename;
}
使用@Select指定查询sql,查询的字段需要一一对应。
import java.util.List;
public interface PersonMapper extends BaseMapper<Person> {
public List<Person> getFullData();
@Select("select person_test.id, person_test.name ,type_test.id as typeid, type_test.name as typename \n" +
"from person_test, type_test \n" +
"where person_test.type=type_test.id\n")
public List<PersonVO> getFullData2();
}
也可使用Mapper.xml
进行匹配。使用resultType
指定类型。
<select id="getFullData2" resultType="com.example.ssmpdemo.entity.vo.PersonVO">
select person_test.*, type_test.id as typeid, type_test.name as typename from person_test, type_test where person_test.type=type_test.id
</select>
最新文章
- Java基础-输入输出-2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
- html4,xhtml,html5发展历史
- ubantu eclipe
- Python中读取csv文件内容方法
- WordPress模板常用函数
- 【iCore3 双核心板_FPGA】例程十三:FSMC总线通信实验——复用地址模式
- 【译】深入理解python3.4中Asyncio库与Node.js的异步IO机制
- SQL获取数据库名,表名,列名,说明等信息
- PHP MySQL 预处理语句
- CTP API开发期货自动交易平台概论
- 【LeeetCode】4. Median of Two Sorted Arrays
- use vue vuex vue-router, not use webpack
- ReactNative学习之css样式使用
- ecshop 商品属性显示方法
- Mac下使用数据库将Excel数据转换存入.plist
- Luffy之支付宝支付开发API
- 用doxygen自动生成文档
- AndrodStudio报错: Cannot launch AVD in emulator.
- python selenium - web自动化环境搭建
- Python学习(24):Python面向对象(2)