一、創建項目
1.File->new->project;
2.選擇“Spring Initializr”,點擊next;(jdk1.8默認即可)
3.完善項目信息,組名可不做修改,項目名可做修改;最終建的項目名為:test,src->main->java下包名會是:com->example->test;點擊next;
4.Web下勾選Spring Web Start,(網上創建springboot項目多是勾選Web選項,而較高版本的Springboot沒有此選項,勾選Spring Web Start即可,2.1.8版本是Spring Web);Template Englines勾選Thymeleaf;SQL勾選:MySQL Driver,JDBC API 和 MyBatis Framework三項;點擊next;
5.選擇項目路徑,點擊finish;打開新的窗口;
6.剛創建好的項目目錄結構
7.點擊右側的Maven,點擊設置(扳手圖標)進行項目Maven倉庫的配置;
8.(1)選擇本地Maven路徑;(2)勾選配置文件后邊的選項,然后修改為本地Maven的配置文件,它會根據配置文件直接找到本地倉庫位置;
9.配置完后,如果沒有自動導包,可以點擊左上角重新導包按鈕,或者呢個下載按鈕,選擇下載所有源文件和文檔
10.在templates文件下新建index.html頁面,作為啟動的初始頁面;
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>hello</title>
- </head>
- <body>
- 你好!初學者,我是SpringBoot的簡單啟動頁面!
- </body>
- </html>
11.在com.example.test下新建controller文件夾,在controller文件夾下建一個簡單的helloController類;(Controller類要添加@Controller注解,項目啟動時,SpringBoot會自動掃描加載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為后綴的,兩種配置方式略有差別,詳情可參考這個網址:https://blog.csdn.net/qq_29648651/article/details/78503853;在這我是用.yml后綴的文件格式。右鍵application文件選擇Refact,選擇Rename,將后綴改為yml;
- spring:
- datasource:
- name: test #數據庫名
- url: jdbc:mysql://localhost:3306/test #url
- username: root #用戶名
- password: 123456 #密碼
- driver-class-name: com.mysql.jdbc.Driver #數據庫鏈接驅動
13.運行項目啟動類TestApplication.java
可以發現上面有一個WARN警告,那是因為還沒有配置編寫MyBatis的相關文件,下面會進行詳解;
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界面;到這項目的初步搭建已經完成,下面可以下一些簡單的業務邏輯,比如從數據庫獲取信息,登錄之類的簡單功能;
15.在進行下一步編寫時,我們先來鏈接一下數據庫;點擊右側的Database,點“加號”,新建數據庫鏈接;
16.填寫數據庫相關信息,點擊Test Connection;
17.如果鏈接失敗可能是驅動的問題,點擊左上角的小扳手,進入數據庫設置界面
18.連接成功后,顯示數據庫信息,user表的基本信息也顯示了,下面就照這個來了;
19.SpringBoot項目大概分為四層:
(1)DAO層:包括XxxMapper.java(數據庫訪問接口類),XxxMapper.xml(數據庫鏈接實現);(這個命名,有人喜歡用Dao命名,有人喜歡用Mapper,看個人習慣了吧)
(2)Bean層:也叫model層,模型層,entity層,實體層,就是數據庫表的映射實體類,存放POJO對象;
(3)Service層:也叫服務層,業務層,包括XxxService.java(業務接口類),XxxServiceImpl.java(業務實現類);(可以在service文件夾下新建impl文件放業務實現類,也可以把業務實現類單獨放一個文件夾下,更清晰)
(4)Web層:就是Controller層,實現與web前端的交互。
依照上面四層,創建目錄結構如下:
20.代碼展示:
(1)在application配置文件中添加MyBatis配置:
- spring:
- datasource:
- name: test #數據庫名
- url: jdbc:mysql://localhost:3306/test #url
- username: root #用戶名
- password: 123456 #密碼
- driver-class-name: com.mysql.jdbc.Driver #數據庫鏈接驅動
- mybatis:
- mapper-locations: classpath:mapper/*.xml #配置映射文件
- type-aliases-package: com.example.test.bean #配置實體類
(2)pom.xml文件配置信息(備注:這個文件以前沒有,2019/12/9日粉絲發現的,這個里面也添加了單元測試所需的配置,記得要重新導一下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數據庫配置-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.41</version>
- <scope>runtime</scope>
- </dependency>
- <!--單元測試配置-->
- <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實體類,依據數據庫表,生成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層訪問數據庫接口文件:
- package com.example.test.mapper;
- import com.example.test.bean.UserBean;
- public interface UserMapper {
- UserBean getInfo(String name,String password);
- }
(5)DAO層訪問數據庫實現文件(需在resource包下創建mapper文件夾,然后再創建一個UserMapper.xml.在application配置文件中mybatis:mapper-locations:對應的就是該文件地址),注意<mapper>標簽的namespace屬性要填寫 訪問數據庫接口類文件路徑:
- <?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層業務接口類編寫:
- package com.example.test.service;
- import com.example.test.bean.UserBean;
- public interface UserService {
- UserBean loginIn(String name,String password);
- }
(7)Service層業務實現類編寫,注意要注解@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)項目啟動類要添加注解@MapperScan項目啟動時掃描mapper接口,否則會報錯找不到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)編寫測試類,看是否能成功 訪問數據庫,獲取數據庫信息:
- 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服務:
- 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">
- 賬號:<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>登錄失?。?/span></h1>
- </body>
- </html>
21.先運行測試類,看是否成功獲取數據庫信息:
22.同時發現一條警告信息,是數據庫連接的jar包問題:
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文件,發現配置文件中未指定數據庫連接的jar包的版本號,用version標簽引入
<version>5.1.41</version>
重新運行測試類,WARN警告消除
23.運行TestApplication.java文件,啟動項目,無任何WARN警告信息,進入瀏覽器輸入localhost:8080/login
項目到這里就算完美結束了。
項目源碼放在GitHub上,可以下載參考:https://github.com/redesperado/SpringBoot.git
有一個基于本項目添加增刪改查功能的項目,僅供參考:https://github.com/redesperado/test1.git
附一個微服務項目搭建過程,有想學的可以參考一下
IDEA基于springboot采用Dubbo+zookeeper+Redis搭建微服務項目-詳細教程:https://blog.csdn.net/baidu_39298625/article/details/108330298
大家如果在創建過程 中遇到什么問題,可以在下邊提供的鏈接中看看,這些是我在創建項目過程遇到的問題,希望可以幫到大家:
1.啟動報錯: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文件數據庫字段報紅
https://blog.csdn.net/baidu_39298625/article/details/98265845
3.項目正常啟動,訪問默認index頁面時404
https://blog.csdn.net/baidu_39298625/article/details/98501840
4. 鏈接MySQL數據庫報錯: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.中文用戶名登錄失敗,無報錯信息
https://blog.csdn.net/baidu_39298625/article/details/103494461
到此這篇關于使用IDEA搭建一個簡單的SpringBoot項目超詳細過程的文章就介紹到這了,更多相關idea搭建springboot項目內容請搜索我們以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/baidu_39298625/article/details/98102453