最近公司有一個內部比賽(黑客馬拉松),報名參加了這麼一個賽事,在準備參賽做品的同時,因爲參賽服務器須要本身搭建且比賽產生的代碼不能外泄的,因此藉着這個機會,本地先寫了個測試的demo,來把tomcat部署相關的知識從0到1從新捋一遍。就當備忘錄了。html
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員再也不須要定義樣板化的配置。經過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成爲領導者。java
從最根本上來說,Spring Boot就是一些庫的集合,它可以被任意項目的構建系統所使用。簡便起見,該框架也提供了命令行界面,它能夠用來運行和測試Boot應用。框架的發佈版本,包括集成的CLI(命令行界面),能夠在Spring倉庫中手動下載和安裝。mysql
能夠在官網https://start.spring.io/生成spring boot的模板。以下圖git
而後用idea導入生成的模板,導入有疑問的能夠看我另一篇文章github
添加註解 @ComponentScan
(註解詳情點這裏) 而後運行web
在看到"Compilation completed successfully in 3s 676ms"消息以後,打開任意瀏覽器,輸入 http://localhost:8080/index 便可查看效果,以下圖spring
MyBatis 是一款優秀的持久層框架,它支持定製化 SQL、存儲過程以及高級映射。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表apache
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, 目錄以下
添加User:
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; } }
添加UserMapper:
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); }
添加UserService:
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); }
添加UserServiceImpl
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{ @Autowired UserMapper userMapper; @Override public List<User> getUser(int age){ return userMapper.getUser(age); } }
controller添加API方法
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 { @Autowired UserService 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; } }
修改租車ZucheApplication
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); } }
添加數據庫鏈接相關配置,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
按以下提示運行
瀏覽器輸入獲得效果:
系統故障經常都是不可預測且難以免的,所以做爲系統設計師的咱們,必需要提早預設各類措施,以應對隨時可能的系統風險。
https://github.com/toutouge/javademosecond/tree/master/hellospringboot
做 者:請叫我頭頭哥
出 處:http://www.cnblogs.com/toutou/
關於做者:專一於基礎平臺的項目開發。若有問題或建議,請多多賜教!
版權聲明:本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文連接。
特此聲明:全部評論和私信都會在第一時間回覆。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信我
聲援博主:若是您以爲文章對您有幫助,能夠點擊文章右下角【推薦】一下。您的鼓勵是做者堅持原創和持續寫做的最大動力!