Springboot整和mybatis方法二
没有使用xml文件
项目目录结构如下
1修改pom.xml文件
<?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-demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis-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-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>
<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>
2修改application.yml文件,mybatis配置会将数据库中的 "_"符合消去
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///information_schema
username: root
password: root
mybatis:
configuration:
map-underscore-to-camel-case: true
3Table实体类
package com.xxx.proj.springbootmybatisdemo1.entity;
import lombok.Data;
@Data
public class Table {
private String tableName;
private String tableSchema;
}
4TableMapper类
package com.xxx.proj.springbootmybatisdemo1.mapper;
import com.xxx.proj.springbootmybatisdemo1.entity.Table;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface TableMapper {
@Select("select * from tables")
List<Table> findAll();
}
5TableController文件
package com.xxx.proj.springbootmybatisdemo1.controller;
import com.xxx.proj.springbootmybatisdemo1.mapper.TableMapper;
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 TableController {
@Autowired
private TableMapper tableMapper;
@RequestMapping("/")
public List findAll() {
return tableMapper.findAll();
}
}
6就可以启动项目了,默认端口修改为80的话浏览器输入localhost就可以启动了,没有修改就是localhost:8080
上一张效果图