关于Spring Boot不在赘述,如官网所言:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
创建的项目结构如下图所示:
示例使用InetlliJ IDEA开发。
一、打开IDEA,使用创建新项目功能,如下图所示:
二、Maven配置
<?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.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</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-aop</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.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--注意:spring boot默认不会编译xml文件,需要单独配置,才能将xml复制到classes目录下-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Maven的配置文件在创建项目的时候大部分已经自动生成,需要注意的是我们自己要单独配置一下xml文件资源。
三、配置application.properties文件
server.port=8080
server.servlet.session.timeout=30
server.tomcat.uri-encoding=utf-8
spring.datasource.url=jdbc:mysql://localhost:3306/guns?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&rewriteBatchedStatements=TRUE&allowMultiQueries=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=ordinary985
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database=mysql
// 自动扫描com.springboot.test.dao下的xml文件
mybatis.type-aliases-package=com.springboot.test.dao
四、配置mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
这个文件没什么用,然而我在测试的时候添加了这个文件,但是删不掉,所以一并附在这里。
五、依照项目结构
根据SSM的项目结构,分层创建JavaWeb项目。
package com.springboot.test.controller;
import com.springboot.test.pojo.SysUser;
import com.springboot.test.pojo.SysUserExample;
import com.springboot.test.services.SysUserService;
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 // 这个注释可以使HTTP请求接口的时候自动返回JSON字符串
public class HelloWorld {
@Autowired
private SysUserService sysUserService;
@RequestMapping(value = "sayhello")
public List<SysUser> createHello() {
SysUserExample example = new SysUserExample();
List<SysUser> list = sysUserService.selectByExample(example);
return list;
}
}
controller的代码如上图所示。
其他结构与普通的SSM项目结构一致。需要注意的是项目结构中所示的SysUserMapper文件,即Mapper的接口文件需要使用“@Mapper”注解,这样Spring Boot才能扫描到Mapper文件。
请求结果如下图所示: