SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

最近公司有一個內部比賽(黑客馬拉松),報名參加了這么一個賽事,在準備參賽作品的同時,由于參賽服務器需要自己搭建且比賽產生的代碼不能外泄的,所以借著這個機會,本地先寫了個測試的demo,來把tomcat部署相關的知識從0到1重新捋一遍。就當備忘錄了。

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。

vSpring Boot概念

從最根本上來講,Spring Boot就是一些庫的集合,它能夠被任意項目的構建系統所使用。簡便起見,該框架也提供了命令行界面,它可以用來運行和測試Boot應用。框架的發布版本,包括集成的CLI(命令行界面),可以在Spring倉庫中手動下載和安裝。

  • 創建獨立的Spring應用程序
  • 嵌入的Tomcat,無需部署WAR文件
  • 簡化Maven配置
  • 自動配置Spring
  • 提供生產就緒型功能,如指標,健康檢查和外部配置
  • 絕對沒有代碼生成并且對XML也沒有配置要求

v搭建Spring Boot

1. 生成模板

可以在官網https://start.spring.io/生成spring boot的模板。如下圖

SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

然后用idea導入生成的模板,導入有疑問的可以看我另外一篇文章

SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

?

2. 創建Controller

SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

3. 運行項目

添加注解 @ComponentScan(注解詳情點這里) 然后運行

SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

在看到"Compilation completed successfully in 3s 676ms"消息之后,打開任意瀏覽器,輸入 http://localhost:8080/index 即可查看效果,如下圖

SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

?

4. 接入mybatis

MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。

在項目對象模型pom.xml中插入mybatis的配置

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.30</version></dependency>

創建數據庫以及user表

use zuche;
CREATE TABLE `users` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`username` varchar(255) NOT NULL,`age` int(10) NOT NULL,`phone` bigint NOT NULL,`email` varchar(255) NOT NULL,PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
insert into users values(1,'',23,158,'3658561548@qq.com');
insert into users values(2,'',27,136,'3658561548@126.com');
insert into users values(3,'',31,159,'3658561548@163.com');
insert into users values(4,'',35,130,'3658561548@sina.com'

分別創建三個包,分別是dao/pojo/service, 目錄如下

SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

添加User:

SpringBoot入門教程(一)詳解intellij idea搭建SpringBootSpringBoot入門教程(一)詳解intellij idea搭建SpringBoot
package com.athm.pojo;/*** Created by toutou on 2018/9/15.*/
public class User {private int id;private String username;private Integer age;private Integer phone;private String email;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getPhone() {return phone;}public void setPhone(Integer phone) {this.phone = phone;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}
View Code

添加UserMapper:

SpringBoot入門教程(一)詳解intellij idea搭建SpringBootSpringBoot入門教程(一)詳解intellij idea搭建SpringBoot
package com.athm.dao;import com.athm.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;/*** Created by toutou on 2018/9/15.*/
@Mapper
public interface UserMapper {@Select("SELECT id,username,age,phone,email FROM USERS WHERE AGE=#{age}")List<User> getUser(int age);
}
View Code

添加UserService:

SpringBoot入門教程(一)詳解intellij idea搭建SpringBootSpringBoot入門教程(一)詳解intellij idea搭建SpringBoot
package com.athm.service;import com.athm.pojo.User;import java.util.List;/*** Created by toutou on 2018/9/15.*/
public interface UserService {List<User> getUser(int age);
}
View Code

添加UserServiceImpl

SpringBoot入門教程(一)詳解intellij idea搭建SpringBootSpringBoot入門教程(一)詳解intellij idea搭建SpringBoot
package com.athm.service;import com.athm.dao.UserMapper;
import com.athm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** Created by toutou on 2018/9/15.*/
@Service
public class UserServiceImpl implements UserService{@AutowiredUserMapper userMapper;@Overridepublic List<User> getUser(int age){return userMapper.getUser(age);}
}
View Code

controller添加API方法

SpringBoot入門教程(一)詳解intellij idea搭建SpringBootSpringBoot入門教程(一)詳解intellij idea搭建SpringBoot
package com.athm.controller;import com.athm.pojo.User;
import com.athm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** Created by toutou on 2018/9/15.*/
@RestController
public class IndexController {@AutowiredUserService userService;@GetMapping("/show")public List<User> getUser(int age){return userService.getUser(age);}@RequestMapping("/index")public Map<String, String> Index(){Map map = new HashMap<String, String>();map.put("北京","北方城市");map.put("深圳","南方城市");return map;}
}
View Code

修改租車ZucheApplication

SpringBoot入門教程(一)詳解intellij idea搭建SpringBootSpringBoot入門教程(一)詳解intellij idea搭建SpringBoot
package com.athm.zuche;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(basePackages = {"com.athm.controller","com.athm.service"})
@MapperScan(basePackages = {"com.athm.dao"})
public class ZucheApplication {public static void main(String[] args) {SpringApplication.run(ZucheApplication.class, args);}
}
View Code

添加數據庫連接相關配置,application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/zuche
spring.datasource.username=toutou
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

按如下提示運行

SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

瀏覽器輸入得到效果:

SpringBoot入門教程(一)詳解intellij idea搭建SpringBoot

v博客總結

系統故障常常都是不可預測且難以避免的,因此作為系統設計師的我們,必須要提前預設各種措施,以應對隨時可能的系統風險。

v源碼地址

https://github.com/toutouge/javademo/tree/master/hellospringboot


作  者:請叫我頭頭哥
出  處:http://www.cnblogs.com/toutou/
關于作者:專注于基礎平臺的項目開發。如有問題或建議,請多多賜教!
版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
特此聲明:所有評論和私信都會在第一時間回復。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信我
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角【推薦】一下。您的鼓勵是作者堅持原創和持續寫作的最大動力!

轉載于:https://www.cnblogs.com/toutou/p/9650939.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249349.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249349.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249349.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

文藝平衡樹 Splay 學習筆記(1)

&#xff08;這里是Splay基礎操作&#xff0c;reserve什么的會在下一篇里面講&#xff09; 好久之前就說要學Splay了&#xff0c;結果茍到現在才學習。 可能是最近良心發現自己實在太弱了&#xff0c;聽數學又聽不懂只好多學點不要腦子的數據結構。 感覺Splay比Treap良心多了—…

JS使用XMLHttpRequest對象POST收發JSON格式數據

JavaScirpt中的XMLHttpRequest對象提供了對 HTTP 協議的完全訪問&#xff0c;使用該對象可以在不刷新頁面的情況與服務器交互數據。XMLHttpRequest是實現AJAX技術的關鍵對象&#xff0c;本站曾整理過一篇介紹該對象的文章&#xff1a; JS使用XMLHttpRequest對象與服務器進行數據…

ShopXO本地化部署安裝之centeros 安裝Apache2.4.6 + PHP7.0.33 + Mysql5.7.25環境

對于centerOS安裝PHP環境&#xff0c;目前網上的帖子都已經比較成熟&#xff0c;具體步驟大家可以自行搜索查看&#xff0c;但是在安裝過程中遇到的一些小細節&#xff0c;這些內容往往需要結合多個帖子才能找到答案&#xff0c;在這里簡單記錄一下。 細節一 如果使用的阿里云…

Spring Boot 擴展點應用之工廠加載機制

Spring 工廠加載機制&#xff0c;即 Spring Factories Loader&#xff0c;核心邏輯是使用 SpringFactoriesLoader 加載由用戶實現的類&#xff0c;并配置在約定好的META-INF/spring.factories 路徑下&#xff0c;該機制可以為框架上下文動態的增加擴展。 該機制類似于 Java SPI…

Vue.js使用-http請求

Vue.js使用-ajax使用 1.為什么要使用ajax 前面的例子&#xff0c;使用的是本地模擬數據&#xff0c;通過ajax請求服務器數據。 2.使用jquery的ajax庫示例 new Vue({el: #app,data: {searchQuery: ,columns: [{name: name, iskey: true}, {name: age},{name: sex, dataSource:…

跨域(Cross-Domain) AJAX for IE8 and IE9

1、有過這樣一段代碼&#xff0c;是ajax $.ajax({url: "http://127.0.0.1:9001",type: "POST",data: JSON.stringify({"reqMsg":"12345"}),dataType: json,timeout: 1000 * 30,success: function (response) {if(response.n6){dosomet…

移動WEB的頁面布局

隨著移動互聯網的日益普遍&#xff0c;現在移動版本的web應用也應用而生&#xff0c;那么在做移動web頁面布局的過程中&#xff0c;應該注意哪些要點呢&#xff1f;現把個人的一些學習經驗總結如下&#xff1a; 要點一、piexl 1px 2dp dp dpr dpi ppi 要點二、viewport io…

AnswerOpenCV(1001-1007)一周佳作欣賞

外國不過十一&#xff0c;所以利用十一假期&#xff0c;看看他們都在干什么。一、小白問題http://answers.opencv.org/question/199987/contour-single-blob-with-multiple-object/ Contour Single blob with multiple objectHi to everyone. Im developing an object shape id…

Mysql 開啟遠程連接

在日常的數據庫的使用過程&#xff0c;往往會因為連接權限的問題搞得我們焦頭爛額&#xff0c;今天我把我們在數據庫連接上的幾個誤區簡單做個記錄。內容如下&#xff1a; 誤區一&#xff1a;MYSQL密碼和數據庫密碼的區別 mysql密碼是我們在安裝mysql服務是設置的密碼&#xf…

基于jsp+servlet完成的用戶注冊

思考 &#xff1a; 需要創建實體類嗎? 需要創建表嗎 |----User 存在、不需要創建了&#xff01;表同理、也不需要了 1.設計dao接口 package cn.javabs.usermanager.dao;import cn.javabs.usermanager.entity.User;/*** 用戶的dao接口的設計* author Mryang**/ public interfa…

vue resource then

https://www.cnblogs.com/chenhuichao/p/8308993.html

云開發創建云函數

安裝wx-server-sdk時候&#xff0c;終端報錯如下&#xff1a; 解決方法&#xff1a; 運行&#xff1a;npm cache clean --force即可 轉載于:https://www.cnblogs.com/moguzi12345/p/9758842.html

Java8新特性——函數式接口

目錄 一、介紹 二、示例 &#xff08;一&#xff09;Consumer 源碼解析 測試示例 &#xff08;二&#xff09;Comparator &#xff08;三&#xff09;Predicate 三、應用 四、總結 一、介紹 FunctionalInterface是一種信息注解類型&#xff0c;用于指明接口類型聲明…

CSS3筆記之基礎篇(一)邊框

效果一、圓角效果 border-radius 實心上半圓&#xff1a; 方法&#xff1a;把高度(height)設為寬度&#xff08;width&#xff09;的一半&#xff0c;并且只設置左上角和右上角的半徑與元素的高度一致&#xff08;大于也是可以的&#xff09;。 div {height:50px;/*是width…

JavaSE之Java基礎(1)

1、為什么重寫equals還要重寫hashcode 首先equals與hashcode間的關系是這樣的&#xff1a; 1、如果兩個對象相同&#xff08;即用equals比較返回true&#xff09;&#xff0c;那么它們的hashCode值一定要相同&#xff1b; 2、如果兩個對象的hashCode相同&#xff0c;它們并不一…

bootstarp table

https://www.cnblogs.com/laowangc/p/8875526.html

高級組件——彈出式菜單JPopupMenu

彈出式菜單JPopupMenu&#xff0c;需要用到鼠標事件。MouseListener必須要實現所有接口&#xff0c;MouseAdapter是類&#xff0c;只寫你關心的方法&#xff0c;即MouseAdapter實現了MouseListener中的方法 import javax.swing.*; import java.awt.*; import java.awt.event.Mo…

CSS3筆記之基礎篇(二)顏色和漸變色彩

效果一、顏色之RGBA RGB是一種色彩標準&#xff0c;是由紅(R)、綠(G)、藍(B)的變化以及相互疊加來得到各式各樣的顏色。RGBA是在RGB的基礎上增加了控制alpha透明度的參數。 語法&#xff1a; color&#xff1a;rgba(R,G,B,A) 以上R、G、B三個參數&#xff0c;正整數值的取值…

19_03_26校內訓練[魔法卡片]

題意 有n張有序的卡片&#xff0c;每張卡片上恰有[1,m]中的每一個數&#xff0c;數字寫在正面或反面。每次詢問區間[l,r]&#xff0c;你可以將卡片上下顛倒&#xff0c;問區間中數字在卡片上方的并的平方和最大是多少。q,n*m≤1,000,000。 思考 一個很重要的性質&#xff0c;若…

vue 靜態圖片引入

https://blog.csdn.net/weixin_33862188/article/details/93325502