SpringBoot專案創建
本節文章主要介紹一下SpringBoot以及用IDEA創建一個Spring Initializr工程
文章目錄
- SpringBoot專案創建
- 前言
- 一、SpringBoot的優點
- 二、簡單的專案創建
- 1.環境準備
- 2.創建專案
- 2.代碼測驗
- 總結
前言
由于J2EE笨重的開發,繁多的配置,復雜的部署,低下的開發效率,第三方技術集成難度大等等,故而誕生出了SpringBoot,它是用來簡化Spring應用開發,約定大于配置,去繁從簡的一個產品級應用,
提示:以下是本篇文章正文內容,下面案例可供參考
一、SpringBoot的優點
- 快速創建獨立運行的Spring專案以及主流框架集成
- 使用嵌入式的Servlet,無需打成WAR包
- starters自動依賴與版本控制
- 大量的自動配置,簡化開發,也可以修改默認值
- 無需手動配置xml,無代碼生成,開箱即用
- 準生產環境的運行時應用監控
- 與云計算的天然集成
二、簡單的專案創建
1.環境準備
- jdk1.8及以上
- myql(越新越好)
- IDEA(越新越好)
2.創建專案
-
找到,選擇jdk版本,點擊next,

-
對Group,Artifact進行自行名命,選擇Java Version(對應JDK版本),我們這里jdk1.8對應8,自行定義版本號,點擊next,

-
選擇Web,勾選Spring Web,

-
再次選擇SQL,勾選MyBatis Framework,Mysql Driver,點擊next,

-
對本工程名命,選擇工程路徑,點擊finish

-
得到如下界面

-
右鍵選中application.properties,依次選擇Refactor->Rename,得到如下界面,將.properties后綴名改為.yml,點擊Refactor,

-
點擊pom.xml,查看版本資訊,將資料庫版本號修改一下,Runime修改為8.0.17(我用的是Mysql8.0.17)

-
在java檔案夾中com.testc45中創建action,bean,mapper,service四個包,搭好代碼框架,

-
配置application.yml檔案,

2.代碼測驗
- 本測驗主要是進行資料庫鏈接,將資料庫資料資訊輸出到網頁上,但是代碼有冗余部分,后面學習會相繼用到,
- 在action中創建EsportAction類,并寫如下代碼,注意每個人名命不同的包名需要修改對應的地方,
package com.testc45.action;
import com.testc45.bean.Esport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.testc45.service.EsportService;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/sport")
public class EsportAction {
@Resource
private EsportService esportService;
@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
public Map selectAll(){
Map map = new HashMap();
List<Esport> list = esportService.selectAll();
map.put("list", list);
map.put("entropy", getEntropy());
return map;
}
public Double getEntropy(){
Double entropy = 0.0;
Double num = esportService.selectNumBySql("select count(*) from esport");
Double numy = esportService.selectNumBySql("select count(*) from esport where play=1");
Double numn = esportService.selectNumBySql("select count(*) from esport where play=0");
entropy = - numy/num * Math.log(numy/num) - numn/num * Math.log(numn/num);
return entropy;
}
}
- 在bean包中創建Esport類
package com.testc45.bean;
public class Esport {
private Integer id;
private String outlook;
private String temperature;
private String humidity;
private Integer windy;
private Integer play;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOutlook() {
return outlook;
}
public void setOutlook(String outlook) {
this.outlook = outlook;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public Integer getWindy() {
return windy;
}
public void setWindy(Integer windy) {
this.windy = windy;
}
public Integer getPlay() {
return play;
}
public void setPlay(Integer play) {
this.play = play;
}
}
- 在mapper包,創建EsportMapper介面
package com.testc45.mapper;
import com.testc45.bean.Esport;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface EsportMapper {
@Select({
"select",
"id, outlook, temperature, humidity, windy, play",
"from esport"
})
List<Esport> selectAll();
@Select({
"${sql}"
})
Double selectNumBySql(@Param("sql") String sql);
}
- 在service包中創建EsportService介面
package com.testc45.service;
import com.testc45.bean.Esport;
import java.util.List;
public interface EsportService {
public List<Esport> selectAll();
public Double selectNumBySql(String sql);
}
- 在service包中創建EsportServiceImpl類
package com.testc45.service;
import com.testc45.bean.Esport;
import com.testc45.mapper.EsportMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("esportService")
public class EsportServiceImpl implements EsportService {
@Resource
private EsportMapper esportMapper;
@Override
public List<Esport> selectAll() {
return esportMapper.selectAll();
}
@Override
public Double selectNumBySql(String sql) {
return esportMapper.selectNumBySql(sql);
}
}
- 在啟動類(專案創建伊始自動生成的那個類)中添加注解映射,@ComponentScan和MapperScan的引數注意對照自己創建的包的名稱,

package com.testc45.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.testc45")
@MapperScan("com.testc45.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
-
點擊運行,

-
打開瀏覽器,輸入http://localhost:8090/sport/selectAll,

總結
- 僅對步驟進行了介紹,還沒用深入代碼詳解,后面會陸續介紹的,
- 代碼匯入進去后,如果有標紅先不要慌,首先把包名修改成自己所對應的,然后剩下報錯應該是Maven依賴項沒有匯入,把滑鼠移動至標紅處,一般左下角有一鍵解決辦法,
- 如果pom.xml檔案標紅,是所下載的組態檔有問題或者損壞,可以進行修改settings檔案,詳細見鏈接: 標紅問題解決方法,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/208907.html
標籤:其他
上一篇:Java中的影像銳化操作
