第一步:配置pom文件,配置mybatis和数据库相关依赖
这里mybatis采用的是3.4.5版本,注意:mybatis3.4.0版本以上才支持@Mapper注解
相关依赖代码如下:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
pom文件完整代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-demo1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
导入pom文件后,若依赖报红,需要重新import下(点击下图里的刷新图标)
第二步:在项目里新建相关包,在对应包下添加如下文件
这边主要设计到的包有Controller(控制层),Entity(实体层),Service(接口层),ServiceImpl(接口实现层),Mapper(数据持久化层)
第三步:编写实体类,数据库建对应的表
在Entity包下编写student实体类,代码如下
package com.example.demo.Entity;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Data
public class Student {
private String id;
private String name;
private Integer age;
private String sex;
private String grade;
public Student(String id,String name,Integer age,String sex,String grade)
{
this.id = id;
this.name=name;
this.age=age;
this.sex=sex;
this.grade=grade;
}
}
数据库中新建一张学生表:
表中插入数据,用于后续接口验证
第四步: 在application.properties中配置数据库和mybatis相关属性
port指的是端口号,可以不用设置(这边设置是因为8080端口被占用)
数据库的名称、账号和密码修改成自己的即可
server.port=8081
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stms?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
mybatis.type-aliases-package=com.example.demo.Entity
mybatis.mapper-locations= classpath:com.example.demo.Mapper/*.xml
第五步:添加Mapper接口文件和对应xml文件
可以在启动类上添加@MapperScan注解,或者在mapper层的每个接口类上添加@Mapper注解@MapperScan注解作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
@MapperScan(basePackages = "com.example.demo.Mapper")
studentMapper接口类代码如下
package com.example.demo.Mapper;
import com.example.demo.Entity.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentMapper {
List<Student> selectAll();
}
在resource下建立如下目录和文件
完整代码如下:
注意事项:namespace的值要和接口类的路径对应上,否则会报错
<?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.example.demo.Mapper.StudentMapper">
<select id="selectAll" resultType="com.example.demo.Entity.Student">
select id,name,age,sex,grade
from `student`
</select>
</mapper>
第六步:编写Controller层
在Controller层的StudentController文件里编写测试代码,如下:
package com.example.demo.Controller; import com.example.demo.DTO.ResultDto; import com.example.demo.Entity.Student; import com.example.demo.Mapper.StudentMapper; import com.example.demo.Service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; @Autowired private StudentMapper studentMapper; @GetMapping("/getAll") public String selectAll() { List<Student> students = studentMapper.selectAll(); return students.toString(); } }
最后一步:在浏览器输入http://localhost:8081/student/getAll
出现如下数据,则证明集成成功 :
写在最后:创作不易,如果这边文章对您有所帮助,不妨点赞支持下,十分感谢!
相关文章
暂无评论...