一、SpringBoot简介
1.1 原有Spring优缺点分析
1.1.1 Spring的优点分析
Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品。无需开发重量级的Enterprise JavaBean(EJB),Spring为企业级Java开发提供了一种相对简单的方法,通过依赖注入和面向切面编程,用简单的Java对象(Plain Old Java Object,POJO)实现了EJB的功能。
1.1.2 Spring的缺点分析
虽然Spring的组件代码是轻量级的,但它的配置却是重量级的。一开始,Spring用XML配置,而且是很多XML配置。Spring 2.5引入了基于注解的组件扫描,这消除了大量针对应用程序自身组件的显式XML配置。Spring 3.0引入了基于Java的配置,这是一种类型安全的可重构配置方式,可以代替XML。
所有这些配置都代表了开发时的损耗。因为在思考Spring特性配置和解决业务问题之间需要进行思维切换,所以编写配置挤占了编写应用程序逻辑的时间。和所有框架一样,Spring实用,但与此同时它要求的回报也不少。
除此之外,项目的依赖管理也是一件耗时耗力的事情。在环境搭建时,需要分析要导入哪些库的坐标,而且还需要分析导入与之有依赖关系的其他库的坐标,一旦选错了依赖的版本,随之而来的不兼容问题就会严重阻碍项目的开发进度。
1.2 SpringBoot的概述
1.2.1 SpringBoot解决上述Spring的缺点
SpringBoot对上述Spring的缺点进行的改善和优化,基于约定优于配置的思想,可以让开发人员不必在配置与逻辑业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发的效率,一定程度上缩短了项目周期。
1.2.2 SpringBoot的特点
- 为基于Spring的开发提供更快的入门体验
- 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求
- 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等
- SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式
1.2.3 SpringBoot的核心功能
-
起步依赖
起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。
简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能。
-
自动配置
Spring Boot的自动配置是一个运行时(更准确地说,是应用程序启动时)的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个。该过程是Spring自动完成的。
2. 使用IDEA快速创建一个SpringBoot项目
设置Maven镜像为阿里云,点击setting,找到maven设置
配置setting.xml文件(没有之后自动新建)
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>/Users/Eric/.m2/repository</localRepository>
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|-->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<repositories>
<repository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://maven.oschina.net/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://maven.oschina.net/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>
这样下载jar包就会快很多哦
编写SpringBoot的HelloWorld文件
首先找到启动文件,然后启动一下试试
点击绿色的三角符号启动程序吗,启动成功后的效果
在jar包中新建HelloController类
编写一下代码,并在浏览器中进行访问
package com.nuesoft.springbootdemo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Eric Lee
* @date 2020/3/17 15:43
*/
@RestController
public class HelloController {
@GetMapping("/")
public String sayHello(){
return "Hello SpringBoot!";
}
}
这样第一个SpringBoot项目就成功的构建起来啦,是不是很开心呢。
pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
spring:
profiles:
active: dev
application-dev.yml
server:
port: 8888
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/sell?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
ddl-auto: 参数说明
- create :每次运行时,都会重新创建表,所以数据会丢失
- create -drop: 每次运行程序会创建表结构, 程序结束删除表
- update :每次运行程序时,没有表会创建表,如果对象发生改变会更新表结构,原来数据不会清空,只会更新(推荐使用)
- validate 校验程序与数据库中字段是否相同, 不同会报错
- none 禁止使用DDL
实体类创建
Lombok常用的几个注解:
- @Data : 注在类上,提供类的get、set、equals、hashCode、canEqual、toString方法
- @AllArgsConstructor : 注在类上,提供类的全参构造
- @NoArgsConstructor : 注在类上,提供类的无参构造
- @Setter : 注在属性上,提供 set 方法
- @Getter : 注在属性上,提供 get 方法
- @EqualsAndHashCode : 注在类上,提供对应的 equals 和 hashCode 方法
- @Log4j/@Slf4j : 注在类上,提供对应的 Logger 对象,变量名为 log
import java.util.Date;
@Entity
@Data
@DynamicUpdate
public class ProductCategory {
@Id
@GeneratedValue
private Integer categoryId;
private String categoryName;
private Integer categoryType;
private Date createTime;
private Date updateTime;
}
数据访问层
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
}
测试
gotest
package com.neuedu.springbootdemo.repository;
import com.neuedu.springbootdemo.dataobject.ProductCategory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProductCategoryRepositoryTest {
@Autowired
ProductCategoryRepository repository;
@Test
public void list(){
List<ProductCategory> all = repository.findAll();
for (ProductCategory productCategory: all)
System.out.println(productCategory);
}
}
services
package com.neuedu.springbootdemo.services;
import com.neuedu.springbootdemo.dataobject.ProductCategory;
import java.util.List;
public interface ProductCategoryService {
public List<ProductCategory> listAll();
}
实现
package com.neuedu.springbootdemo.services.impl;
import com.neuedu.springbootdemo.dataobject.ProductCategory;
import com.neuedu.springbootdemo.repository.ProductCategoryRepository;
import com.neuedu.springbootdemo.services.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductCategoryService {
@Autowired
ProductCategoryRepository repository;
@Override
public List<ProductCategory> listAll() {
return repository.findAll();
}
}
controller
package com.neuedu.springbootdemo.controller;
import com.neuedu.springbootdemo.dataobject.ProductCategory;
import com.neuedu.springbootdemo.services.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;
@RequestMapping("/seller/category")
@Controller
public class CategoryController {
@Autowired
ProductCategoryService productCategoryService;
@RequestMapping("/list")
public ModelAndView list(Map<String, Object> map){
List<ProductCategory> categoryList = productCategoryService.listAll();
map.put("categoryList", categoryList);
return new ModelAndView("category/list", map);
}
}
页面模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://cdn.bootcss.com/bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table">
<thead>
<tr>
<th>
商品ID
</th>
<th>
商品名称
</th>
<th>
创建时间
</th>
</tr>
</thead>
<tbody>
<#list categoryList as productcategory>
<tr class="success">
<td>
${productcategory.categoryId}
</td>
<td>
${productcategory.categoryName}
</td>
<td>
${productcategory.createTime}
</td>
</tr>
</#list>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>