Spring Boot精華 開發人員必備
- 首先我們對spring Boot做一個認識,它是什麼?有什麼用?我們為什麼要學習?
spring boot是用來簡化spring初期的環境搭建以及開發發過程,以提高我們的開發效率,那麼它目前為什麼這麼火?相比以前我們學的spring簡化了多少,接下來我就為大家闡述下? <1>spring boot入門 a:建立Maven專案 b:匯入spring boot依賴
org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE java.version 指定jdk版本號:新增spring-boot-starter-web依賴 org.springframework.boot spring-boot-starter-web
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
c:我們可以寫一段程式碼測試一下
c:熱部署 即使修改了輸出內容也要重啟APP,非常麻煩!可以使用spring-boot-devtools來實現! 1)介紹 spring-boot-devtools 是一個為開發者服務的一個模組,其中最重要的功能就是自動應用程式碼更改到最新的App上面去。原理是在發現程式碼有更改之後,重新啟動應用,但是速度比手動停止後再啟動還要更快,更快指的不是節省出來的手工操作的時間。 其深層原理是使用了兩個ClassLoader,一個Classloader載入那些不會改變的類(第三方Jar包),另一個ClassLoader載入會更改的類,稱為 restart ClassLoader ,這樣在有程式碼更改的時候,原來的restart ClassLoader 被丟棄,重新建立一個restart ClassLoader,由於需要載入的類相比較少,所以實現了較快的重啟時間(5秒以內) 2)使用 新增依賴包: org.springframework.boot spring-boot-devtools true 2. spring boot web springboot搭建web專案非常簡單,和springmvc的使用方式類似,但是啟動方式和配置方式有區別 a:引入 前面我們使用Spring Boot能往瀏覽器中輸出一個字串!實際上我們需要的是跳轉到一個頁面或者獲取一個Json資料。那怎麼實現呢? b:跳轉jsp 步驟: 建立Maven web project 引入依賴 配置application.properties對jsp支援 org.apache.tomcat.embed tomcat-embed-jasper provided
編寫測試Controller
編寫JSP
編寫啟動App
b:配置properties對jsp的支援 新增src/main/resources/application.properties:
#tomcat server port server.port=80
頁面預設字首目錄
spring.mvc.view.prefix=/WEB-INF/jsp/
響應頁面預設字尾
spring.mvc.view.suffix=.jsp
自定義屬性,可以在Controller中讀取
application.hello=Hello Angel From application
Yaml 方式 server: port: 8080 name: kd spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp 測試: @Controller public class HelloController { @RequestMapping("/hello") public String helloJsp(Model model){ System.out.println(“HelloController.helloJsp().hello=hello”); model.addAttribute(“hello”, “你好”); return “hello”; } } 編寫jsp: 在 src/main 下面建立 webapp/WEB-INF/jsp 目錄用來存放我們的jsp頁面:helloJsp.jsp:
<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>
Yaml 方式 spring: datasource: driverClassName: com.mysql.jdbc.Driver url : jdbc:mysql://localhost:3306/spring-boot-demo?useUnicode=true&characterEncoding=utf-8 username : root password : root 4. spring boot - spring data jpa a: 引入maven依賴mysql,springdatajpa mysql mysql-connector-java
org.springframework.boot spring-boot-starter-data-jpa b:配置jdbc spring data jpa #tomcat server port server.port=80 spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
-
spring boot - mybatis a: 整合mybatis (1)新建maven project; 新建一個maven project,取名為:spring-boot-mybatis (2)在pom.xml檔案中引入相關依賴; (1)基本依賴,jdk版本號; (2)mysql驅動,mybatis依賴包,mysql分頁PageHelper: org.springframework.boot spring-boot-starter-web mysql mysql-connector-java
<!-- spring-boot mybatis依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- spring boot mybatis 分頁外掛 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.2</version> </dependency>
(3)建立啟動類App.java @SpringBootApplication @MapperScan(“cn.itsource.springboot.mybatis.mapper”) public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } } //這裡和以往不一樣的地方就是MapperScan的註解,這個是會掃描該包下的介面
(4)在application.properties新增配置檔案; #tomcat server port server.port=80
######################################################## ###datasource ######################################################## spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10
(5)編寫User測試類; package cn.itsource.springboot.mybatis.domain;
import java.io.Serializable;
public class User implements Serializable { private static final long serialVersionUID = -2107513802540409419L; private Long id; private String name;
getter/setter...
}
(6)編寫UserMapper; 註解方式 : package cn.itsource.springboot.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import cn.itsource.springboot.mybatis.domain.User; @Mapper public interface UserMapper { @Select(“select * from t_user t_user name = #{name}”) List likeName(String name);
@Select("select * from t_user where id = #{id}")
User getById(long id);
@Select("select name from t_user where id = #{id}")
String getNameById(long id);
}
XML方式: package cn.itsource.springboot.mybatis.mapper;
import java.util.List;
import cn.itsource.springboot.mybatis.domain.User; public interface UserMapper { List likeName(String name); User getById(long id); String getNameById(long id); }
然後在resources下增加mapper.xml檔案 /cn/itsource/springboot/mybatis/mapper/UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> select * from t_user where name like concat('%',#{name},'%') select * from t_user where id = #{id} select name from t_user where id = #{id} insert into t_user(name) values(#{name})最後需要在application.properties中增加別名包和mapper xml掃描包的配置
Mybatis config
mybatis.typeAliasesPackage=cn.itsource.springboot.mybatis.domain mybatis.mapperLocations=classpath:mapper/*.xml #Yaml 配置
Mybatis配置
mybatis: typeAliasesPackage: cn.itsource.domain mapperLocations: classpath:cn/itsource/dao/mapper/*.xml
(7)編寫UserService package cn.itsource.springboot.mybatis.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import cn.itsource.springboot.mybatis.domain.User; import cn.itsource.springboot.mybatis.mapper.UserMapper;
@Service public class UserService { @Autowired private UserMapper userMapper;
public User get(Long id){
return userMapper.getById(id);
}
}
(8)編寫UserController; package cn.itsource.springboot.mybatis.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;
import cn.itsource.springboot.mybatis.domain.User; import cn.itsource.springboot.mybatis.service.UserService;
@RestController public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/{id}")
@ResponseBody
public User get(@PathVariable Long id) {
return userService.get(id);
}
} 6. 使用pageHelper分頁 在application.properties中配置分頁外掛 #pagehelper. pagehelper.autoDialect=true pagehelper.closeConn=true
在呼叫mapper的前面開啟分頁功能 package cn.itsource.springboot.mybatis.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo;
import cn.itsource.springboot.mybatis.domain.User; import cn.itsource.springboot.mybatis.mapper.UserMapper;
@Service public class UserService { @Autowired private UserMapper userMapper;
public User get(Long id){
return userMapper.getById(id);
}
public PageInfo<User> likeName(String name,Integer p) {
PageHelper.startPage(p, 1);
List<User> users = userMapper.likeName(name);
return new PageInfo<>(users);
}
}