引入mybatis依赖

在pom.xml文件中的标签内部添加下列语句

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>2.0.1</version>
  5. </dependency>

内部标签的含义

MyBatis是一个用.xml或者注解来写SQL语句的框架,但一般都用.xml

mapper

  1. <mapper namespace="com.ouyangjianrong.demo.mapper.UserMapper"></mapper>

一个映射,namespace属性的值为映射的接口,即填写Mapper映射接口的路径,以java文件为默认初始路径

mapper下的resultMap

  1. <resultMap id="User" type="com.ouyangjianrong.demo.model.User"></resultMap>

resultMap是一个实体类的结果集合,里边装的是实体类的各属性对应的数据库中的某一列。其中id可以任意取名,只是一个标识,而type则要对应一个实体类的路径,能够让MaBatis找到所依赖的实体类。

resultMap下的标签

  1. <resultMap ...>
  2. <result property="id" column="id"/>
  3. <result property="name" column="name"/>
  4. ...
  5. <association property="operatorType" javaType="com.ouyangjianrong.demo.model.OperatorType">
  6. <id property="type" column="operator_type"></id>
  7. <result property="name" column="name"></result>
  8. </association>
  9. </resultMap>

result表示一个实体类的属性与数据库中列的对应关系,property指实体类的属性,column指数据库中某张表的列名。association表示将该实体类与另一个实体类关联起来,也相当于是一张表的外键,主要用于联合查询。property的含义是实体类的一个属性,且该属性是一个复杂数据类型,即一个JavaBean属性;javaType的含义是对应连接外部实体类的路径。association内部的id标签为主键,用法和result标签基本相同。

select

  1. <select id="selectUser" resultType="java.util.Map">
  2. select u.id,u.name,.u.password,o.name, as operatorName,u.avatar,u.email,u.gender,o.operator_type from user u,operatortype o where u.operator_type=o.operator_type
  3. <if test="id != null">
  4. and u.id = #{id}
  5. </if>
  6. </insert>

select标签用于进行数据库的查询,内部写sql语句,resultType表示将查询的结果存入Map中,也可以不是Map,标签内的id属性表示对应的mapper接口的方法,后端通过调用该mapper接口的selectUser方法的时候,便会调用该条sql语句。

insert,delete,update的用法和select基本上相似,都是标签内部写sql语句,但是并不需要resultType返回值。

if语句的注意事项:

逻辑连接符没有&&、||,代替的是用and、or代替。

后端调用mabatis的一个基本过程

第一步

前端通过路由访问后端方法的时候,会通过该注解找到Controller层的方法。

例:

  1. @RestController
  2. @RequestMapping("/user")
  3. public class UserController {
  4. @Resource
  5. UserService userService;
  6. @GetMapping(value="/selectUser")
  7. public List<Map<String,Object>> selectUser(@RequestParam Integer id) {
  8. return userService.selectUser(id);
  9. }
  10. ...
  11. }

前端通过/user/selectUser,便可以通过注解找到selectUser方法,并将数据传入id中。

第二步

Service通过调用Mapper层的方法跳转到Mapper当中。

例:

  1. @Service
  2. public class UserService {
  3. @Resource
  4. private UserMapper userMapper;
  5. public List<Map<String,Object>> selectUser(Integer id) {
  6. return userMapper.selectUser(id);
  7. }
  8. ...
  9. }

@Service注解是用来对该层进行标注的,告诉SpringBoot该类是Service层。通过Controller层调用Service层的方法,Service再调用Mapper层的方法,跳转到xml中。

我最开始觉得将改成删掉直接让Controller层调用Mapper层会更方便一点,就不用去写一个单纯用于调用的Service层,但是删掉之后再调用该层的时候程序跑不起来,报的错误就是’A component required a bean of type ‘…Service’ that could not be found.某个bean的Service找不到 ,将Service层加上去之后就可以正常的运行了。不过据说这只是一种规范,是可以删掉的,但是我删掉为什么程序跑不起来就不是很清楚了。

第三步

调用Mapper层的方法的时候,xml对应的sql语句便会开始执行。

例:

  1. public interface UserMapper {
  2. List<Map<String,Object>> selectUser(@Param("id") id);
  3. ...
  4. }

调用selectUser的时候,便会跳到xml中的对应的id的sql语句中。

其中,xml中能够正确的找到数据库中的某一张表,是靠着@Table注解实现的,使用这个注解,在生成的Bean的时候就能通过该注解找到表。

例:

  1. @Table(name="user")
  2. public class User {
  3. ...
  4. }