第一次创建springboot框架项目
生活随笔
收集整理的這篇文章主要介紹了
第一次创建springboot框架项目
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
第一次創(chuàng)建springboot框架項目
- 1.1_創(chuàng)建步驟
- 2.1_啟動時遇到的問題
- 2.2_啟動響應網(wǎng)頁測試
- 2.3_連接數(shù)據(jù)庫嘗試
1.1_創(chuàng)建步驟
(1)創(chuàng)建spring項目
(2)
(3)
加入引擎
下一步即可
2.1_啟動時遇到的問題
(1)剛開始沒有啟動圖標,等一會就好了
(2)后來啟動失敗并報錯ERROR 3704 — [ main] o.s.b.d.LoggingFailureAnalysisReporter :
因為默認端口是8080,若已被占用需要更改默認端口號
修改方法:修改application.properties文件,在文件中添加:
啟動成功:
(3)接下來訪問http://localhost:8081/ 成功,但是后來過了一天又不行了發(fā)現(xiàn)是目錄的問題
應該訪問http://localhost:8081/demo
2.2_啟動響應網(wǎng)頁測試
package com.example.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController//響應服務器,與@RequestMapping配合使用(“@RestController配合@RequestMapping”與“@Controller配合@ResponseBody再配合@RequestMapping”效果一樣) @SpringBootApplication//聲明該類是一個springboot引導類, public class DemoApplication {public static void main(String[] args) {//run方法表示運行springboot的引導類SpringApplication.run(DemoApplication.class, args);}@RequestMappingpublic String hello() {return "hello spring boot!";} }在瀏覽器搜索:“http://localhost:8081/”
2.3_連接數(shù)據(jù)庫嘗試
package com.example.demo; import java.sql.*;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController//響應服務器,與@RequestMapping配合使用(“@RestController配合@RequestMapping”與“@Controller配合@ResponseBody再配合@RequestMapping”效果一樣) @SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@RequestMappingpublic String hello()throws Exception {SqlOperation.main();ResultSet resultSet = SqlOperation.statement.executeQuery("select * from Score");String s;resultSet.next();s = resultSet.getString("name");return s;} } class SqlOperation {public static Connection connection = null;//定義連接數(shù)據(jù)庫的對象(橋梁)public static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Studentinfo";public static Statement statement = null;//定義靜態(tài)操作對象public static PreparedStatement preparedStatement = null;//定義動態(tài)操作對象public static void main() {try{//第一步加載驅(qū)動Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");System.out.println("加載驅(qū)動成功!");//第二步建立連接connection = DriverManager.getConnection(url,"sa","shejiashuai");System.out.println("連接數(shù)據(jù)庫成功!");//第三步建立操作對象statement = connection.createStatement();}catch (Exception e){e.printStackTrace();System.out.println("連接數(shù)據(jù)庫失敗!");}}public static void close(){//關(guān)閉連接try{statement.close();connection.close();}catch (Exception e){e.printStackTrace();}} }總結(jié)
以上是生活随笔為你收集整理的第一次创建springboot框架项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 九、PyQt5 QLineEdit输入的
- 下一篇: YOLOv3实现鱼类目标检测