Spring|SpringBoot基础-Mybatis常用标签

准备数据

创建数据库,创建表
create database note; use note; CREATE TABLE `teacher` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `t_name` varchar(255) DEFAULT NULL COMMENT '姓名', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO `note`.`teacher`(`id`, `t_name`) VALUES (1, '姜文'); INSERT INTO `note`.`teacher`(`id`, `t_name`) VALUES (2, '罗翔'); CREATE TABLE `student` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `s_name` varchar(255) DEFAULT NULL COMMENT '姓名', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; INSERT INTO `note`.`student`(`id`, `s_name`) VALUES (1, '张三'); INSERT INTO `note`.`student`(`id`, `s_name`) VALUES (2, '李四'); INSERT INTO `note`.`student`(`id`, `s_name`) VALUES (3, '王五');

创建SpringBoot项目
创建项目,导入相关依赖
org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web com.alibaba fastjson 1.2.72 com.baomidou mybatis-plus-boot-starter 3.4.2 org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 org.springframework.boot spring-boot-devtools runtime true mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine

配置.yml文件
mybatis: mapper-locations: classpath:/mybatis/**/*.xml type-aliases-package: com.example.note.domain map-underscore-to-camel-case: true mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl typeAliasesPackage: com.example.note.domain mapperLocations: classpath:mybatis/**/*.xml global-config: db-config: id-type: autoserver: port: 8080 spring: application: name: note datasource: driver-class-name: com.mysql.cj.jdbc.Driver name: defaultDataSource password: '123456789' url: jdbc:mysql://localhost:3306/note?serverTimezone=UTC username: 'root'

编写内容
1.创建test包,创建Student,Teacher类
package com.example.note.test.domain; import lombok.Data; @Data public class Student { private Long id; private String sName; }

package com.example.note.test.domain; import lombok.Data; @Data public class Teacher { private Long id; private String tName; }

2.创建mapper接口(注意添加@Mapper注解)
package com.example.note.test.mapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface TestMapper { }

3.创建业务service接口
package com.example.note.test.service; public interface ITestService { }

业务service类(注意添加@Service注解)
package com.example.note.test.service.impl; import org.springframework.stereotype.Service; @Service public class TestServiceImpl extends ITestService { }

4.创建控制层controller
package com.example.note.test.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("test") public class TestController { }

5.在resources目录下创建mybatis/test目录,创建TestMapper.xml文件

Mybatis编写
1.查询:,写在标签中,,id是在命名空间中唯一的标识符。resultType期望从这条语句中返回结果的类全限定名或别名,我们写的Javabean类路径或Java包装类路径。resultMap对外部 标签 的命名引用。resultType 和 resultMap 之间只能同时使用一个。用法如下(编辑TestMapper.xml):
select * from student

编写TestMapper
package com.example.note.test.mapper; import com.example.note.test.domain.Student; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface TestMapper { public List getAllStudent(); }

编写ITestService
package com.example.note.test.service; import com.example.note.test.domain.Student; import java.util.List; public interface ITestService { public List getAllStudent(); }

编写TestServiceImpl
package com.example.note.test.service.impl; import com.example.note.test.domain.Student; import com.example.note.test.mapper.TestMapper; import com.example.note.test.service.ITestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class TestServiceImpl implements ITestService { @Autowired private TestMapper testMapper; public List getAllStudent(){ return this.testMapper.getAllStudent(); } }

编写TestController
package com.example.note.test.controller; import com.example.note.test.domain.Student; import com.example.note.test.service.impl.TestServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("test") public class TestController { @Autowired private TestServiceImpl testService; @RequestMapping("getAllStudent") public List getAllStudent(){ return this.testService.getAllStudent(); } }

前端访问:
Spring|SpringBoot基础-Mybatis常用标签
文章图片


2.条件查询:条件判断语句,满足test里面的条件,执行标签里面的语句。
select * from student where id = #{id}

3.条件查询:满足test里面的条件,执行标签里面的语句。
select * from student id = #{id} and s_name = #{name}

4.插入:写在标签中,id是命名空间唯一标识符;parameterType入参类型;keyProperty指定能够唯一识别对象的属性;keyColumn设置生成键值在表中的列名;MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键。
insert into student (s_name) values (#{student.name})

5.遍历语句item是循环中的一个对象;index索引变量;collection传入的对象集合的名称;open指定开头的字符串;separator指定的集合项迭代之间的分隔符;close指定的结尾的字符串。批量插入操作如下:
insert into student (s_name) values ( #{item.sName} )

6.返回最后的自增id(相当于执行 select LAST_INSERT_ID()):
insert into student (s_name) values (#{student.sName} )

7.修改:写在标签中,id是命名空间唯一标识符;parameterType入参类型;keyProperty指定能够唯一识别对象的属性;keyColumn设置生成键值在表中的列名; MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键。
update student set s_name=#{student.sName}where id=#{student.id}

8.删除:写在标签中,id是命名空间唯一标识符;parameterType入参类型。
delete from student where id=#{id}

9.结果映射:,id是在命名空间中唯一的标识符。type期望从这条语句中返回结果的类全限定名或别名(Javabean类路径)。用法如下:
select * from student

10.关联:property映射到列结果的字段或属性;javaType一个 Java 类的完全限定名,或一个类型别名。用法如下(一对一,多对一查询):
修改数据库:
CREATE TABLE `student` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `s_name` varchar(255) DEFAULT NULL COMMENT '姓名', `t_id` bigint(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4;

修改Student类:
package com.example.note.test.domain; import lombok.Data; @Data public class Student { private Long id; private String sName; private Long tId; private Teacher teacher; }

编辑xml
select * from student left join teacher on student.t_id=teacher.id

查询结果:
Spring|SpringBoot基础-Mybatis常用标签
文章图片


11.集合:property映射到列结果的字段或属性;ofType一个 Java 类的完全限定名,或一个类型别名。用法如下(一对多查询):
修改Student类:
package com.example.note.test.domain; import lombok.Data; @Data public class Student { private Long id; private String sName; }

修改Teacher类:
package com.example.note.test.domain; import lombok.Data; import java.util.List; @Data public class Teacher { private Long id; private String tName; private List students; }

编辑xml
select * from teacher left join student on student.t_id=teacher.id

查询结果:
【Spring|SpringBoot基础-Mybatis常用标签】Spring|SpringBoot基础-Mybatis常用标签
文章图片


总结
上述就是我在平时学习和SpringBoot项目里面用到的比较多的mybatis标签及他们的简单用法。

    推荐阅读