整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          如何簡單快捷地構建Spring應用,別慌,阿里架構師

          如何簡單快捷地構建Spring應用,別慌,阿里架構師手把手教你


          、Spring介紹

          1.1 SpringBoot簡介

          在您第1次接觸和學習Spring框架的時候,是否因為其繁雜的配置而退卻了?在你第n次使用Spring框架的時候,是否覺得一堆反復黏貼的配置有一些厭煩?那么您就不妨來試試使用Spring Boot來讓你更易上手,更簡單快捷地構建Spring應用!

          Spring Boot讓我們的Spring應用變的更輕量化。比如:你可以僅僅依靠一個Java類來運行一個Spring引用。你也可以打包你的應用為jar并通過使用java -jar來運行你的Spring Web應用。

          Spring Boot的主要優點:

          • 為所有Spring開發者更快的入門
          • 開箱即用,提供各種默認配置來簡化項目配置
          • 內嵌式容器簡化Web項目
          • 沒有冗余代碼生成和XML配置的要求

          本章主要目標完成Spring Boot基礎項目的構建,并且實現一個簡單的Http請求處理,通過這個例子對Spring Boot有一個初步的了解,并體驗其結構簡單、開發快速的特性。

          1.2 系統要求:

          • Java 7及以上
          • Spring Framework 4.1.5及以上
          • 本文采用Java 1.8.0_73、Spring Boot 1.3.2調試通過。

          二、快速入門

          2.1 創建一個Maven工程

          名為”springboot-helloworld” 類型為Jar工程項目

          2.2 pom文件引入依賴

          <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>1.3.3.RELEASE</version>
              </parent>
              <dependencies>
                <!—SpringBoot web 組件 -->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
              </dependencies>

          spring-boot-starter-parent作用:在pom.xml中引入spring-boot-start-parent,spring官方的解釋叫什么stater poms,它可以提供dependency management,也就是說依賴管理,引入以后在申明其它dependency的時候就不需要version了,后面可以看到。

          spring-boot-starter-web作用:springweb 核心組件

          spring-boot-maven-plugin作用: 如果我們要直接Main啟動spring,那么以下plugin必須要添加,否則是無法啟動的。如果使用maven 的spring-boot:run的話是不需要此配置的。(我在測試的時候,如果不配置下面的plugin也是直接在Main中運行的。)

          2.3 編寫HelloWorld服務

          創建package命名為com.itmayiedu.controller(根據實際情況修改)

          創建HelloController類,內容如下

          @RestController
          @EnableAutoConfiguration
          public class HelloController {
              @RequestMapping("/hello")
                  public String index() {
                  return "Hello World";
              }
              public static void main(String[] args) {
                  SpringApplication.run(HelloController.class, args);
              }
          }

          2.4 @RestController

          在上加上RestController 表示修飾該Controller所有的方法返回JSON格式,直接可以編寫Restful接口

          2.5 @EnableAutoConfiguration

          注解:作用在于讓 Spring Boot 根據應用所聲明的依賴來對 Spring 框架進行自動配置

          這個注解告訴Spring Boot根據添加的jar依賴猜測你想如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration將假定你正在開發一個web應用并相應地對Spring進行設置。

          2.6 SpringApplication.run(HelloController.class, args);標識為啟動類

          2.6.1 @SpringbootApplication

          使用@SpringbootApplication注解 可以解決根類或者配置類(我自己的說法,就是main所在類)頭上注解過多的問題,一個@SpringbootApplication相當于@Configuration,@EnableAutoConfiguration和 @ComponentScan 并具有他們的默認屬性值

          @SpringBootApplication //等同于 @Configuration @EnableAutoConfiguration @ComponentScanpublic
          class Application {
              public static void main(String[] args) {
                  SpringApplication.run(Application.class, args);
              }

          2.7 SpringBoot啟動方式1

          Springboot默認端口號為8080

          @RestController
          @EnableAutoConfiguration
          public class HelloController {
              @RequestMapping("/hello")
                  public String index() {
                  return "Hello World";
              }
              public static void main(String[] args) {
                  SpringApplication.run(HelloController.class, args);
              }
          }

          啟動主程序,打開瀏覽器訪問http://localhost:8080/index,可以看到頁面輸出Hello World

          2.8 SpringBoot啟動方式2

          @ComponentScan(basePackages="com.itmayiedu.controller")---控制器掃包范圍

          @ComponentScan(basePackages="com.itmayiedu.controller")
          @EnableAutoConfiguration
          public class App {
              public static void main(String[] args) {
                  SpringApplication.run(App.class, args);
              }
          }

          三、Web開發

          3.1 靜態資源訪問

          在我們開發Web應用的時候,需要引用大量的js、css、圖片等靜態資源。

          默認配置

          Spring Boot默認提供靜態資源目錄位置需置于classpath下,目錄名需符合如下規則:

          /static
          /public
          /resources
          /META-INF/resources

          舉例:我們可以在src/main/resources/目錄下創建static,在該位置放置一個圖片文件。啟動程序后,嘗試訪問http://localhost:8080/D.jpg。如能顯示圖片,配置成功。

          3.2 全局捕獲異常

          • @ExceptionHandler 表示攔截異常
          • @ControllerAdvice 是 controller 的一個輔助類,最常用的就是作為全局異常處理的切面類
          • @ControllerAdvice 可以指定掃描范圍
          • @ControllerAdvice 約定了幾種可行的返回值,如果是直接返回 model 類的話,需要使用 @ResponseBody 進行 json 轉換
          • 返回 String,表示跳到某個 view
          • 返回 modelAndView
          • 返回 model + @ResponseBody
          @ControllerAdvice
          public class GlobalExceptionHandler {
              @ExceptionHandler(RuntimeException.class)
                  @ResponseBody
                  public Map<String, Object> exceptionHandler() {
                  Map<String, Object> map=new HashMap<String, Object>();
                  map.put("errorCode", "101");
                  map.put("errorMsg", "系統錯誤!");
                  return map;
              }
          }

          3.3 渲染Web頁面

          渲染Web頁面

          在之前的示例中,我們都是通過@RestController來處理請求,所以返回的內容為json對象。那么如果需要渲染html頁面的時候,要如何實現呢?

          模板引擎

          在動態HTML實現上Spring Boot依然可以完美勝任,并且提供了多種模板引擎的默認配置支持,所以在推薦的模板引擎下,我們可以很快的上手開發動態網站。

          Spring Boot提供了默認配置的模板引擎主要有以下幾種:

          • Thymeleaf
          • FreeMarker
          • Velocity
          • Groovy
          • Mustache

          Spring Boot建議使用這些模板引擎,避免使用JSP,若一定要使用JSP將無法實現Spring Boot的多種特性,具體可見后文:支持JSP的配置

          當你使用上述模板引擎中的任何一個,它們默認的模板配置路徑為:src/main/resources/templates。當然也可以修改這個路徑,具體如何修改,可在后續各模板引擎的配置屬性中查詢并修改。

          3.4 使用Freemarker模板引擎渲染web視圖

          3.4.1 pom文件引入:

          <!-- 引入freeMarker的依賴包. -->
          <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-freemarker</artifactId>
          </dependency>

          3.4.2 后臺代碼在src/main/resources/創建一個templates文件夾,后綴為*.ftl

          @RequestMapping("/index")
              public String index(Map<String, Object> map) {
              map.put("name","美麗的天使...");
              return "index";
          }

          3.4.3 前臺代碼

          <!DOCTYPE html>
          <html>
          <head lang="en">
          <meta charset="UTF-8" />
          <title></title>
          </head>
          <body>
                ${name}
          </body> 
          </html>

          3.4.4 Freemarker其他用法

          @RequestMapping("/index")
              public String index(Map<String, Object> map) {
              map.put("name","###螞蟻課堂###");
              map.put("sex",1);
              List<String> userlist=new ArrayList<String>();
              userlist.add("余勝軍");
              userlist.add("張三");
              userlist.add("李四");
              map.put("userlist",userlist);
              return "index";
          }
          <!DOCTYPE html>
          <html>
          <head lang="en">
          <meta charset="UTF-8" />
          <title>首頁</title>
          </head>
          <body>
                ${name}
                <#if sex==1>
                      男
                <# elseif sex==2>
                      女
               <# else>
                  其他      
                </#if>
               <#list userlist as user>
                 ${user}
               </#list>
          </body> 
          </html>

          3.4.5 Freemarker配置

          新建application.properties文件

          ########################################################
          ###FREEMARKER (FreeMarkerAutoConfiguration)
          ########################################################
          spring.freemarker.allow-request-override=false
          spring.freemarker.cache=true
          spring.freemarker.check-template-location=true
          spring.freemarker.charset=UTF-8
          spring.freemarker.content-type=text/html
          spring.freemarker.expose-request-attributes=false
          spring.freemarker.expose-session-attributes=false
          spring.freemarker.expose-spring-macro-helpers=false
          #spring.freemarker.prefix=#spring.freemarker.request-context-attribute=#spring.freemarker.settings.*=spring.freemarker.suffix=.ftl
          spring.freemarker.template-loader-path=classpath:/templates/
          #comma-separated list
          #spring.freemarker.view-names=# whitelist of view names that can be resolved

          3.5 使用JSP渲染Web視圖

          3.5.1 pom文件引入以下依賴

          <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>1.3.3.RELEASE</version>
              </parent>
              <dependencies>
                  <!-- SpringBoot 核心組件 -->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-tomcat</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.apache.tomcat.embed</groupId>
                      <artifactId>tomcat-embed-jasper</artifactId>
                  </dependency>
              </dependencies>

          3.5.2 在application.properties創建以下配置

          spring.mvc.view.prefix=/WEB-INF/jsp/
          spring.mvc.view.suffix=.jsp

          3.5.3 后臺代碼

          @Controller
          public class IndexController {
              @RequestMapping("/index")
                  public String index() {
                  return "index";
              }
          }


          四、數據訪問

          4.1 springboot整合使用JdbcTemplate

          4.1.1 pom文件引入

          <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>1.5.2.RELEASE</version>
              </parent>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-jdbc</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>mysql</groupId>
                      <artifactId>mysql-connector-java</artifactId>
                      <version>5.1.21</version>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-test</artifactId>
                      <scope>test</scope>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
              </dependencies>

          4.1.2 application.properties新增配置

          spring.datasource.url=jdbc:mysql://localhost:3306/test
          spring.datasource.username=root
          spring.datasource.password=root
          spring.datasource.driver-class-name=com.mysql.jdbc.Driver

          4.1.3 UserService類

          @Service
          public class UserServiceImpl implements UserService {
              @Autowired
              private JdbcTemplate jdbcTemplate;
              public void createUser(String name, Integer age) {
                  System.out.println("ssss");
                  jdbcTemplate.update("insert into users values(null,?,?);", name, age);
              }
          }

          4.1.4 App類

          @ComponentScan(basePackages="com.itmayiedu")
          @EnableAutoConfiguration
          public class App {
              public static void main(String[] args) {
                  SpringApplication.run(App.class, args);
              }
          }

          注意: spring-boot-starter-parent要在1.5以上

          4.2 springboot整合使用mybatis

          4.2.1 pom文件引入

          <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>1.3.2.RELEASE</version>
                  <relativePath /> <!-- lookup parent from repository -->
              </parent>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-test</artifactId>
                      <scope>test</scope>
                  </dependency>
                  <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.21</version>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
              </dependencies>

          4.2.2 配置文件引入

          spring.datasource.url=jdbc:mysql://localhost:3306/test
          spring.datasource.username=root
          spring.datasource.password=root
          spring.datasource.driver-class-name=com.mysql.jdbc.Driver

          4.2.3 Mapper代碼

          public interface UserMapper {
              @Select("SELECT * FROM USERS WHERE NAME=#{name}")
                  User findByName(@Param("name") String name);
              @Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})")
                  int insert(@Param("name") String name, @Param("age") Integer age);
          }

          4.2.4 啟動方式

          @ComponentScan(basePackages="com.itmayiedu")
          @MapperScan(basePackages="com.itmayiedu.mapper")
          @SpringBootApplication
          public class App {
              public static void main(String[] args) {
                  SpringApplication.run(App.class, args);
              }
          }

          4.3 springboot整合使用springjpa

          4.3.1 pom文件引入依賴

          <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>1.4.2.RELEASE</version>
              </parent>
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-data-jpa</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>mysql</groupId>
                      <artifactId>mysql-connector-java</artifactId>
                      <version>5.1.21</version>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>
              </dependencies>

          4.3.2 創建User實體類

          @Entity(name="users")
          public class User {
              @Id
                  @GeneratedValue
                  private Integer id;
              @Column
                  private String name;
              @Column
                  private Integer age;
              // ..get/set方法
          }

          4.3.3 創建UserDao

          public interface UserDao extends JpaRepository<User, Integer> {
          }

          4.3.4 創建IndexController

          @RestController
          public class IndexController {
              @Autowired
                  private UserDao userDao;
              @RequestMapping("/index")
                  public String index(Integer id) {
                  User findUser=userDao.findOne(id);
                  System.out.println(findUser.getName());
                  return "success";
              }
          }

          4.3.5 啟動項目

          @ComponentScan(basePackages={ "com.itmayiedu" })
          @EnableJpaRepositories(basePackages="com.itmayiedu.dao")
          @EnableAutoConfiguration
          @EntityScan(basePackages="com.itmayiedu.entity")
          public class App {
              public static void main(String[] args) {
                  SpringApplication.run(App.class, args);
              }
          }

          4.4 springboot整合多數據源

          思考一下,你們在項目中有使用到多數據源嗎?

          4.4.1 配置文件中新增兩個數據源

          spring.datasource.test1.driverClassName=com.mysql.jdbc.Driver
          spring.datasource.test1.url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
          spring.datasource.test1.username=root
          spring.datasource.test1.password=root
          
          spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver
          spring.datasource.test2.url=jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8
          spring.datasource.test2.username=root
          spring.datasource.test2.password=root

          4.4.2 配置文件中新增兩個數據源

          @Configuration // 注冊到springboot容器中
          @MapperScan(basePackages="com.itmayiedu.user1", sqlSessionFactoryRef="test1SqlSessionFactory")
          public class DataSource1Config {
              /**
               * 
               * @methodDesc: 功能描述:(配置test1數據庫)
               * @author: 余勝軍
               * @param: @return
               * @createTime:2017年9月17日 下午3:16:44
               * @returnType:@return DataSource
               * @copyright:上海每特教育科技有限公司
               * @QQ:644064779
               */
              @Bean(name="test1DataSource")
                  @Primary
                  @ConfigurationProperties(prefix="spring.datasource.test1")
                  public DataSource testDataSource() {
                  return DataSourceBuilder.create().build();
              }
              /**
               * 
               * @methodDesc: 功能描述:(test1 sql會話工廠)
               * @author: 余勝軍
               * @param: @param
               *             dataSource
               * @param: @return
               * @param: @throws
               *             Exception
               * @createTime:2017年9月17日 下午3:17:08
               * @returnType:@param dataSource
               * @returnType:@return
               * @returnType:@throws Exception SqlSessionFactory
               * @copyright:上海每特教育科技有限公司
               * @QQ:644064779
               */
              @Bean(name="test1SqlSessionFactory")
                  @Primary
                  public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
                          throws Exception {
                  SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
                  bean.setDataSource(dataSource);
                  //      bean.setMapperLocations(
                  //              new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
                  return bean.getObject();
              }
              /**
               * 
               * @methodDesc: 功能描述:(test1 事物管理)
               * @author: 余勝軍
               * @param: @param
               *             dataSource
               * @param: @return
               * @param: @throws
               *             Exception
               * @createTime:2017年9月17日 下午3:17:08
               * @returnType:@param dataSource
               * @returnType:@return
               * @returnType:@throws Exception SqlSessionFactory
               * @copyright:上海每特教育科技有限公司
               * @QQ:644064779
               */
              @Bean(name="test1TransactionManager")
                  @Primary
                  public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
                  return new DataSourceTransactionManager(dataSource);
              }
              @Bean(name="test1SqlSessionTemplate")
                  public SqlSessionTemplate testSqlSessionTemplate(
                          @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
                  return new SqlSessionTemplate(sqlSessionFactory);
              }
          }

          4.4.2 創建分包Mapper

          public interface User1Mapper {
              @Insert("insert into users values(null,#{name},#{age});")
                  public int addUser(@Param("name") String name, @Param("age") Integer age);
          }

          4.4.3 啟動項目

          @ComponentScan(basePackages="com.itmayiedu")
          @EnableAutoConfiguration
          public class App {
              public static void main(String[] args) {
                  SpringApplication.run(App.class, args);
              }
          }

          五、事物管理

          5.1.1 springboot整合事物管理

          springboot默認集成事物,只主要在方法上加上@Transactional即可

          5.1.2 SpringBoot分布式事物管理

          使用springboot+jta+atomikos 分布式事物管理

          5.1.2.1 新增配置文件信息

          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-jta-atomikos</artifactId>
          </dependency>

          5.1.2.2 新增配置文件信息

          # Mysql 1
          mysql.datasource.test.url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
          mysql.datasource.test.username=root
          mysql.datasource.test.password=root
          
          mysql.datasource.test.minPoolSize=3
          mysql.datasource.test.maxPoolSize=25
          mysql.datasource.test.maxLifetime=20000
          mysql.datasource.test.borrowConnectionTimeout=30
          mysql.datasource.test.loginTimeout=30
          mysql.datasource.test.maintenanceInterval=60
          mysql.datasource.test.maxIdleTime=60
          mysql.datasource.test.testQuery=select 1
          
          
          # Mysql 2
          mysql.datasource.test2.url=jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8
          mysql.datasource.test2.username=root
          mysql.datasource.test2.password=root
          
          mysql.datasource.test2.minPoolSize=3
          mysql.datasource.test2.maxPoolSize=25
          mysql.datasource.test2.maxLifetime=20000
          mysql.datasource.test2.borrowConnectionTimeout=30
          mysql.datasource.test2.loginTimeout=30
          mysql.datasource.test2.maintenanceInterval=60
          mysql.datasource.test2.maxIdleTime=60
          mysql.datasource.test2.testQuery=select 1
          

          5.1.2.3 讀取配置文件信息

          package com.itmayiedu.config;
          import org.springframework.boot.context.properties.ConfigurationProperties;
          @ConfigurationProperties(prefix="mysql.datasource.test")
          public class DBConfig1 {
              private String url;
              private String username;
              private String password;
              private int minPoolSize;
              private int maxPoolSize;
              private int maxLifetime;
              private int borrowConnectionTimeout;
              private int loginTimeout;
              private int maintenanceInterval;
              private int maxIdleTime;
              private String testQuery;
          }
          package com.itmayiedu.config;
          import org.springframework.boot.context.properties.ConfigurationProperties;
          @ConfigurationProperties(prefix="mysql.datasource.test1")
          public class DBConfig2 {
              private String url;
              private String username;
              private String password;
              private int minPoolSize;
              private int maxPoolSize;
              private int maxLifetime;
              private int borrowConnectionTimeout;
              private int loginTimeout;
              private int maintenanceInterval;
              private int maxIdleTime;
              private String testQuery;
          }
          

          5.1.2.4 創建多數據源

          @Configuration
          // basePackages 最好分開配置 如果放在同一個文件夾可能會報錯
          @MapperScan(basePackages="com.itmayiedu.test01", sqlSessionTemplateRef="testSqlSessionTemplate")
          public class TestMyBatisConfig1 {
              // 配置數據源
              @Primary
                  @Bean(name="testDataSource")
                  public DataSource testDataSource(DBConfig1 testConfig) throws SQLException {
                  MysqlXADataSource mysqlXaDataSource=new MysqlXADataSource();
                  mysqlXaDataSource.setUrl(testConfig.getUrl());
                  mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
                  mysqlXaDataSource.setPassword(testConfig.getPassword());
                  mysqlXaDataSource.setUser(testConfig.getUsername());
                  mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
                  AtomikosDataSourceBean xaDataSource=new AtomikosDataSourceBean();
                  xaDataSource.setXaDataSource(mysqlXaDataSource);
                  xaDataSource.setUniqueResourceName("testDataSource");
                  xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
                  xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());
                  xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
                  xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
                  xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
                  xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());
                  xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
                  xaDataSource.setTestQuery(testConfig.getTestQuery());
                  return xaDataSource;
              }
              @Bean(name="testSqlSessionFactory")
                  public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource)
                          throws Exception {
                  SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
                  bean.setDataSource(dataSource);
                  return bean.getObject();
              }
              @Bean(name="testSqlSessionTemplate")
                  public SqlSessionTemplate testSqlSessionTemplate(
                          @Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
                  return new SqlSessionTemplate(sqlSessionFactory);
              }
          }
          package com.itmayiedu.datasource;
          import java.sql.SQLException;
          import javax.sql.DataSource;
          import org.apache.ibatis.session.SqlSessionFactory;
          import org.mybatis.spring.SqlSessionFactoryBean;
          import org.mybatis.spring.SqlSessionTemplate;
          import org.mybatis.spring.annotation.MapperScan;
          import org.springframework.beans.factory.annotation.Qualifier;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;
          import com.atomikos.jdbc.AtomikosDataSourceBean;
          import com.itmayiedu.config.DBConfig1;
          import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
          @Configuration
          // basePackages 最好分開配置 如果放在同一個文件夾可能會報錯
          @MapperScan(basePackages="com.itmayiedu.test02", sqlSessionTemplateRef="test2SqlSessionTemplate")
          public class TestMyBatisConfig2 {
              // 配置數據源
              @Bean(name="test2DataSource")
                  public DataSource testDataSource(DBConfig1 testConfig) throws SQLException {
                  MysqlXADataSource mysqlXaDataSource=new MysqlXADataSource();
                  mysqlXaDataSource.setUrl(testConfig.getUrl());
                  mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
                  mysqlXaDataSource.setPassword(testConfig.getPassword());
                  mysqlXaDataSource.setUser(testConfig.getUsername());
                  mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
                  AtomikosDataSourceBean xaDataSource=new AtomikosDataSourceBean();
                  xaDataSource.setXaDataSource(mysqlXaDataSource);
                  xaDataSource.setUniqueResourceName("test2DataSource");
                  xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
                  xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());
                  xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
                  xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
                  xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
                  xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());
                  xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
                  xaDataSource.setTestQuery(testConfig.getTestQuery());
                  return xaDataSource;
              }
              @Bean(name="test2SqlSessionFactory")
                  public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
                          throws Exception {
                  SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
                  bean.setDataSource(dataSource);
                  return bean.getObject();
              }
              @Bean(name="test2SqlSessionTemplate")
                  public SqlSessionTemplate testSqlSessionTemplate(
                          @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
                  return new SqlSessionTemplate(sqlSessionFactory);
              }
          }

          5.1.2.4 啟動加載配置

          @EnableConfigurationProperties(value={ DBConfig1.class, DBConfig2.class })

          六、日志管理

          6.1 使用log4j記錄日志

          6.1.2 新建log4j配置文件

          #log4j.rootLogger=CONSOLE,info,error,DEBUG
          log4j.rootLogger=info,error,CONSOLE,DEBUG
          log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender     
          log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout     
          log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n     
          log4j.logger.info=info
          log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
          log4j.appender.info.layout=org.apache.log4j.PatternLayout     
          log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
          log4j.appender.info.datePattern='.'yyyy-MM-dd
          log4j.appender.info.Threshold=info   
          log4j.appender.info.append=true   
          #log4j.appender.info.File=/home/admin/pms-api-services/logs/info/api_services_info
          log4j.appender.info.File=/Users/dddd/Documents/testspace/pms-api-services/logs/info/api_services_info
          log4j.logger.error=error  
          log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
          log4j.appender.error.layout=org.apache.log4j.PatternLayout     
          log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
          log4j.appender.error.datePattern='.'yyyy-MM-dd
          log4j.appender.error.Threshold=error   
          log4j.appender.error.append=true   
          #log4j.appender.error.File=/home/admin/pms-api-services/logs/error/api_services_error
          log4j.appender.error.File=/Users/dddd/Documents/testspace/pms-api-services/logs/error/api_services_error
          log4j.logger.DEBUG=DEBUG
          log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender
          log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout     
          log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
          log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd
          log4j.appender.DEBUG.Threshold=DEBUG   
          log4j.appender.DEBUG.append=true   
          #log4j.appender.DEBUG.File=/home/admin/pms-api-services/logs/debug/api_services_debug
          log4j.appender.DEBUG.File=/Users/dddd/Documents/testspace/pms-api-services/logs/debug/api_services_debug

          6.2 使用AOP統一處理Web請求日志

          6.2.1 POM文件新增依賴

          <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-aop</artifactId>
              </dependency>

          6.2.2 POM文件新增依賴

          @Aspect
          @Component
          public class WebLogAspect {
              private Logger logger=LoggerFactory.getLogger(getClass());
              @Pointcut("execution(public * com.itmayiedu.controller..*.*(..))")
                  public void webLog() {
              }
              @Before("webLog()")
                  public void doBefore(JoinPoint joinPoint) throws Throwable {
                  // 接收到請求,記錄請求內容
                  ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                  HttpServletRequest request=attributes.getRequest();
                  // 記錄下請求內容
                  logger.info("URL : " + request.getRequestURL().toString());
                  logger.info("HTTP_METHOD : " + request.getMethod());
                  logger.info("IP : " + request.getRemoteAddr());
                  Enumeration<String> enu=request.getParameterNames();
                  while (enu.hasMoreElements()) {
                      String name=(String) enu.nextElement();
                      logger.info("name:{},value:{}", name, request.getParameter(name));
                  }
              }
              @AfterReturning(returning="ret", pointcut="webLog()")
                  public void doAfterReturning(Object ret) throws Throwable {
                  // 處理完請求,返回內容
                  logger.info("RESPONSE : " + ret);
              }
          }

          七、緩存支持

          7.1 注解配置與EhCache使用

          7.1.1 pom文件引入

          <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-bot-starter-cache</artifactId>
          </dependency>
          

          7.1.2 新建ehcache.xml 文件

          <?xml version="1.0" encoding="UTF-8"?>
          <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
              updateCheck="false">
              <diskStore path="java.io.tmpdir/Tmp_EhCache" />
              <!-- 默認配置 -->
              <defaultCache maxElementsInMemory="5000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120"
                  memoryStoreEvictionPolicy="LRU" overflowToDisk="false" />
              <cache name="baseCache" maxElementsInMemory="10000"
                  maxElementsOnDisk="100000" />
          </ehcache>

          配置信息介紹

          <!--
          name:緩存名稱。
          maxElementsInMemory:緩存最大個數。
          eternal:對象是否永久有效,一但設置了,timeout將不起作用。  
          timeToIdleSeconds:設置對象在失效前的允許閑置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大。  
          timeToLiveSeconds:設置對象在失效前允許存活時間(單位:秒)。最大時間介于創建時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。  
          overflowToDisk:當內存中對象數量達到maxElementsInMemory時,Ehcache將會對象寫到磁盤中。  
          diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩沖區。  
          maxElementsOnDisk:硬盤最大緩存個數。  
          diskPersistent:是否緩存虛擬機重啟期數據 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.  
          diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。  
          12.        memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置為FIFO(先進先出)或是LFU(較少使用)。  
          13.        clearOnFlush:內存數量最大時是否清除。  
          14.     --> 

          7.1.3 代碼使用Cacheable

          @CacheConfig(cacheNames="baseCache")
          public interface UserMapper {
              @Select("select * from users where name=#{name}")
                  @Cacheable
                  UserEntity findName(@Param("name") String name);
          }

          7.1.4 清除緩存

          @Autowired
          private CacheManager cacheManager;
          @RequestMapping("/remoKey")
          public void remoKey() {
              cacheManager.getCache("baseCache").clear();
          }

          使用Redis做集中式緩存

          八、其他內容

          8.1 使用@Scheduled創建定時任務

          在Spring Boot的主類中加入@EnableScheduling注解,啟用定時任務的配置

          @Component
          public class ScheduledTasks {
              private static final SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm:ss");
              @Scheduled(fixedRate=5000)
              public void reportCurrentTime() {
                  System.out.println("現在時間:" + dateFormat.format(new Date()));
              }
          }

          8.2 使用@Async實現異步調用

          啟動加上@EnableAsync ,需要執行異步方法上加入 @Async

          8.3 自定義參數

          配置文件值

          name=itmayiedu.com

          配置文件值

          @Value("${name}")
              private String name;
          @ResponseBody
              @RequestMapping("/getValue")
              public String getValue() {
              return name;
          }

          8.4 多環境配置

          spring.profiles.active=pre
          application-dev.properties:開發環境
          application-test.properties:測試環境
          application-prod.properties:生產環境

          8.5 修改端口號

          server.port=8888 
          server.context-path=/itmayiedu

          8.6 SpringBoot yml 使用創建application.yml

          server:
            port:  8090
            context-path: /itmayiedu

          8.7 發布打包

          使用mvn clean清除以前的編譯文件使用mvn package 打包使用java –jar 包名如果報錯沒有主清單,在pom文件中新增

          <build>
                  <plugins>
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-compiler-plugin</artifactId>
                          <configuration>
                              <source>1.8</source>
                              <target>1.8</target>
                          </configuration>
                      </plugin>
                      <plugin>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-maven-plugin</artifactId>
                          <configuration>
                              <maimClass>com.itmayiedu.app.App</maimClass>
                          </configuration>
                          <executions>
                              <execution>
                                  <goals>
                                      <goal>repackage</goal>
                                  </goals>
                              </execution>
                          </executions>
                      </plugin>
                  </plugins>
              </build>

          寫在最后:

          • 針對于Java程序員,筆者最近整理了一些面試真題,思維導圖,程序人生等PDF學習資料;
          • 關注私信我"86",即可獲取!
          • 希望讀到這的您能點個小贊和關注下我,以后還會更新技術干貨,謝謝您的支持!

          先聲明此項目來自開源網,小編也是愛好者;

          閱讀本文之前相信已經閱讀過【開源在線考試系統一鍵生成各種題型試卷且可以實時判卷】

          開源在線考試系統一鍵生成各種題型試卷且可以實時判卷

          本文分享快速部署版jar包,原項目前后臺是單獨項目,其中前臺用vue,后臺java;對于好多愛好者入門相對有難度;所以咱們整合到一起,直接啟動jar包就可以使用啦

          打包啟動流程

          前臺編譯打包

          • git clone
          • npm install
          • 資源地址和演示地址在文章尾部↓↓↓↓↓↓↓
          • npm run build

          這樣就生成了dist文件夾,里面就是前臺項目的html代碼

          后臺打包

          把前臺打包dist放入后臺resource下,然后package即可

          啟動命令

          首先進入jar包所在目錄

          Linux啟動:

          • nohup java -jar exam-2.0.1.jar --spring.profiles.active=prod --server.port=8130(端口) --spring.redis.host=127.0.0.1(redis地址) --spring.redis.password=redis密碼 --spring.datasource.username=數據庫用戶名 --spring.datasource.password=數據庫密碼 &

          window啟動:

          • java -jar exam-2.0.1.jar --spring.profiles.active=prod --server.port=8130(端口) --spring.redis.host=127.0.0.1(redis地址) --spring.redis.password=redis密碼 --spring.datasource.username=數據庫用戶名 --spring.datasource.password=數據庫密碼

          需要修改內容

          富文本修改為相對路徑

          后臺添加靜態資源和過濾靜態資源

          后臺數據庫配置內容

          前臺根路徑修改為相對路徑

          前臺js引入修改為相對路徑

          打包后的jar包結構

          獲取方式

          關注+轉發+私信“考試”,自動發送資源地址

          (溫馨提示:先關注,然后轉發,然后私信引號內的關鍵字)

          時候我們可能需要用到json文件存儲數據,然后通過前臺語言直接進行訪問。

          首先是json文件:

          {
           "管道": [
           {
           "ElementId": "標識號",
           "GISID": "GISID",
           "Label": "編號",
           "StartNodeID":"起始節點ID",
           "EndNodeID":"終止節點ID",
           "StartNodeLabel":"起始節點編號",
           "EndNodeLabel":"終止節點編號",
           "Physical_PipeDiameter":"管徑",
           "Physical_PipeMaterialID":"管材",
           "Physical_HazenWilliamsC":"海曾威廉C值",
           "Physical_Length":"管長",
           "Physical_MinorLossCoefficient":"局部阻力系數",
           "Physical_InstallationYear":"鋪設年代",
           "Physical_Address":"地址",
           "District":"營銷公司",
           "DMA":"計量區",
           "Zone":"行政區",
           "flow":"當前流量",
           "velocity":"當前流速",
           "headloss":"當前水頭損失"
           }],
           "節點": [{
           "ElementId": "標識號",
           "GISID": "GISID",
           "Label": "編號",
           "Physical_Elevation":"地面高程",
           "Physical_Depth":"埋深",
           "Physical_Address":"地址",
           "District":"營銷公司",
           "DMA":"計量區",
           "Zone":"行政區",
           "hydraulicGrade":"水壓標高",
           "pressure":"自由水壓",
           "demand":"節點流量",
           "cl":"余氯濃度",
           "age":"水齡",
           "source":"供水水源"
           }],
           "閥門": [{
           "ElementId": "標識號",
           "GISID": "GISID",
           "Label": "編號",
           "Physical_Elevation":"地面高程",
           "Physical_Depth":"埋深",
           "Physical_Diameter":"口徑",
           "Physical_Status":"閥門狀態",
           "Physical_Address":"地址",
           "District":"營銷公司",
           "DMA":"計量區",
           "Zone":"行政區",
           "Physical_InstallationYear":"安裝年代"
           }],
           "水表": [{
           "DIAMETER":"口徑",
           "CALIBER": "表徑",
           "MATERIAL": "材質",
           "DEPTH":"埋深",
           "HEIGHT":"地面高程",
           "ADDR":"地址",
           "WATREGID": "表號",
           "USERNAME":"用戶名",
           "JUNCTION":"接口類型",
           "DISTRICT":"行政區",
           "MEASUREIN":"營銷公司",
           "FINISHDATE":"安裝日期"
           }],
           "消火栓": [{
           "ElementId": "標識號",
           "GISID": "GISID",
           "Label": "編號",
           "Physical_Elevation":"地面高程",
           "Physical_Depth":"埋深",
           "Physical_Address":"地址",
           "District":"營銷公司",
           "DMA":"計量區",
           "Zone":"行政區",
           "Physical_Diameter":"口徑",
           "Physical_Type":"樣式"
           }]
          }
          

          創建CriteriaQuery.json文件,文件內容如上。

          前臺代碼:

          <html>
          <head>
           <meta charset="GBK"/>
           <title></title>
           <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
           <script type="text/javascript">
           $(function (){
           $("#btn").click(function(){
           $.getJSON("CriteriaQuery.json",function(data){
           var $jsontip=$("#jsonTip");
           var strHtml="";
           $jsontip.empty();
           $.each(data.管道,function(infoIndex,info){
           for(var o in info){
           
           strHtml +=info[o];
           }
           //strHtml +=info["ElementId"];
           });
           $jsontip.html(strHtml);
           })
           })
           })
           </script>
          </head>
          <body>
          <div id="divframe">
           <div class="loadTitle">
           <input type="button" value="獲取數據" id="btn"/>
           </div>
           <div id="jsonTip">
           </div>
          </div>
          </body>
          </html>
          

          我這里的jquery用的是微軟發布的1.4的js文件,即 http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js

          json文件與html放在同一個目錄下。

          運行代碼,點擊獲取數據按鈕,就能夠在頁面上看到遍歷json文件所得到的“管道”的所有信息。當然,也可以通過key來獲取到json文件的value,即

          strHtml +=info["ElementId"];


          主站蜘蛛池模板: 日韩在线视频不卡一区二区三区 | 麻豆一区二区免费播放网站| 精品国产一区二区三区AV性色| 日美欧韩一区二去三区| 午夜视频一区二区| 麻豆AV天堂一区二区香蕉| 久久国产精品免费一区二区三区| 无码中文字幕乱码一区| 日韩综合无码一区二区| 亚洲av无码片vr一区二区三区| 日韩精品午夜视频一区二区三区| 日韩精品乱码AV一区二区| 亚洲乱码一区二区三区国产精品| 色婷婷AV一区二区三区浪潮| 亚洲香蕉久久一区二区| 精品无码一区二区三区亚洲桃色| 国产成人久久一区二区三区| 东京热无码一区二区三区av| 无码精品人妻一区二区三区人妻斩 | 亚洲av成人一区二区三区观看在线 | 日韩视频在线观看一区二区| 高清一区二区三区视频| 精品无人区一区二区三区| 在线视频精品一区| 人妻少妇AV无码一区二区| 国产伦精品一区二区三区| 无码日韩精品一区二区免费暖暖| 久久人做人爽一区二区三区| 色一情一乱一伦一区二区三欧美 | 日本高清成本人视频一区| 午夜福利av无码一区二区 | 日韩人妻无码一区二区三区久久99| 2022年亚洲午夜一区二区福利 | 精品无码国产一区二区三区麻豆| 精品乱子伦一区二区三区| 亚洲国产欧美国产综合一区| 国产另类ts人妖一区二区三区| 亚洲福利电影一区二区?| 国产一区三区三区| 天天看高清无码一区二区三区 | 免费在线观看一区|