本文共 4036 字,大约阅读时间需要 13 分钟。
使用xml文件
项目目录结构如下
1pom.xml文件中添加依赖(插件库下载lombok使用@Data注解)
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.11.RELEASE com.xxx.proj springboot-mybatis-demo2 0.0.1-SNAPSHOT springboot-mybatis-demo2 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web 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 src/main/java **/*.xml false org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
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;@Datapublic 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 { ListfindAll();}
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;@RestControllerpublic class StudentController { @Autowired private StudentMapper studentMapper; @RequestMapping("/") public ListfindAll() { return studentMapper.findAll(); }}
6StudentMapper.xml文件的创建
7启动类添加注解去扫描包然后运行启动类
@SpringBootApplication@MapperScan("com.xxx.proj.springbootmybatisdemo2.mapper")
说明:访问localhost:8080,没有修改项目访问目录所以是8080,lombok插件和依赖必须有不然@Data注解就用不了
转载地址:http://nqrwi.baihongyu.com/