1 Maven
Maven是最流行的Java项目构建工具
为什么需要使用Maven?
在JavaWeb开发中Maven能够帮我们自动管理jar包、自动构建项目。
1.1 安装Maven
-
安装:解压压缩包即可。
-
Maven核心配置文件setting.xml
1.2 配置环境变量
MAVEN_HOME 配置Maven的目录
在系统的Path中配置 %MAVEN_HOME%\bin
打开DOS窗口输入mvn -v命令,查看版本。当出现下面信息就说明Maven安装成功了
1.3 配置镜像
<mirrors>
<!--
镜像:方便下载使用,Maven是国外网站,下载jar包很慢,配置镜像加快下载速度,建议国内使用阿里云镜像
-->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
1.4 配置本地仓库
本地仓库用于存放从服务器下载的jar包
<localRepository>D:/JavaTools/Maven/apache-maven-3.6.1/repository</localRepository>
1.5 Maven集成IDEA
idea中有一个自带的maven。 我们要让idea使用自己安装的maven。
-
选择File-->Settings-->Build,Execution,Deployment-->Maven
2.选择File-->Settings-->Build,Execution,Deployment-->Maven-->Runner
-DarchetypeCatalog=internal
3.选择File---> Other Settings---> Settings for New Projects
窗前其他的工程Maven配置也跟之前一样
2第一个SpringBoot程序
目标:向web浏览器输出HelloSpringBoot
步骤如下:
2.1创建父工程
2.2定义父工程的POM文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersio n>
<groupId>com.shouyi</groupId>
<artifactId>shouyixueyuan-springboot-water</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>shouyixueyuan-springboot-water-demo-01</module>
</modules>
<!--
POM Project Object Management 它是Maven工程的核心
<packaging>pom</packaging> 打包类型为pom,通常用在父工程中
作用如下:
1 用来做jar包的版本控制
2 父工程使用Maven分模块管理项目(每个父工程下面可以有若干个子模块)
3 子模块复用父模块的groupId、version。只需要定义artifactId
-->
<packaging>pom</packaging>
<!--统一管理依赖jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--SpringBoot版本是2.3.0.RELEASE-->
<springboot.version>2.3.0.RELEASE</springboot.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.23</druid.version>
<mybatisplus.version>3.3.2</mybatisplus.version>
<lombok.version>1.18.20</lombok.version>
<jcl-over.version>1.7.26</jcl-over.version>
<slf4j.version>1.7.26</slf4j.version>
<junit.version>1.8.0</junit.version>
<log4j.version>1.2.17</log4j.version>
<hutool.version>5.1.0</hutool.version>
<swagger.version>2.9.2</swagger.version>
</properties>
<!--父工程只管理子模块jar包的版本,不导入jar包,让子模块中引用父工程依赖而不用显示的列出版本号-->
<dependencyManagement>
<dependencies>
<!--使用的SpringBoot版本是2.3.0.RELEASE-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<!--只能用在dependencyManagement里面,作用是管理依赖jar包的版本,保持当前项目的所有依赖jar包的版本统一-->
<scope>import</scope>
<!--引入下载该Jar包的所有依赖-->
<type>pom</type>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mybatis-plus持久层框架启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<!--Druid数据库连接池启动器-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- 胡图工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!--
Lombok插件
-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<version>${lombok.version}</version>
<!--多个项目之间依赖不传递-->
<optional>true</optional>
</dependency>
<!--swagger是前后端分离的接口文档-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--
单元测试
-->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--
日志工具类
-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jcl-over.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<!--编译java源文件的同时导入src/main/java目录下所有的properties配置文件和xml配置文件-->
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<!-- 配置文件在编译时排除Java源文件-->
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<!--SpringBoot配置Maven插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3 创建子模块
选中工程,右键点击New-->Module
选择Maven,点击Next
定义模块名称,点击Finish
2.4 定义子模块的POM文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>minzu-springboot</artifactId>
<groupId>com.minzu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>minzu-springboot-demo-01</artifactId>
<dependencies>
<!--Springboot开发Web应用程序的启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除SpringBoot默认依赖的所有日志包-->
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 监控系统健康情况的启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--Springboot日志启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!--SpringBoot单元测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!--排除JUnit 4引擎测试-->
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--
提供很多实用注解的工具插件
-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!-- 胡图工具类依赖jar -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--日志依赖jar-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
</dependency>
</dependencies>
</project>
2.5编写YML配置文件
application.yml
.yml是一种特殊的配置文件,也是Springboot的配置文件
2.5.1 YML是什么
YAML (Ain't a Markup Language)YAML不是一种标记语言,通常以.yml为后缀的文件,是一种直观的能够被电脑识别的数据序列化格式,并且容易被人类阅读,一种专门用来写配置文件的语言。可用于如: Java,C/C++, Ruby, Python, Perl, C#, PHP等。
2.5.2 YML优点
-
YAML易于人们阅读。
-
YAML数据在编程语言之间是可移植的。
-
YAML具有表现力和可扩展性。
-
YAML易于实现和使用。
2.5.3 YML语法
-
k: v 表示键值对关系,冒号后面必须有一个空格
-
大小写敏感
-
缩进时不允许使用Tab键,只允许使用空格。
# 微服务应用程序端口号
server:
port: 8081
2.6 编写日志配置文件
在resources目录下定义日志配置文件log4j.properties,配置项如下:
# 将日志级别为DEBUG的日志信息输出到file(文件)和console(控制台)这两个目的地
log4j.rootLogger=INFO, file, console
# 文件输出的相关设置
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/System.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
#控制台输出的相关设置
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS} %5p %c{1}:%L - %m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
2.7编写控制器Controller
/**
* TODO
*
* @author caojie
* @version 1.0
* 被@RestController注解修饰的类在程序启动的时候会注入到SpringBoot容器中,表示这个类充当Controller(控制器),它
* 里面定义的方法全部以Json字符串的形式返回给浏览器
*/
@RestController
public class HelloController {
/**
* @GetMapping将HTTP GET请求映射到特定的方法。例如:将浏览器的/hello请求映射到hello()方法
* @return
*/
@GetMapping("/hello")
public String hello() {
return "Hello SpringBoot";
}
}
2.8编写SpringBoot启动类
package com.wenhua.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* TODO
* SpringBoot入口
* @author caojie
* @version 1.0
被@SpringBootApplication注解修饰的类是SpringBoot的启动类
*/
@SpringBootApplication
public class HelloSpringBoot {
public static void main(String[] args) {
SpringApplication.run(HelloSpringBoot.class,args);
}
}
2.9运行程序
http://localhost:8081/hello
2.10小结
父工程:1建立工程 2 编写父工程的POM 3删除父工程的src目录
子模块:1 建立子模块 2 编写POM 3 yml配置文件 4定义日志文件 5编写Controller控制器类 6 编写SpringBoot入口类
核心:
@SpringBootApplication @RestController
3 使用SpringBoot在浏览器输出客户信息
3.1创建子模块(Module)
3.2定义子模块的POM文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>shouyixueyuan-springboot-water</artifactId>
<groupId>com.shouyi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shouyixueyuan-springboot-water-demo-01</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除SpringBoot默认依赖的所有日志包-->
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 监控系统健康情况的工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--Springboot日志启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!--SpringBoot单元测试启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!--排除JUnit 4引擎测试-->
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--MyBatis-plus启动器 baomidou 包米豆-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!--
Druid数据库连接池启动器
阿里巴巴开发的数据库连接池,也是目前最好的连接池。特征:可以对SQL语句进行监控
-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--
提供很多实用注解的工具插件,
-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--swagger:前后端分离的接口文档-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!-- 胡图工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--日志配置依赖jar-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<!--单元测试依赖jar-->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
</dependency>
</dependencies>
</project>
3.3编写YML配置文件
application.yml
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/shouyi?useUnicode=true&characterEncoding=UTF8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
druid:
min-idle: 5
max-active: 10
max-wait: 5000
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 全局策略配置,全部都是用auto策略(数据库ID自增长)
global-config:
db-config:
id-type: auto
# 配置日志级别
logging:
com.shouyi: info
3.4 编写日志配置文件
# 将日志级别为DEBUG的日志信息输出到file(文件)和console(控制台)这两个目的地
log4j.rootLogger=INFO, file, console
# 文件输出的相关设置
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/caojie.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
#控制台输出的相关设置
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS} %5p %c{1}:%L - %m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
3.5定义相关的package
entities包放置实体类
mapper包放置Mapper映射文件
service包放置业务逻辑接口和实现类
controller包放置Controller控制器
3.6定义SpringBoot启动类
package com.wenhua.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* TODO
* @author caojie
* @version 1.0
@MapperScan扫描指定包下面的所有接口,在编译之后都会生成对应的实现类
*/
@SpringBootApplication
@MapperScan("com.wenhua.springboot.mapper")
public class CustomerSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerSpringBootApplication.class,args);
}
}
3.7 在IDEA里面安装Lombok插件
File--->Plugins
3.8定义Customer实体类
package com.wenhua.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* TODO
* @author caojie
* @version 1.0
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Customer {
private Integer cid;
private String custName;
private String custMobile;
private String custAddress;
private Integer custTicket;
}
3.9定义Mapper接口
封装了数据库访问的所有操作(CRUD 增删改查)。Mapper用来在数据库表和Java实体类的属性之间做映射
package com.wenhua.springboot.mapper;
import com.wenhua.springboot.entities.Customer;
import java.util.List;
/**
* TODO:Mapper用于在数据库表和Java实体类之间做映射,封装了数据库所有的增删改查操作,用于跟数据库交互
* @author caojie
* @version 1.0
*/
@Repository
public interface CustomerMapper extends BaseMapper<Customer> {
List<Customer> listCustomer();
}
3.10编写业务逻辑接口Service
package com.shouyi.service;
import com.shouyi.pojo.Customer;
import java.util.List;
/**
* TODO
* @author caojie
* @version 1.0
* @date 2021/10/09 13:10
*/
public interface CustomerService {
String hello();
List<Customer> listCustomer();
}
3.11编写Service实现类
package com.shouyi.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shouyi.mapper.CustomerMapper;
import com.shouyi.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* TODO
* @author caojie
* @version 1.0
* @date 2021/10/19 13:10
*/
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper customerMapper;
@Override
public List<Customer> listCustomer() {
return customerMapper.selectList(null);
}
}
3.12编写Controller控制器
package com.wenhua.springboot.controller;
import com.wenhua.springboot.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* TODO: 当客户在浏览器输入http://localhost:8097/listCustomer,请求会派发给CustomerController的listCustomer()方法处理该请求(向浏览器输出所有客户信息)
* @author caojie
* @version 1.0
* @date 2021/6/21 21:48
*/
@RestController
public class CustomerController {
/**
* @Autowired根据类型自动装配业务逻辑层的CustomerService对象
*/
@Autowired
private CustomerService customerService;
@GetMapping("/listCustomer")
public String listCustomer(){
return customerService.listCustomer().toString();
}
}
3.13启动应用程序
http://localhost:8081/listCustomer
3.14 小结
entities 包下面放置实体类 1
mapper包放置Mapper接口,它负责数据库表和Java实体类属性的映射 2
service包放置业务逻辑接口和实现类 3
controller包放置控制器类 4
service业务逻辑需要依赖Mapper接口,所以需要自动装配
controller控制器需要依赖service,所以需要自动装配