idea創(chuàng)建SpringBoot項(xiàng)目及注解配置相關(guān)應(yīng)用小結(jié)
SpringBoot:
約定大于配置 springboot == ss springboot的版本之間差異較大
一、簡言
1、Spring Boot是Spring社區(qū)發(fā)布的一個(gè)開源項(xiàng)目,旨在幫助開發(fā)者快速并且更簡單的構(gòu)建項(xiàng)目。
2、使用Spring Boot很容易創(chuàng)建一個(gè)獨(dú)立運(yùn)行的項(xiàng)目:脫離自己配置服務(wù)器,內(nèi)部服務(wù)器運(yùn)行
3、使用Spring Boot創(chuàng)建準(zhǔn)生產(chǎn)級(jí)別的基于Spring框架的項(xiàng)目:企業(yè)級(jí)發(fā)布型項(xiàng)目
4、使用SpringBoot可以不用或者只需要很少的配置文件
二、SpringBoot作用
Spring Boot框架,其功能非常簡單,便是幫助我們實(shí)現(xiàn)自動(dòng)配置。我們都知道Spring Boot框架的核心是自動(dòng)配置。只要有相應(yīng)的jar包,Spring就會(huì)幫助我們實(shí)現(xiàn)自動(dòng)配置,而無需像以前我們使用spring框架一樣要做很多配置。當(dāng)默認(rèn)配置不能滿足我們要求的時(shí)候,我們能夠用自己的配置來替換這些自動(dòng)的配置類。此外,上面我們也提到Spring Boot內(nèi)嵌了web應(yīng)用容器,除此之外還集成了系統(tǒng)監(jiān)控等功能,這些都可以幫助我們快速搭建企業(yè)級(jí)的應(yīng)用程序并使用。
三、核心功能
1、可以不依賴tomcat等外部容器來獨(dú)立運(yùn)行的web項(xiàng)目,springboot的優(yōu)點(diǎn)是能夠以jar包的形式運(yùn)行。
2、嵌入式的Servlet容器:我們不需要像以前那邊先打個(gè)war包,然后再運(yùn)行,在springboot看來這些都是多余的,我們可以選擇他內(nèi)嵌的tomcat、Jetty或者Undertow等容器來直接運(yùn)行。
3、使pom文件配置更簡化:我們只需要在pom文件中添加starter-web依賴即可,無需像以前一樣引入很多依賴而造成容易漏掉(自動(dòng)配置bean)。
4、能夠生產(chǎn)環(huán)境中直接使用性能指標(biāo)、健康檢查和應(yīng)用信息等。
5、springboot不需要任何xml文件配置而能實(shí)現(xiàn)所有的spring配置
四、創(chuàng)建springBoot項(xiàng)目
1、新建SpringBoot(maven基礎(chǔ)下)
創(chuàng)建SpringBoot有兩種方式:
- 一種是從
Service URL:start.aliyun.com創(chuàng)建- 從中央倉庫進(jìn)行下載,下載速度慢
- 另一種是從:
Service URL:https://start.spring.io/- 創(chuàng)建 阿里云鏡像倉庫
服務(wù)地址改變:將網(wǎng)址復(fù)制:

因?yàn)閕dea版本不同創(chuàng)建方式顯示頁面會(huì)有不同,但方法都一樣,例如2018 IDEA:

2022版 idea 正常創(chuàng)建:

2、next—選版本導(dǎo)jar包

建完maven項(xiàng)目一般倉庫還會(huì)是在C盤,記得Settings–maven–把庫改為自己的(懂得都懂~~)
3、創(chuàng)建后的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.1.17.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Jules</groupId>
<artifactId>springBoot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot01</name>
<description>springBoot01</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.1.17.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!-- spring-boot-starter-web會(huì)自動(dòng)幫我們引入開發(fā)過程中所需要的web模塊jar包,
如包含了spring-web和spring-webmvc的依賴,好處就是我們不需要逐個(gè)的去導(dǎo)入,
只需要導(dǎo)入一個(gè)就可以,這樣可以避免遺漏或者版本問題。 -->
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 自己的插件配置,我自己的插件生成的,省略了,不重要 -->
<build>
<plugins>
<plugin>
…………
</plugin>
…………
</plugins>
</build>
</project>這段最重要:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.17.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>此項(xiàng)目其他會(huì)使用的jar包
<!--mysql驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!--數(shù)據(jù)庫連接-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--實(shí)體類注解包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mybatis的包支持sqpeingboot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>4、SpringBoot啟動(dòng)類
在src—main—java中會(huì)生成下面的核心啟動(dòng)類,直接運(yùn)行就會(huì)開啟服務(wù)器
@SpringBootApplication /*核心注解*/
public class SpringBoot01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01Application.class, args);
}
}
==============若是不想用他的服務(wù)器可以配置自己的,后面(九)=============
?????
【核心注解@SpringBootApplication】
- @SpringBootApplication注解是多個(gè)注解的合體,其中最重要的=======
- @SpringBootConfiguration是spring注解(啟動(dòng)tomcat時(shí)加載當(dāng)前類),
- @EnableAutoConfiguration(實(shí)現(xiàn)自動(dòng)裝配),
- @ComponentScan(掃描service,controller層)
- @SpringBootConfiguration 繼承至@Configuration,其實(shí)兩種功能一致,都是標(biāo)注該類為配置類,
- 讀取啟動(dòng)類
- @EnableAutoConfiguration 這個(gè)注解是SpirngBoot自動(dòng)配置的核心所在,通過此注解,能所有符合自
- 動(dòng)配置條件的bean的定義加載到spring容器中。
- @ComponentScan 該注解會(huì)掃描當(dāng)前包及子包下面被納入sping容器管理的類,相當(dāng)于 <context:component-scan base-package=“com.hz.service”/>。
六、測(cè)試項(xiàng)目
【先配置好第七步的application.yml中的配置】

1.項(xiàng)目構(gòu)建:
實(shí)體類省略……………后端代碼:
//dao層接口、service層接口
//@Repository //dao接口注解,如果不要在service的實(shí)現(xiàn)層會(huì)有一個(gè)報(bào)紅,但不會(huì)影響項(xiàng)目執(zhí)行
public interface ProviderMapper {
/**
*
* @param proName
* @param pyl 偏移量
* @param limit 頁面容量
* @return
*/
public List<Provider> providerInfoList(
@Param("proName") String proName,
@Param("pyl") Integer pyl,
@Param("limit") Integer limit
);
}<?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="com.jules.springboot01.mapper.ProviderMapper">
<select id="providerInfoList" resultType="com.jules.springboot01.pojo.Provider">
select * from smbms_provider
<where>
<if test="proName != null and proName != ''">
and proName like concat('%',#{proName},'%')
</if>
</where>
order by id desc limit #{pyl},#{limit}
</select>
</mapper>//service層實(shí)現(xiàn)
@Service
public class ProviderServiceImpl implements ProviderService {
@Autowired
private ProviderMapper providerMapper; //前面說的報(bào)紅就是這里,不影響
public List<Provider> providerInfoList(String proName,Integer page, Integer limit) {
//計(jì)算偏移量
int pyl = (page-1)*limit;
return providerMapper.providerInfoList(proName,pyl,limit);
}
}//controller層
@Controller
@ResponseBody
@RequestMapping("/xxx")
public class ProviderController {
@Autowired
private ProviderService providerService;
@RequestMapping(value = "/providerInfoList",method = RequestMethod.GET)
@ResponseBody
public List<Provider> providerInfoList(String proName,Integer page, Integer limit){
System.out.println("proName=="+proName);
List<Provider> providers = providerService.providerInfoList(proName,1,10);
return providers;
}
}2.數(shù)據(jù)源注入dao層—@MapperScan
【到此speingBoot構(gòu)建好了,項(xiàng)目創(chuàng)建完成,雖然數(shù)據(jù)庫連接與創(chuàng)建數(shù)據(jù)源已完成,但是數(shù)據(jù)源注入dao層還沒有完成】
@MapperScan
- 作用:指定要變成實(shí)現(xiàn)類的接口所在的包,然后包下面的所有接口在編譯之后都會(huì)生成相應(yīng)的實(shí)現(xiàn)類
- 添加位置:是在Springboot啟動(dòng)類上面添加
@SpringBootApplication
@MapperScan("com.jules.springboot01.mapper")
public class SpringBoot01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01Application.class, args);
}
}【啟動(dòng),運(yùn)行路徑:http://localhost:8080/xxx/providerInfoLis拿到數(shù)據(jù)庫的數(shù)據(jù)】

七、關(guān)于application.properties文件
application.properties格式文件
#配置相關(guān)配置,格式如下 #項(xiàng)目名 spring.application.name: springBoot01 #端口號(hào) service.port: 8080
application.properties可以將后綴名改為.yml文件,application.yml格式如下(冒號(hào)后面空格一下;項(xiàng)目用此格式):
#數(shù)據(jù)庫連接(導(dǎo)jar包)
spring:
datasource:
url: jdbc:mysql://localhost:3306/數(shù)據(jù)庫名稱?useUnicode=true&characterEncoding=utf8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: xxxx
password: xxxx
# 端口號(hào)
server:
port: 8080
namevo: 朱爾斯
# mybatis相關(guān)配置
mybatis:
mapper-locations: classpath:mapper/*.xml #掃描mapper(dao層)
type-aliases-package: com.jules.springboot01.pojo #別名
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #SQL控制臺(tái)打印【有關(guān)靜態(tài)資源配置:】

不配置靜態(tài)資源,直接訪問index.html,報(bào)錯(cuò):

配置靜態(tài)資源就可以正常進(jìn)入index.html;也可以本地磁盤
spring: resources: static-locations: classpath:/templates,classpath:/static/,file:D://
八、注解
@Configuration
標(biāo)注一個(gè)類為配置類:可以自己創(chuàng)建配置文件類,加上@Configuration則表示是配置文件,項(xiàng)目啟動(dòng)會(huì)先加載此類。
@Bean
用@Bean標(biāo)注方法等價(jià)于XML中配置的bean
@ConfigurationProperties + @Value
讀取properties或yml文件+讀取里面的值
示例:
@Configuration //容器啟動(dòng)時(shí)加載
//@ConfigurationProperties("application.yml")
public class AppConfig {
@Value("${namevo}")
private String namevo;
//創(chuàng)建bean實(shí)例 別名為stu
@Bean(name = "stu")
public Student getStudent(){
Student stu = new Student();
stu.setStudentName("Jules");
return stu;
}
}@ComponentScan
- 此注解已經(jīng)存在@SpringBootApplication中,自動(dòng)掃描當(dāng)前包以及子包service,controller層
- 如果掃描非當(dāng)前包,則需要在添加@ComponentScan()去掃描
@ComponentScan("com.jules.springBoot")多包逗號(hào)隔開

或者使用@SpringBootApplication注解的scanBasePackages屬性進(jìn)行配置@SpringBootApplication(scanBasePackages ={"com.jules.service","com.jules.controller"})多包使用數(shù)組形式
@EnableTransactionManagement
- 開啟事務(wù)注解:在總局上開啟則在啟動(dòng)類上使用@EnableTransactionManagement
- 具體使用事務(wù):用
@Transactional注解,例如service層,誰用則在方法上@Transactional
九、配置外部容器:
1.去掉pom.xml中默認(rèn)加載的tomcat,并添加servlet-api
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--移除tomcat--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
2.覆蓋啟動(dòng)類
//使啟動(dòng)類繼承 SpringBootServletInitializer 類,并覆蓋 configure 方法
@SpringBootApplication
public class SpringbootCSApplication extends SpringBootServletInitializer {
//覆蓋configure方法
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootCS.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootCS.class, args);
}
}3.加入自己的服務(wù)器
跟以前的加入與啟動(dòng)服務(wù)器是一樣的

十、SpringBoot中application.xxx配置文件的加載順序
問:在正常項(xiàng)目下有一個(gè)application.yml文件和application.properties文件,先加載哪個(gè)文件?
答:先加載后綴為.properties的,如果.yml中的有相同的配置,則不會(huì)在加載.yml配置。按優(yōu)先級(jí)排序,位置高的將覆蓋位置低的(properties位置高)。
比如:
# application.properties文件 namevo: 朱爾斯
# application.yml文件 namevo: Jules
執(zhí)行輸出的會(huì)是:“朱爾斯”
如果在不同的目錄中存在多個(gè)配置文件,它的讀取順序是:
1、項(xiàng)目根目錄下的config文件中的:
- config/application.properties
- config/application.yml
2、項(xiàng)目根目錄下的:
- application.properties
- application.yml
3、項(xiàng)目resources目錄下config目錄中的:
- resources/config/application.properties
- resources/config/application.yml
4、項(xiàng)目的resources目錄下(項(xiàng)目創(chuàng)建自動(dòng)生成的):
- resources/application.properties(項(xiàng)目的resources目錄下)
- resources/application.yml
到此這篇關(guān)于idea創(chuàng)建SpringBoot項(xiàng)目及注解配置相關(guān)應(yīng)用的文章就介紹到這了,更多相關(guān)idea創(chuàng)建SpringBoot項(xiàng)目內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Kafka實(shí)現(xiàn)優(yōu)先級(jí)隊(duì)列的示例詳解
在分布式系統(tǒng)中,消息隊(duì)列是一種常見的異步通信機(jī)制,而優(yōu)先級(jí)隊(duì)列則是消息隊(duì)列的一種特殊形式,下面我們來看看如何利用Kafka實(shí)現(xiàn)優(yōu)先級(jí)隊(duì)列吧2025-03-03
Java實(shí)現(xiàn)天天酷跑小游戲完整代碼(附源碼)
這篇文章主要介紹了使用Java實(shí)現(xiàn)天天酷跑(附源碼),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
徹底解決java.lang.ClassNotFoundException: com.mysql.jdbc.Dr
這篇文章給大家介紹了如如何徹底解決java.lang.ClassNotFoundException: com.mysql.jdbc.Driver問題,文中有詳細(xì)的解決思路以及解決方法,需要的朋友可以參考下2023-11-11

