Springboot整和mybatis方法一
使用xml文件
项目目录结构如下
1pom.xml文件中添加依赖(插件库下载lombok使用@Data注解)
<?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.3.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xxx.proj</groupId>
<artifactId>springboot-mybatis-demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis-demo2</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<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>
</plugins>
</build>
</project>
2application.yml文件中添加(格式要对要有空格和缩进)
spring:
datasource:
url: jdbc:mysql:///test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
3Student实体类的创建
package com.xxx.proj.springbootmybatisdemo2.entity;
import lombok.Data;
@Data
public class Student {
private Integer id;
private String name;
private String address;
}
4StudentMapper的创建
package com.xxx.proj.springbootmybatisdemo2.mapper;
import com.xxx.proj.springbootmybatisdemo2.entity.Student;
import java.util.List;
public interface StudentMapper {
List<Student> findAll();
}
5StudentController类的创建
package com.xxx.proj.springbootmybatisdemo2.controller;
import com.xxx.proj.springbootmybatisdemo2.entity.Student;
import com.xxx.proj.springbootmybatisdemo2.mapper.StudentMapper;
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
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@RequestMapping("/")
public List<Student> findAll() {
return studentMapper.findAll();
}
}
6StudentMapper.xml文件的创建
<?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.xxx.proj.springbootmybatisdemo2.mapper.StudentMapper">
<select id="findAll" resultType="com.xxx.proj.springbootmybatisdemo2.entity.Student">
select * from student
</select>
</mapper>
7启动类添加注解去扫描包然后运行启动类
@SpringBootApplication
@MapperScan("com.xxx.proj.springbootmybatisdemo2.mapper")
说明:访问localhost:8080,没有修改项目访问目录所以是8080,lombok插件和依赖必须有不然@Data注解就用不了