所需依赖:

    <groupId>com.baomidou</groupId>    <artifactId>mybatis-plus-boot-starter</artifactId>    <version>3.3.1.tmp</version>
graph TD    A[前台发出请求] -->|依据url匹配到c层对应办法| B(Controller)    B --> C(调用服务层对应办法)

那么服务层中的办法从哪里来呢(比方最简略的save、delete等)

其中Iservice中次要蕴含了罕用的save、update、remove(delete)、page等办法。
并且他们的实现都依赖于ServiceImpl类并且利用BaseMapper辅助实现。
也就是说在mybatis中并不需要仓库层来辅助实现各项操作。

也就是说咱们如果想要实现简略的增删改查只须要配置好上述关系并在C层中进行调用即可
例:对于test的增删改查

@RestController@RequestMapping("/test")public class testController {  @Autowired  private TestService testService;  @PostMapping  public RespBean addTest(@RequestBody test test){    testService.save(test);    return RespBean.success("增加胜利。");  }  @GetMapping()  public List<test> getAllSettlement(){    return testService.list();  }  @PutMapping()  public RespBean updateUser(@RequestBody test test){    if(testService.updateById(test)){      RespBean.success("更新用户信息胜利");    }    return RespBean.success("更新用户信息失败");  }  @DeleteMapping("/{id}")  public RespBean deleteTest(@PathVariable String id) {    testService.removeById(id);    return RespBean.success("胜利删除test");  }}

说完了最根本的需要,那么要怎么依据前台传来的参数进行查问或者分页呢?
这就须要配置Mapper.xml来结构查问条件并与之前定义的Mapper接口进行关联。

首先就是最根本的含糊查问:

public interface TestMapper extends BaseMapper<Test> {  List<Test> getTestsByName(String name);}
<?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.recorde.server.mapper.TestMapper">    <!-- 通用查问映射后果 -->    <!-- 即把实体中字段与数据库中字段进行对应 -->    <resultMap id="BaseResultMap" type="com.recorde.server.model.Test">        <result column="id" property="id"/>        <result column="name" property="name"/>    </resultMap>    <!-- 通用查问后果列 -->    <sql id="Base_Column_List">        id        , name    </sql>    <select id="getTestsByName" resultType="com.recorde.server.model.Test">        select *        from test        where name like concat('%', #{name}::text, '%')    </select></mapper>

其中的select项中的id就是负责和mapper中办法进行绑定从而针对此办法进行查问,标签内的内容就是sql语句间接进行查问。
ManyToOne 左关联查问:
数据筹备:

public class FatherTest {  private String id;  private String name;  @TableField(exist = false)  @ApiModelProperty(value = "子test信息")  private List<Test> tests;}

public class Test {  private String id;  private String name;  private String fatherTestId;}


首先咱们要明确的就是什么是左关联:
很显著如上代码所示,Test对FatherTest是manytoOne的关系,咱们如果想要依据FatherTest中的字段(如fatherTest.name)寻找Test实体就须要进行关联查问。
而左关联就是关联查问一种形式,其sql语句如下:

SELECT * FROM table1 LEFT JOIN table2 ON table1.a=table2.b
示意将利用table1.a与table2.b绑定从而将table1和table2进行关联。
其中table1示意主表(左表)table2示意从表(右表)
左关联就示意主表元素全副保留,从表元素中如果没有对应元素则置为null。

比方对上述已有数据进行左关联:

SELECT    *FROM test AS a         LEFT JOIN     "fatherTest" AS b     ON         a."fatherTestId" = b.id

查问后果如下:

而咱们如果进行右关联查问就只须要将LEFT改为RIGHT即可,失去的数据保留状况与之相同:

当然还有其余关联形式在此就不一一列举了。
如果咱们想要依据父表数据查问子表只须要在此基础上加上where查问即可。

那么问题又来了,如果test也有子对象且为一对多关系那么在查问返回后果时要怎么连带其子对象返回呢?
数据筹备:

public class SonTest {  String id;  String name;  String testId;}

@TableName("test")public class Test {  private String id;  private String name;  private String fatherTestId;  @TableField(exist = false)  @ApiModelProperty(value = "存储查问出的子test信息,并且不需存储在数据库中")  private List<SonTest> sonTests;}


如果咱们不做任何改变返回后果中sonetests会为null,如下所示


所以咱们就须要对其返回后果进行规定,并且依据其返回后果的id与sonTestId相关联从而查问出test对应的sonTest并且将其退出到查问出的后果中。

所以咱们就须要对resultMap进行设定,并且将其与相应的查问进行绑定。

    <resultMap id="BaseResultMap" type="com.recorde.server.model.Test">        <result column="id" property="id"/>        <result column="name" property="name"/>        <result column="fatherTestId" property="fatherTestId"/>        <collection property="sonTests" javaType="ArrayList" ofType="com.recorde.server.model.SonTest"                    select="selectSons" column="id">            <result column="id" property="id"/>            <result column="testId" property="testId"/>            <result column="name" property="name"/>        </collection>    </resultMap>
    <select id="selectSons" resultType="com.recorde.server.model.SonTest" parameterType="string">        select *        from             "sonTest" a,            "test" b        where a."testId"=b."id"            and b."id"=#{id}    </select>

其中collection就能够实现查问并且返还list<sonTest>的性能。
其中的select属性就能够与<select>通过id进行绑定,并且将column作为输出进行查问并且通过property与Test进行绑定从而退出信息到Test实体中。
运行成果: