Spring Boot入門,springboot入門
最近在公司太清閑了,為了打發我過剩的精力,我逛起了上關注的幾個關於java技術的公眾號,有一篇文章上《Java工程師技能表》,上邊的技能排名挺有意思的:
其他技能即使在公司項目中沒用過,但是咱最少自學過,有點瞭解。但是Spring Boot竟然排名這麼高,那我今天就來入入門。
什麼是Spring Boot?特點是什嗎?
Spring Boot是由Pivotal團隊提供的全新架構,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該架構使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。
特點:
1. 建立獨立的Spring應用程式 2. 嵌入的Tomcat,無需部署WAR檔案 3. 簡化Maven配置 4. 自動設定Spring 5. 提供生產就緒型功能,如指標,健全狀態檢查和外部配置 6. 絕對沒有代碼產生和對XML沒有要求配置------------------------------------- Spring Boot配置簡單、功能齊全,如果我們只需要開發一個功能簡單的程式,就可以使用Spring Boot,省去繁瑣的架構整合時間。 下面就開始進行一個Spring Boot demo的建立吧!
第一步:下載壓縮包,匯入項目。 : http://start.spring.io/ 下載獲得: demo.zip 解壓縮並匯入到項目中。
第二步:編寫相關代碼。 1.pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.example</groupId> 7 <artifactId>demo</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging>10 11 <name>demo</name>12 <description>Demo project for Spring Boot</description>13 14 <parent>15 <groupId>org.springframework.boot</groupId>16 <artifactId>spring-boot-starter-parent</artifactId>17 <version>1.5.2.RELEASE</version>18 <relativePath /> <!-- lookup parent from repository -->19 </parent>20 21 <properties>22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>24 <java.version>1.8</java.version>25 </properties>26 27 <dependencies>28 <dependency>29 <groupId>org.springframework.boot</groupId>30 <artifactId>spring-boot-starter</artifactId>31 </dependency>32 33 <dependency>34 <groupId>org.springframework.boot</groupId>35 <artifactId>spring-boot-starter-test</artifactId>36 <scope>test</scope>37 </dependency>38 39 40 <dependency>41 <groupId>org.springframework.boot</groupId>42 <artifactId>spring-boot-starter-web</artifactId>43 </dependency>44 45 <!-- mybatis -->46 <dependency>47 <groupId>org.mybatis.spring.boot</groupId>48 <artifactId>mybatis-spring-boot-starter</artifactId>49 <version>1.1.1</version>50 </dependency>51 <dependency>52 <groupId>mysql</groupId>53 <artifactId>mysql-connector-java</artifactId>54 </dependency>55 <dependency>56 <groupId>org.springframework.boot</groupId>57 <artifactId>spring-boot-devtools</artifactId>58 <optional>true</optional>59 </dependency>60 </dependencies>61 62 <build>63 <plugins>64 <plugin>65 <groupId>org.springframework.boot</groupId>66 <artifactId>spring-boot-maven-plugin</artifactId>67 </plugin>68 </plugins>69 </build>70 71 72 </project>
2.application.properties
1 mybatis.type-aliases-package=com.example.myproject.entity2 3 spring.datasource.driverClassName = com.mysql.jdbc.Driver4 spring.datasource.url = jdbc:mysql://localhost:3306/tao?useUnicode=true&characterEncoding=utf-85 spring.datasource.username = root6 spring.datasource.password = 123
3.實體類
1 package com.example.myproject.entity; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 private String username; 7 private String password; 8 private String name; 9 private String email;10 public String getUsername() {11 return username;12 }13 public void setUsername(String username) {14 this.username = username;15 }16 public String getPassword() {17 return password;18 }19 public void setPassword(String password) {20 this.password = password;21 }22 public String getName() {23 return name;24 }25 public void setName(String name) {26 this.name = name;27 }28 public String getEmail() {29 return email;30 }31 public void setEmail(String email) {32 this.email = email;33 }34 35 }
4.DAO層代碼
1 package com.example.myproject.dao; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Delete; 6 import org.apache.ibatis.annotations.Insert; 7 import org.apache.ibatis.annotations.Select; 8 import org.apache.ibatis.annotations.Update; 9 10 import com.example.myproject.entity.User;11 12 public interface UserDao {13 @Select("SELECT * FROM user")14 List<User> getAll();15 16 @Select("SELECT * FROM user WHERE username = #{username}")17 User getOne(String username);18 19 @Insert("INSERT INTO user(username,password,name,email) VALUES(#{username}, #{password}, #{name}, #{email})")20 void insert(User user);21 22 @Update("UPDATE user SET name=#{name} WHERE username =#{username}")23 void updateNameByUsername(User user);24 25 @Delete("DELETE FROM user WHERE username =#{username}")26 void delete(String username);27 }
5.service層代碼
1 package com.example.myproject.service; 2 3 import java.util.List; 4 5 import com.example.myproject.entity.User; 6 7 public interface UserService { 8 /** 9 * 查詢全部user10 * @return11 */12 List<User> getAll();13 /**14 * 根據username查詢一條user15 * @param username16 * @return17 */18 User getOne(String username);19 /**20 * 插入一條user21 * @param user22 */23 void insert(User user);24 /**25 * 根據username查到user,修改name26 * @param user27 */28 void updateNameByUsername(User user);29 /**30 * 刪除user31 * @param username32 */33 void delete(String username);34 }
1 package com.example.myproject.service.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.example.myproject.dao.UserDao; 9 import com.example.myproject.entity.User;10 import com.example.myproject.service.UserService;11 12 @Service("userService")13 public class UserServiceImpl implements UserService {14 15 16 @Autowired17 private UserDao userDao;18 19 @Override20 public List<User> getAll() {21 return userDao.getAll();22 }23 24 @Override25 public User getOne(String username) {26 return userDao.getOne(username);27 }28 29 @Override30 public void insert(User user) {31 userDao.insert(user);32 }33 34 @Override35 public void updateNameByUsername(User user) {36 userDao.updateNameByUsername(user);37 }38 39 @Override40 public void delete(String username) {41 userDao.delete(username);42 }43 44 }
6.controller代碼:
1 package com.example.myproject.action; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 import org.springframework.web.bind.annotation.RestController;10 11 import com.example.myproject.entity.User;12 import com.example.myproject.service.UserService;13 14 @Controller15 public class HelloWorldController {16 17 18 @Autowired(required = false)19 private UserService userService;20 21 22 @RequestMapping("/getAll")23 public @ResponseBody List<User> getAll() {24 return userService.getAll();25 }26 27 @RequestMapping("/getOne")28 public @ResponseBody User getOne(String username) {29 return userService.getOne(username);30 }31 32 @RequestMapping("/insert")33 public @ResponseBody User insert() {34 User user = new User();35 user.setUsername("zs");36 user.setPassword("123456");37 user.setName("zhangsan");38 user.setEmail("123@qq.com");39 userService.insert(user);40 return userService.getOne("zs");41 }42 43 @RequestMapping("/updateNameByUsername")44 public @ResponseBody User updateNameByUsername() {45 User user = new User();46 user.setUsername("zs");47 user.setName("zs1111");48 userService.updateNameByUsername(user);49 return userService.getOne("zs");50 }51 52 @RequestMapping("/delete")53 public @ResponseBody List<User> delete() {54 userService.delete("zs");55 return userService.getAll();56 }57 }
7.運行代碼修改:
1 package com.example; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 //地址到dao層介面處,否則報錯 9 @MapperScan("com.example.myproject.dao")10 public class DemoApplication {11 12 public static void main(String[] args) {13 SpringApplication.run(DemoApplication.class, args);14 }15 }
至此,只要運行這個main方法,訪問 localhost:8080/getAll 就可以運行了。
簡單的試用了一下Spring Boot,確實被它簡單的配置給驚到了,看來以後自己學點學習的demo時,就用它吧!哈哈。。。
感謝以下博文:
http://www.cnblogs.com/ityouknow/p/6037431.html
http://www.cnblogs.com/ityouknow/p/5662753.html