关于mybatis:mybatis的基本使用基本关联查询

4次阅读

共计 3712 个字符,预计需要花费 10 分钟才能阅读完成。

所需依赖:

    <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 实体中。
运行成果:

正文完
 0