ToB企服应用市场:ToB评测及商务社交产业平台

标题: Spring Boot 整合 Mybatis [打印本页]

作者: 莫张周刘王    时间: 2024-3-20 00:56
标题: Spring Boot 整合 Mybatis
1、导入依赖
  1.                
  2.         <dependency>
  3.             <groupId>com.mysql</groupId>
  4.             <artifactId>mysql-connector-j</artifactId>
  5.             <version>8.1.0</version>
  6.         </dependency>
  7.         
  8.         <dependency>
  9.             <groupId>org.mybatis.spring.boot</groupId>
  10.             <artifactId>mybatis-spring-boot-starter</artifactId>
  11.             <version>2.1.4</version>
  12.         </dependency>
  13.         
  14.         <dependency>
  15.             <groupId>com.alibaba</groupId>
  16.             <artifactId>druid-spring-boot-starter</artifactId>
  17.             <version>1.2.16</version>
  18.         </dependency>
复制代码
导入依赖的时候,需要根据自己所使用的Spring Boot和MySQL的版本而定。
2、创建数据库

我这里使用的是MySQL数据库。
首先创建一个mybatis_learn的数据库。然后创建一个student的表。
  1. CREATE TABLE `student` (
  2.   `id` int NOT NULL,
  3.   `name` varchar(20) NOT NULL,
  4.   `sex` tinyint NOT NULL COMMENT '0为女1为男'
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
复制代码
对应的,要实现一个Java的实体类,来对应数据库的表。
  1. public class Student implements Serializable {
  2.     private static final long serialVersionUID = -339516038496531943L;
  3.     private String id;
  4.     private String name;
  5.     private String sex;
  6.        
  7.     // 省略了 get方法和set方法
  8. }
复制代码
3、配置文件
  1. # 数据源配置
  2. spring.datasource.druid.type=com.alibaba.druid.pool.DruidDataSource
  3. spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
  4. spring.datasource.druid.url=jdbc:mysql://localhost:3306/mybatis_learn?serverTimezone=UTC
  5. spring.datasource.druid.username=root
  6. spring.datasource.druid.password=123456
  7. # mybatis 配置
  8. # type aliases package是指定对应数据库实体类的包
  9. mybatis.type-aliases-package=com.example.spring_boot_mybatis.entity
  10. # 这里指定对应的xml文件
  11. mybatis.mapper-locations=classpath:mapper/*.xml
复制代码
4、编写各个文件


创建如图结构的各个包和文件。
(1)StudentMapper

这个接口是对应Mybatis的xml文件的接口,其中的方法对应每一种操作。
  1. @Component
  2. @Mapper
  3. public interface StudentMapper {
  4.     // 这里以查询为例子
  5.     Student queryStudentById(Integer id);
  6. }
复制代码
(2)StudentService和StudentServiceImpl
  1. @Service("studentService")
  2. public class StudentServiceImpl implements StudentService {
  3.     @Autowired
  4.     private StudentMapper studentMapper;
  5.     // 在这里调用了StudentMapper中的查询接口
  6.     @Override
  7.     public Student queryStudentById(Integer id) {
  8.         return this.studentMapper.queryStudentById(id);
  9.     }
  10. }
复制代码
(3)StudentMapper.xml

这个文件中就要写SQL的各种语句
其中开头的格式都是固定的。
namespace要对应写入上面mapper包中要实现的Mapper接口的类名。
在select语句中的id则要对应其中的方法名。
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3.         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.example.spring_boot_mybatis.mapper.StudentMapper">
  6.     <select id="queryStudentById" resultType="student">
  7.         select * from student where id = #{id}
  8.     </select>
  9. </mapper>
复制代码
(4)编写Controller进行测试
  1. @RestController
  2. public class TestController {
  3.     @Autowired
  4.     private StudentService studentService;
  5.     @RequestMapping(value = "/queryStudent", method = RequestMethod.GET)
  6.     public Student queryStudentById(Integer id) {
  7.         return this.studentService.queryStudentById(id);
  8.     }
  9. }
复制代码
controller编写了一个查询接口,只要传入id就能返回数据库中的内容。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4