基本的學習流程請查看 泥瓦匠的blog
http://www.bysocket.com/?cat=72
他的各個小節的例子
https://github.com/JeffLi1993/springboot-learning-example
sms-server
POM.XML,由於項目主要是用來發簡訊服務的,所以其他依賴包加上也無妨,不影響
打war包的方式如下面所示
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xialeme</groupId> <artifactId>sms-server</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 部署到tomcat中需要使用 --> <packaging>war</packaging> <properties> <start-class>com.Application</start-class><!-- main檔案路徑 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories><!--ali 程式碼程式庫 --> <repository> <id>maven-ali</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> </repositories> <!-- spring boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencies> <!-- 部署到tomcat中需要使用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <!-- <scope>provided</scope> --> </dependency> <!-- spring boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 引入 spring-boot-starter-actuator 監控需要--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 引入 spring-boot-starter-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <!-- alibaba fastjson httpclient --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <!-- jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> <skipTests>true</skipTests> <!-- 打成war包時名子 --> <warName>sms-server</warName> <!-- mave時啟用的main路徑(因為其他包裡面測試時也加了main函數) --> <mainClass>${start-class}</mainClass> <skip>true</skip><!-- 跳過測試 --> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build></project>
application.properties配置src/main/resources下面
# 監聽連接埠server.port=8081##http://blog.csdn.net/xiaoyu411502/article/details/47865561########################################################### tomcat 配置######################################################### tomcat最大線程數,預設為200 #server.tomcat.max-threads=800 # tomcat的URI編碼 #server.tomcat.uri-encoding=UTF-8 # 存放Tomcat的日誌、Dump等檔案的臨時檔案夾,預設為系統的tmp檔案夾(如:C:\Users\Shanhy\AppData\Local\Temp) #server.tomcat.basedir=D:/springboot-log# 開啟Tomcat的Access日誌,並可以設定日誌格式的方法: #server.tomcat.access-log-enabled=true #server.tomcat.access-log-pattern= # accesslog目錄,預設在basedir/logs #server.tomcat.accesslog.directory= # 記錄檔目錄 logging.path=D:/springboot-log# 記錄檔名稱,預設為spring.log logging.file=springboot.log ###########################################################datasource 配置MySQL資料來源;########################################################spring.datasource.url=jdbc:mysql://192.168.10.10:3306/testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver# Hibernate ddl auto (create, create-drop, update) spring.jpa.properties.hibernate.hbm2ddl.auto=updatespring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10## Mybatis 配置 #由於使用的是jpa所以這裡就注釋掉mybatis#mybatis.typeAliasesPackage=org.spring.springboot.domain#mybatis.mapperLocations=classpath:mapper/*.xml########################################################### Java Persistence Api 自動進行建表######################################################### Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sqlqueryspring.jpa.show-sql = true# hibernate ddl auto (create,create-drop, update)spring.jpa.hibernate.ddl-auto = update# Naming strategyspring.jpa.hibernate.naming-strategy =org.hibernate.cfg.ImprovedNamingStrategy# stripped before adding them tothe entity manager)spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect###########################################################REDIS (RedisProperties) redis基本配置;########################################################## Redis 配置## Redis資料庫索引(預設為0)spring.redis.database=0## Redis伺服器位址spring.redis.host=127.0.0.1## Redis伺服器串連連接埠spring.redis.port=6379## Redis伺服器串連密碼(預設為空白)spring.redis.password=## 串連池最大串連數(使用負值表示沒有限制)spring.redis.pool.max-active=8## 串連池最大阻塞等待時間(使用負值表示沒有限制)spring.redis.pool.max-wait=-1## 串連池中的最大空閑串連spring.redis.pool.max-idle=8## 串連池中的最小空閑串連spring.redis.pool.min-idle=0## 連線逾時時間(毫秒)spring.redis.timeout=0
Application 不能放在java.main的根目錄,必須建一個目錄存放不然保錯
它會自動掃描它所在目錄下的子目錄的相關註解
package com;import java.util.ArrayList;import java.util.List;import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.context.embedded.FilterRegistrationBean;import org.springframework.boot.context.web.SpringBootServletInitializer;import org.springframework.context.annotation.Bean;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import com.common.filter.AuthFilter;/******* * Springboot 預設不需要繼承SpringBootServletInitializer * <p>如果部署到外部的TOMCAT則需要繼承SpringBootServletInitializer,並且重寫configure * pom中添加<packaging>war</packaging>和spring-boot-starter-tomcat.jar * </p> * @author bamboo * */@RestController@SpringBootApplicationpublic class Application { @Autowired private RedisTemplate redisTemplate; @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } @RequestMapping("/") //@ResponseBody String home() { return "sms server"; } @RequestMapping("/city") //@ResponseBody String city(String id) { // 從緩衝中擷取城市資訊 String key = "city_" + id; ValueOperations<String, String> operations = redisTemplate.opsForValue(); // 緩衝存在 boolean hasKey = redisTemplate.hasKey(key); if (hasKey) { String city = operations.get(key); System.out.println("CityServiceImpl.findCityById() : 從緩衝中擷取了城市 >> " + city.toString()); return city; } // 從 DB 中擷取城市資訊 String city = "空城"; // 插入緩衝 operations.set(key, city, 120, TimeUnit.SECONDS); System.out.println("CityServiceImpl.findCityById() : 城市插入緩衝 >> " + city.toString()); return city; } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }}
運行Application main方法
重新整理兩次瀏覽器,可以看到控制台列印如下
http://localhost:8081/city?id=1
CityServiceImpl.findCityById() : 城市插入緩衝 >> 空城CityServiceImpl.findCityById() : 從緩衝中擷取了城市 >> 空城
可以在redis desktop manager 重新整理看到資料已經插入進來了