1 环境
Maven 3.6.0+
https://blog.csdn.net/github_37759996/article/details/90748461开发工具:IDEA
必备插件
1、lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
2、 mysql
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3、jdbc
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
4、druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
5、mybatis
6、junit
7、log4j
8、SpringBoot2
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
9、devtools
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
2 构建一个SpringBoot2项目步骤
https://spring.io/quickstart
1、点击IDEA的创建项目;
2、点击spring initializer
3、编写常规说明;
4、选择插件;
5、完成。
3 springboot2 结构
image.png
1) Main.class 主启动类;
2) controller 控制层
3) Mapper 数据映射接口层
4) Dao 数据层
5) Service 服务层
6) Entities 实体层
7) application.yml 核心配置文件
8) Config 功能扩展层
9) Utils 工具层
13) resources/mybatis 数据映射xml层
3.1 Main.class示例
main文件
main文件包括java和resource两个文件夹,Java存放核心代码,resource存放配置文件和静态资源文件
image.png
xxx.application
- 作用:xxx.application是Springboot的主启动类;
- 示例如下:
package com.test.fri;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot2learntestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2learntestApplication.class, args);
}
}
3.2 controller示例
- 作用:存放控制层程序,接收前端发来的URL请求和参数,调用Service层的方法执行请求,返回结果;
- 示例:
package com.test.fri.controller;
import com.test.fri.Service.UrlCheckResult;
import com.test.fri.Service.UrlCheckService;
import com.test.fri.Service.UrlSaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class UrlHandler {
@Autowired
UrlCheckService urlCheckService;
@Autowired
UrlSaveService urlSaveService;
@RequestMapping(value = "/website/check/{url}", method = RequestMethod.GET)
public UrlCheckResult urlCheckResult(@PathVariable("url") String url){
return urlCheckService.getResult(url);
}
@RequestMapping(value = "/website/save/{url}",method = RequestMethod.GET)
public void urlSaver(@PathVariable("url") String url){
urlSaveService.saveCheckResult(url);
}
}
3.3 Mapper示例
- 作用:实体对象和sql语句的映射
- 示例:
<?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="org.springblade.core.log.mapper.LogApiMapper">
<!-- 通用查询映射结果 -->
<resultMap id="logResultMap" type="org.springblade.core.log.model.LogApi">
<result column="id" property="id"/>
<result column="create_time" property="createTime"/>
<result column="service_id" property="serviceId"/>
<result column="server_host" property="serverHost"/>
<result column="server_ip" property="serverIp"/>
<result column="env" property="env"/>
<result column="type" property="type"/>
<result column="title" property="title"/>
<result column="method" property="method"/>
<result column="request_uri" property="requestUri"/>
<result column="user_agent" property="userAgent"/>
<result column="remote_ip" property="remoteIp"/>
<result column="method_class" property="methodClass"/>
<result column="method_name" property="methodName"/>
<result column="params" property="params"/>
<result column="time" property="time"/>
<result column="create_by" property="createBy"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="baseColumnList">
select id,
create_time AS createTime,
service_id, server_host, server_ip, env, type, title, method, request_uri, user_agent, remote_ip, method_class, method_name, params, time, create_by
</sql>
</mapper>
3.4 Dao示例
- 作用:存储Mapper映射方法的接口程序
package org.springblade.core.log.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springblade.core.log.model.LogApi;
/**
* Mapper 接口
*
* @author Chill
* @since 2018-09-26
*/
public interface LogApiMapper extends BaseMapper<LogApi> {
}
3.5 Service示例
- 作用:处理数据的具体方法
- 示例:
package com.test.fri.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UrlCheckService {
@Autowired
// urlCheckResult是个json
UrlCheckResult urlCheckResult;
public UrlCheckResult getResult(String url){
// method body
return this.urlCheckResult;
}
}
3.6 Entities示例
- 作用: 定义实体,用于和数据产生映射
- 示例:
package com.test.fri.Entities.AritlcleService;
import lombok.Data;
import org.springframework.stereotype.Component;
@Component
@Data
public class test {
private int var1;
private String var2;
}
3.7 application.yml示例
- 作用:配置程序的基本环境,例如服务器端口、数据源等信息
- 示例:
#配置服务器端口
server:
port: 8080
#配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/cache?useSSL=FALSE&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver