SpringBoot整合MybatisPlus的一对多查询

第一步:导入相对应的依赖包 :部分名字自行修改

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.linyi.boot</groupId>
    <artifactId>boot-00-mybatisplus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-00-mybatisplus</name>
    <description>整合Mybatis_Plus</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        mv-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

<!--        druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.6</version>
        </dependency>
<!--        Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.linyi.boot.Boot00MybatisplusApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

第二步:在包下创建对应的pojo实体类

package com.wula.stumanage.pojo;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.Date;

/**
 * 成绩表
 */
@Data
@TableName("source")
public class TSource {
    @TableId(value = "stu_no",type = IdType.AUTO)
    private Integer stuNo;
//    @TableId(value = "course_id")
    private Integer courseId;
    private Float Source;
    private Date createTime;
    private Date updateTime;

}

在学生表中插入一个List<TSource>属性

在其上方标注:@TableField(exist = false)注解,表示不是表内属性字段
package com.wula.stumanage.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
import java.util.List;

/**
 学生表
**/
@Data
@TableName("student")
public class Student {

    @TableId(value = "stu_no",type = IdType.AUTO)
    private Integer stuNo;

    private String stuName;

    private String Sex;

    private Integer Phone;

    private String Email;

    private Date Birth;

    private String Address;

    private String Pic;

    private Integer classId;
    @TableField(exist = false)
    private List<TSource> tSource;
    /**
     * 入学日期
     */
    private Date enroTime;
    /**
     * 创建日期
     */
    private Date createTime;
    /**
     * 修改日期
     */
    private Date updateTime;


}

第三步:创建对应的Mapper/Dao类

 extends BaseMapper<TSource> 作用:MybatisPlus自带的一些简单的SQL增删改查等操作。方便我们开发,提高效率
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wula.stumanage.pojo.Student;
import org.springframework.stereotype.Repository;

/**

 */
@Repository
public interface IStudentMapper extends BaseMapper<Student> {
}
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wula.stumanage.pojo.TSource;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface ISourceMapper extends BaseMapper<TSource> {
}

第四步:编写service层及其impl实现类


public interface IStudentService extends IService<Student> {

    /**
     * 根据学号获取全部成绩
     * @param no
     * @return
     */
    Student getAllByNo(Integer no);
}
@Service
public class StudentServiceImpl extends ServiceImpl<IStudentMapper, Student> implements IStudentService {
    @Autowired
    private ISourceMapper iSourceMapper;
    @Autowired
    private IStudentMapper studentMapper;
    @Override
    public Student getAllByNo(Integer no) {
        LambdaQueryWrapper<TSource> tSourceQueryWrapper = new LambdaQueryWrapper<>();
        tSourceQueryWrapper.like(no!=null,TSource::getStuNo,no);
        List<TSource> tSources = iSourceMapper.selectList(tSourceQueryWrapper);
        Student student = studentMapper.selectById(no);
        student.setTSource(tSources);
        return student;
    }
}

实现类中的这段,是使用Lambda去给查询添加条件

LambdaQueryWrapper<TSource> tSourceQueryWrapper = new LambdaQueryWrapper<>();
        tSourceQueryWrapper.like(no!=null,TSource::getStuNo,no);
        List<TSource> tSources = iSourceMapper.selectList(tSourceQueryWrapper);

第五步:最后在controller调用该service层的方法实现数据返回

@RestController
public class SourceController {

    @Autowired
    private IStudentService studentService;

    @RequestMapping("/byno")
    public Map test(Integer no){
        Map map = new HashMap();
        Student allByNo = studentService.getAllByNo(no);
        map.put("stu",allByNo);
        return map;
    }
}

最后显示的效果为:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值