使用IDEA搭建一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目超詳細(xì)過(guò)程
一、創(chuàng)建項(xiàng)目
1.File->new->project;

2.選擇“Spring Initializr”,點(diǎn)擊next;(jdk1.8默認(rèn)即可)

3.完善項(xiàng)目信息,組名可不做修改,項(xiàng)目名可做修改;最終建的項(xiàng)目名為:test,src->main->java下包名會(huì)是:com->example->test;點(diǎn)擊next;

4.Web下勾選Spring Web Start,(網(wǎng)上創(chuàng)建springboot項(xiàng)目多是勾選Web選項(xiàng),而較高版本的Springboot沒(méi)有此選項(xiàng),勾選Spring Web Start即可,2.1.8版本是Spring Web);Template Englines勾選Thymeleaf;SQL勾選:MySQL Driver,JDBC API 和 MyBatis Framework三項(xiàng);點(diǎn)擊next;

5.選擇項(xiàng)目路徑,點(diǎn)擊finish;打開新的窗口;


6.剛創(chuàng)建好的項(xiàng)目目錄結(jié)構(gòu)

7.點(diǎn)擊右側(cè)的Maven,點(diǎn)擊設(shè)置(扳手圖標(biāo))進(jìn)行項(xiàng)目Maven倉(cāng)庫(kù)的配置;

8.(1)選擇本地Maven路徑;(2)勾選配置文件后邊的選項(xiàng),然后修改為本地Maven的配置文件,它會(huì)根據(jù)配置文件直接找到本地倉(cāng)庫(kù)位置;

9.配置完后,如果沒(méi)有自動(dòng)導(dǎo)包,可以點(diǎn)擊左上角重新導(dǎo)包按鈕,或者呢個(gè)下載按鈕,選擇下載所有源文件和文檔

10.在templates文件下新建index.html頁(yè)面,作為啟動(dòng)的初始頁(yè)面;

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hello</title> </head> <body> 你好!初學(xué)者,我是SpringBoot的簡(jiǎn)單啟動(dòng)頁(yè)面! </body> </html>
11.在com.example.test下新建controller文件夾,在controller文件夾下建一個(gè)簡(jiǎn)單的helloController類;(Controller類要添加@Controller注解,項(xiàng)目啟動(dòng)時(shí),SpringBoot會(huì)自動(dòng)掃描加載Controller)


package com.example.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/index")
public String sayHello(){
return "index";
}
}
12.在resources文件夾下application中先配置DataSource基本信息,application文件有兩種文件格式,一種是以.properties為后綴,一種是以.yml為后綴的,兩種配置方式略有差別,詳情可參考這個(gè)網(wǎng)址:https://blog.csdn.net/qq_29648651/article/details/78503853;在這我是用.yml后綴的文件格式。右鍵application文件選擇Refact,選擇Rename,將后綴改為yml;

spring: datasource: name: test #數(shù)據(jù)庫(kù)名 url: jdbc:mysql://localhost:3306/test #url username: root #用戶名 password: 123456 #密碼 driver-class-name: com.mysql.jdbc.Driver #數(shù)據(jù)庫(kù)鏈接驅(qū)動(dòng)
13.運(yùn)行項(xiàng)目啟動(dòng)類TestApplication.java

可以發(fā)現(xiàn)上面有一個(gè)WARN警告,那是因?yàn)檫€沒(méi)有配置編寫MyBatis的相關(guān)文件,下面會(huì)進(jìn)行詳解;
2019-08-02 09:14:27.473 WARN 9120 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.example.test]' package. Please check your configuration.
14.在瀏覽器中輸入localhost:8080,回車顯示初始的index界面;到這項(xiàng)目的初步搭建已經(jīng)完成,下面可以下一些簡(jiǎn)單的業(yè)務(wù)邏輯,比如從數(shù)據(jù)庫(kù)獲取信息,登錄之類的簡(jiǎn)單功能;

15.在進(jìn)行下一步編寫時(shí),我們先來(lái)鏈接一下數(shù)據(jù)庫(kù);點(diǎn)擊右側(cè)的Database,點(diǎn)“加號(hào)”,新建數(shù)據(jù)庫(kù)鏈接;


16.填寫數(shù)據(jù)庫(kù)相關(guān)信息,點(diǎn)擊Test Connection;

17.如果鏈接失敗可能是驅(qū)動(dòng)的問(wèn)題,點(diǎn)擊左上角的小扳手,進(jìn)入數(shù)據(jù)庫(kù)設(shè)置界面

18.連接成功后,顯示數(shù)據(jù)庫(kù)信息,user表的基本信息也顯示了,下面就照這個(gè)來(lái)了;

19.SpringBoot項(xiàng)目大概分為四層:
(1)DAO層:包括XxxMapper.java(數(shù)據(jù)庫(kù)訪問(wèn)接口類),XxxMapper.xml(數(shù)據(jù)庫(kù)鏈接實(shí)現(xiàn));(這個(gè)命名,有人喜歡用Dao命名,有人喜歡用Mapper,看個(gè)人習(xí)慣了吧)
(2)Bean層:也叫model層,模型層,entity層,實(shí)體層,就是數(shù)據(jù)庫(kù)表的映射實(shí)體類,存放POJO對(duì)象;
(3)Service層:也叫服務(wù)層,業(yè)務(wù)層,包括XxxService.java(業(yè)務(wù)接口類),XxxServiceImpl.java(業(yè)務(wù)實(shí)現(xiàn)類);(可以在service文件夾下新建impl文件放業(yè)務(wù)實(shí)現(xiàn)類,也可以把業(yè)務(wù)實(shí)現(xiàn)類單獨(dú)放一個(gè)文件夾下,更清晰)
(4)Web層:就是Controller層,實(shí)現(xiàn)與web前端的交互。
依照上面四層,創(chuàng)建目錄結(jié)構(gòu)如下:

20.代碼展示:
(1)在application配置文件中添加MyBatis配置:
spring: datasource: name: test #數(shù)據(jù)庫(kù)名 url: jdbc:mysql://localhost:3306/test #url username: root #用戶名 password: 123456 #密碼 driver-class-name: com.mysql.jdbc.Driver #數(shù)據(jù)庫(kù)鏈接驅(qū)動(dòng) mybatis: mapper-locations: classpath:mapper/*.xml #配置映射文件 type-aliases-package: com.example.test.bean #配置實(shí)體類
(2)pom.xml文件配置信息(備注:這個(gè)文件以前沒(méi)有,2019/12/9日粉絲發(fā)現(xiàn)的,這個(gè)里面也添加了單元測(cè)試所需的配置,記得要重新導(dǎo)一下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 http://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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</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-jdbc</artifactId>
</dependency>
<!--thymeleaf模板引擎配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--Web依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MyBatis配置-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--MySQL數(shù)據(jù)庫(kù)配置-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<!--單元測(cè)試配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3)Bean實(shí)體類,依據(jù)數(shù)據(jù)庫(kù)表,生成set和get方法;
package com.example.test.bean;
public class UserBean {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
(4)DAO層訪問(wèn)數(shù)據(jù)庫(kù)接口文件:
package com.example.test.mapper;
import com.example.test.bean.UserBean;
public interface UserMapper {
UserBean getInfo(String name,String password);
}
(5)DAO層訪問(wèn)數(shù)據(jù)庫(kù)實(shí)現(xiàn)文件(需在resource包下創(chuàng)建mapper文件夾,然后再創(chuàng)建一個(gè)UserMapper.xml.在application配置文件中mybatis:mapper-locations:對(duì)應(yīng)的就是該文件地址),注意<mapper>標(biāo)簽的namespace屬性要填寫 訪問(wèn)數(shù)據(jù)庫(kù)接口類文件路徑:
<?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.example.test.mapper.UserMapper">
<select id="getInfo" parameterType="String" resultType="com.example.test.bean.UserBean">
SELECT * FROM user WHERE name = #{name} AND password = #{password}
</select>
</mapper>
(6)Service層業(yè)務(wù)接口類編寫:
package com.example.test.service;
import com.example.test.bean.UserBean;
public interface UserService {
UserBean loginIn(String name,String password);
}
(7)Service層業(yè)務(wù)實(shí)現(xiàn)類編寫,注意要注解@Service,注入DAO:
package com.example.test.serviceImpl;
import com.example.test.bean.UserBean;
import com.example.test.mapper.UserMapper;
import com.example.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
//將DAO注入Service層
@Autowired
private UserMapper userMapper;
@Override
public UserBean loginIn(String name, String password) {
return userMapper.getInfo(name,password);
}
}
(8)項(xiàng)目啟動(dòng)類要添加注解@MapperScan項(xiàng)目啟動(dòng)時(shí)掃描mapper接口,否則會(huì)報(bào)錯(cuò)找不到mapper文件:
package com.example.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.test.mapper")
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
(9)編寫測(cè)試類,看是否能成功 訪問(wèn)數(shù)據(jù)庫(kù),獲取數(shù)據(jù)庫(kù)信息:
package com.example.test;
import com.example.test.bean.UserBean;
import com.example.test.service.UserService;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads() {
UserBean userBean = userService.loginIn("a","a");
System.out.println("該用戶ID為:");
System.out.println(userBean.getId());
}
}
(10) controller層,注意添加@controller注解,注入Service服務(wù):
package com.example.test.controller;
import com.example.test.bean.UserBean;
import com.example.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
//將Service注入Web層
@Autowired
UserService userService;
@RequestMapping("/login")
public String show(){
return "login";
}
@RequestMapping(value = "/loginIn",method = RequestMethod.POST)
public String login(String name,String password){
UserBean userBean = userService.loginIn(name,password);
if(userBean!=null){
return "success";
}else {
return "error";
}
}
}
(11)html文件:
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form role="form" action = "/loginIn" method="post">
賬號(hào):<input type="text" id="name" name = "name"> <br>
密碼:<input type="password" id = "password" name = "password"> <br>
<input type="submit" id = "login" value = "login">
</form>
</body>
</html>
success.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>success</title> </head> <body> <h1>登錄成功!</h1> </body> </html>
error.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>error</title> </head> <body> <h1>登錄失??!</h1> </body> </html>
21.先運(yùn)行測(cè)試類,看是否成功獲取數(shù)據(jù)庫(kù)信息:

22.同時(shí)發(fā)現(xiàn)一條警告信息,是數(shù)據(jù)庫(kù)連接的jar包問(wèn)題:
2019-08-02 11:25:04.150 WARN 16868 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
打開pom.xml文件,發(fā)現(xiàn)配置文件中未指定數(shù)據(jù)庫(kù)連接的jar包的版本號(hào),用version標(biāo)簽引入
<version>5.1.41</version>


重新運(yùn)行測(cè)試類,WARN警告消除

23.運(yùn)行TestApplication.java文件,啟動(dòng)項(xiàng)目,無(wú)任何WARN警告信息,進(jìn)入瀏覽器輸入localhost:8080/login



項(xiàng)目到這里就算完美結(jié)束了。
項(xiàng)目源碼放在GitHub上,可以下載參考:https://github.com/redesperado/SpringBoot.git
有一個(gè)基于本項(xiàng)目添加增刪改查功能的項(xiàng)目,僅供參考:https://github.com/redesperado/test1.git
附一個(gè)微服務(wù)項(xiàng)目搭建過(guò)程,有想學(xué)的可以參考一下
IDEA基于springboot采用Dubbo+zookeeper+Redis搭建微服務(wù)項(xiàng)目-詳細(xì)教程:https://blog.csdn.net/baidu_39298625/article/details/108330298
大家如果在創(chuàng)建過(guò)程 中遇到什么問(wèn)題,可以在下邊提供的鏈接中看看,這些是我在創(chuàng)建項(xiàng)目過(guò)程遇到的問(wèn)題,希望可以幫到大家:
1.啟動(dòng)報(bào)錯(cuò):Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
https://blog.csdn.net/baidu_39298625/article/details/98261102
2.mapper.xml文件數(shù)據(jù)庫(kù)字段報(bào)紅
https://blog.csdn.net/baidu_39298625/article/details/98265845
3.項(xiàng)目正常啟動(dòng),訪問(wèn)默認(rèn)index頁(yè)面時(shí)404
https://blog.csdn.net/baidu_39298625/article/details/98501840
4. 鏈接MySQL數(shù)據(jù)庫(kù)報(bào)錯(cuò):java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
https://blog.csdn.net/baidu_39298625/article/details/100915264
5.中文用戶名登錄失敗,無(wú)報(bào)錯(cuò)信息
https://blog.csdn.net/baidu_39298625/article/details/103494461
到此這篇關(guān)于使用IDEA搭建一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目超詳細(xì)過(guò)程的文章就介紹到這了,更多相關(guān)idea搭建springboot項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決idea不支持SpringBoot yml文件的圖文教程
- idea中導(dǎo)入別人的springboot項(xiàng)目的方法(圖文)
- 解決Idea啟動(dòng)Spring Boot很慢的問(wèn)題
- SpringBoot 在IDEA中實(shí)現(xiàn)熱部署步驟詳解(實(shí)用版)
- Idea如何導(dǎo)入一個(gè)SpringBoot項(xiàng)目的方法(圖文教程)
- intellij IDEA配置springboot的圖文教程
- Idea安裝及涉及springboot詳細(xì)配置的圖文教程
- 從零開始使用IDEA創(chuàng)建SpringBoot項(xiàng)目(圖文)
- 在IDEA中創(chuàng)建跑得起來(lái)的Springboot項(xiàng)目
- idea+spring boot創(chuàng)建項(xiàng)目的搭建全過(guò)程
相關(guān)文章
Java 8中map()和flatMap()方法區(qū)別詳解
這篇文章主要為大家介紹了Java 8中map()和flatMap()方法區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
maven如何利用springboot的配置文件進(jìn)行多個(gè)環(huán)境的打包
這篇文章主要介紹了maven如何利用springboot的配置文件進(jìn)行多個(gè)環(huán)境的打包,在Spring Boot中多環(huán)境配置文件名需要滿足application-{profiles.active}.properties的格式,其中{profiles.active}對(duì)應(yīng)你的環(huán)境標(biāo)識(shí),本文給大家詳細(xì)講解,需要的朋友可以參考下2023-02-02
淺析Java中對(duì)稱與非對(duì)稱加密算法原理與使用
密碼學(xué)是研究編制密碼和破譯密碼的技術(shù)科學(xué)。這篇文章主要為大家介紹了Java中對(duì)稱與非對(duì)稱加密算法的原理與使用,感興趣的小伙伴可以了解一下2023-03-03
如何從eureka獲取服務(wù)的ip和端口號(hào)進(jìn)行Http的調(diào)用
這篇文章主要介紹了如何從eureka獲取服務(wù)的ip和端口號(hào)進(jìn)行Http的調(diào)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java9新特性Java.util.Optional優(yōu)化與增強(qiáng)解析
這篇文章主要為大家介紹了Java9新特性Java.util.Optional優(yōu)化與增強(qiáng)使用說(shuō)明解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
mybatis-plus 處理大數(shù)據(jù)插入太慢的解決
這篇文章主要介紹了mybatis-plus 處理大數(shù)據(jù)插入太慢的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
java中g(shù)et()方法和set()方法的作用淺析
這篇文章主要給大家介紹了關(guān)于java中g(shù)et()方法和set()方法的作用,set是是對(duì)數(shù)據(jù)進(jìn)行設(shè)置,而get是對(duì)數(shù)據(jù)進(jìn)行獲取,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

