国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

關(guān)于Spring Boot你不得不知道的事--Spring Boot的基本操作

fancyLuo / 3477人閱讀

摘要:版本和編碼方式依賴管理這樣比如使用的時(shí)候就不需要指定版本號(hào)使用自己的項(xiàng)目這時(shí)候?qū)⒁蕾嚬芾淼膯?wèn)題放到中。

1 Pom文件
1.1 spring-boot-starter-parent
表示當(dāng)前pom文件從spring-boot-starter-parent繼承下來(lái),在spring-boot-starter-parent中提供了很多默認(rèn)配置,可以簡(jiǎn)化我們的開(kāi)發(fā)。

org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
 


Java版本和編碼方式

UTF-8
1.8
@
${java.version}
UTF-8
${java.version}


依賴管理spring-boot-dependencies

5.15.9
2.7.7
1.9.73
2.6.4
...


這樣比如使用starter-web的時(shí)候就不需要指定版本號(hào)

org.springframework.boot
spring-boot-starter-web
2.1.4.RELEASE


使用自己的parent項(xiàng)目
這時(shí)候?qū)⒁蕾嚬芾淼膯?wèn)題放到dependencyManagement中。

官網(wǎng)說(shuō)明文檔見(jiàn):13.2.2 Using Spring Boot without the Parent POM


  
     org.springframework.boot
     spring-boot-dependencies
     2.1.4.RELEASE
     pom
     import
  



1.2 打包管理
使用mvn package打包的plugin。


  
     org.springframework.boot
     spring-boot-maven-plugin
  



1.3 Starters
官網(wǎng)見(jiàn):13.5 Starters

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.
官方starter命名
spring-boot-starter-*

自定義starter命名
thirdpartyproject-spring-boot-starter

spring-boot-web-starter
查看其diagram,可以排除某個(gè)依賴


org.springframework.boot
spring-boot-starter-web

  
     org.springframework.boot
     spring-boot-starter-tomcat
  



2 XXXApplication
2.1 @SpringBootApplication
官網(wǎng)見(jiàn):18. Using the @SpringBootApplication Annotation

等同于@EnableAutoConfiguration,@ComponentScan和@Configuration

2.2 SpringApplication.run
官網(wǎng)見(jiàn):23. SpringApplication

3 配置文件
3.1 初步感受
server.port=9090
3.2 yml文件
application.yml

3.3 給屬性注入值
實(shí)體類Person和IDCard
public class Person {

private String name;
private int age;
private Date birthday;
private String[] hobbies;
private IDCard idCard;
...

}
public class IDCard {

private int id;
private String number;

}
yml注入寫法
person:

name: Jack
age: 17
birthday: 1997/06/01
hobbies: [code,sing,share]
idCard: 
    id: 1
    number: 111

Person類增加注解
@Component
@ConfigurationProperties(prefix="person")
測(cè)試
@Autowired
private Person person;
如果Person類上報(bào)錯(cuò),在Pom文件中加入如下依賴

org.springframework.boot
spring-boot-configuration-processor


4 處理動(dòng)靜態(tài)資源
4.1 動(dòng)態(tài)資源
官網(wǎng)見(jiàn):90.2 Reload Templates without Restarting the Container

templates
resources目錄下有一個(gè)templates文件夾,可以將動(dòng)態(tài)資源放到其中

引入thymeleaf

org.springframework.boot
spring-boot-starter-thymeleaf


templates下新建test.html文件




controller中return test
@Controller
@RequestMapping("/gupao")
public class GupaoController {

@RequestMapping("/hello")
public String hello(Model model){
    String str="hello spring boot";
    //想要?jiǎng)討B(tài)的顯示在網(wǎng)頁(yè)當(dāng)中
    model.addAttribute("str",str);
    //接下來(lái)的頁(yè)面是能夠動(dòng)態(tài)顯示傳過(guò)來(lái)的數(shù)據(jù)
    return "test";
}

}
4.2 靜態(tài)資源
static文件夾
在resources目錄下有一個(gè)static文件夾,可以將靜態(tài)資源放到其中,瀏覽器可以直接訪問(wèn)。

靜態(tài)資源其他存放文件夾
"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"
WebMvcAutoConfiguration源碼分析
WebMvcAutoConfiguration--->WebMvcAutoConfigurationAdapter.addResourceHandlers(xxx)--->

this.resourceProperties.getStaticLocations()
return this.staticLocations;
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {

  "classpath:/META-INF/resources/", "classpath:/resources/",
  "classpath:/static/", "classpath:/public/" };

自定義靜態(tài)資源文件夾
觀察

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
配置application.properties

spring.resources.static-locations=classpath:/gupao/
5 整合MyBatis
5.1 需求
通過(guò)Spring Boot Web項(xiàng)目api接口的方式,整合MyBatis實(shí)現(xiàn)crud的操作。

5.2 創(chuàng)建Spring Boot Web項(xiàng)目
重溫一下web項(xiàng)目創(chuàng)建的過(guò)程。

5.3 引入項(xiàng)目中需要的starter依賴

mysql
mysql-connector-java


org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1


org.springframework.boot
spring-boot-starter-thymeleaf

5.4 創(chuàng)建數(shù)據(jù)庫(kù)表
db_gupao_springboot--->t_user

5.5 創(chuàng)建domain/User對(duì)象
public class User {

private int id;
private String username;
private String password;
private String number;
...

}
5.6 開(kāi)發(fā)dao層
@Repository
@Mapper
public interface UserMapper {

User find(String username);
List list();
int insert(User user);
int delete(int id);
int update(User user);

}
5.7 開(kāi)發(fā)service層
@Service
public class UserService {

@Autowired
public UserMapper userMapper;
public User findByUsername(String username){
    return userMapper.find(username);
}
public List listUser(){
    return userMapper.list();
}
public int insertUser(User user){
    return userMapper.insert(user);
}
public int updateUser(User user){
    return userMapper.update(user);
}
public int delete(int id){
    return userMapper.delete(id);
}

}
5.8 開(kāi)發(fā)controller層
@RestController
@RequestMapping(value="/user",method = {RequestMethod.GET,RequestMethod.POST})
public class UserController {

@Autowired
private UserService userService;
@RequestMapping("/listone")
@ResponseBody
public User listOne(String username){
    return userService.findByUsername(username);
}
@RequestMapping("/listall")
@ResponseBody
public List listAll(){
    return userService.listUser();
}

@RequestMapping(value="/add",method= RequestMethod.POST)
@ResponseBody
public String add(User user){
    int result=userService.insertUser(user);
    if(result>=1) {
        return "添加成功";
    }else{
        return "添加失敗";
    }
}
@RequestMapping(value="/update",method= RequestMethod.POST)
@ResponseBody
public String update(User user){
    int result=userService.updateUser(user);
    if(result>=1) {
        return "修改成功";
    }else{
        return "修改失敗";
    }
}
@RequestMapping(value="/delete",method= RequestMethod.GET)
@ResponseBody
public String delete(int id){
    int result=userService.delete(id);
    if(result>=1) {
        return "刪除成功";
    }else{
        return "刪除失敗";
    }
}

}
5.9 resources目錄下創(chuàng)建mapper文件夾---UserMapper.xml

    "-//mybatis.org//DTD com.example.Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


    
    
    




  INSERT INTO t_user
  (
  id,username,password,number
  )
  VALUES (
  #{id},
  #{username, jdbcType=VARCHAR},
  #{password, jdbcType=VARCHAR},
  #{number}
  )


  delete from t_user where id=#{id}

update t_user set user.username=#{username},user.password=#{password},user.number=#{number} where user.id=#{id}


5.10 application.properties文件配置

數(shù)據(jù)源

spring:
datasource:

url: jdbc:mysql://127.0.0.1:3306/boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis托管mapper文件

mybatis:
mapper-locations: classpath:mapper/*.xml
5.11 啟動(dòng)項(xiàng)目測(cè)試
查詢
http://localhost:8888/user/listone?username=Jack

全部查詢
http://localhost:8888/user/listall

增加
http://localhost:8888/user/add?id=3&username=AAA&password=111111&number=300

更新
http://localhost:8888/user/update?id=3&username=BBB

刪除
http://localhost:8888/user/delete?id=3

6 項(xiàng)目打包
jar包
mvn -Dmaven.test.skip -U clean install

java -jar xxx.jar

war包
com.csdn
springboot-demo2
0.0.1-SNAPSHOT
war
7 Spring Boot in less than 10 minutes
https://www.youtube.com/watch...

BUILD ANYTHING WITH SPRING BOOT

Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.

Get started in seconds using Spring Initializr

Build anything: REST API, WebSocket, web, streaming, tasks, and more

Simplified security

Rich support for SQL and NoSQL

Embedded runtime support: Tomcat, Jetty, and Undertow

Developer productivity tools such as LiveReload and Auto Restart

Curated dependencies that just work

Production-ready features such as tracing, metrics, and health status

Works in your favorite IDE: Spring Tool Suite, IntelliJ IDEA, and NetBeans

7.1 IDEA創(chuàng)建工程
group:com.example

artifact:bootiful

dependencies:Reactive Web,Reactive MongoDB,Lombok,Actuator,Security

7.2 DATA DRIVE
Spring Data integrates seamlessly with SQL and NoSQL persistence stores. Spring Data supports reactive data access,too!

@Component
class DataWriter implements ApplicationRunner {

private final CustomerRepository customerRepository;

DataWriter(CustomerRepository customerRepository) {
    this.customerRepository = customerRepository;
}

@Override
public void run(ApplicationArguments args) throws Exception {
    Flux.just("Jack", "Rechal", "Richard", "Jobs")
            .flatMap(name -> customerRepository.save(new Customer(null, name)))
            .subscribe(System.out::println);
}

}
interface CustomerRepository extends ReactiveMongoRepository {
}
@Document
@NoArgsConstructor
@Data
class Customer {

private String id,name;

public Customer(String id, String name) {
    this.id = id;
    this.name = name;
}

}
7.3 REST
On the web,nobody knows you"re a reactive microservice.

@SpringBootApplication
public class BootifulApplication {

@Bean
RouterFunction routes(CustomerRepository cr){
    return RouterFunctions.route(GET("/customers"),serverRequest -> ok().body(cr.findAll(),Customer.class));
}

public static void main(String[] args) {
    SpringApplication.run(BootifulApplication.class, args);
}

}
7.4 OBSERVABILITY
How"s your app"s health?Who better to articulate that then the application itself?

Spring Boot featurese strong opinions,loosely held.

It"s easy to change any of them with properties or pluggable implementations

management.endpoint.health.show-details=always
management.endpoints.web.exposure.exclude=*
@Bean
HealthIndicator healthIndicator(){
return () -> Health.status("I <3 Production").build();
}
訪問(wèn):curl http://localhost:8080/actuator/health | jq

7.5 SECURITY
Effortlessly plugin authentication and authorization in a traditional or reactive application with Spring Security

@Bean
MapReactiveUserDetailsService users(){

return new MapReactiveUserDetailsService(User.withDefaultPasswordEncoder().username("user").password("pw").roles("USER").build());

}
訪問(wèn):curl -vu user:pw http://localhost:8080/customers | jq

7.6 TO PRODUCTION
Let"s provision a MongoDB instance,configure our application"s route and MongoDB binding,and then push our application to production with Cloud Foundry.

命令切換到bootiful根目錄下

cf services

定位到my-mongodb文件夾

復(fù)制對(duì)應(yīng)文件,修改和觀察



文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/77866.html

相關(guān)文章

  • 關(guān)于Spring Boot得不知道

    摘要:采用一套固化的認(rèn)知來(lái)建立生產(chǎn)環(huán)境準(zhǔn)備的應(yīng)用。我們采用一套關(guān)于固化平臺(tái)和第三包依賴庫(kù)的認(rèn)知,以至于你可以通過(guò)最小的煩惱來(lái)啟動(dòng)。大多數(shù)的應(yīng)用程序只需要非常少的配置。 1 Spring Boot官網(wǎng)[2.1.5 CURRENT GA] 1.1 Pivotal Wiki Pivotal Software, Inc. is a software and services company base...

    ygyooo 評(píng)論0 收藏0
  • 關(guān)于微服務(wù)得不知道——Spring Boot注解分析

    摘要:注解分析注解定義注解,用于為代碼提供元數(shù)據(jù)。我們可以將元注解看成一種特殊的修飾符,用來(lái)解釋說(shuō)明注解,它是注解的元數(shù)據(jù)。被修改的注解,結(jié)合可以指定該注解存在的聲明周期。新增的可重復(fù)注解。 Spring Boot 注解分析 1 注解1.1 定義Annotation(注解),用于為Java代碼提供元數(shù)據(jù)。簡(jiǎn)單理解注解可以看做是一個(gè)個(gè)標(biāo)簽,用來(lái)標(biāo)記代碼。是一種應(yīng)用于類、方法、參數(shù)、變量、構(gòu)造器...

    nevermind 評(píng)論0 收藏0
  • Spring Web

    摘要:認(rèn)證鑒權(quán)與權(quán)限控制在微服務(wù)架構(gòu)中的設(shè)計(jì)與實(shí)現(xiàn)一引言本文系認(rèn)證鑒權(quán)與權(quán)限控制在微服務(wù)架構(gòu)中的設(shè)計(jì)與實(shí)現(xiàn)系列的第一篇,本系列預(yù)計(jì)四篇文章講解微服務(wù)下的認(rèn)證鑒權(quán)與權(quán)限控制的實(shí)現(xiàn)。 java 開(kāi)源項(xiàng)目收集 平時(shí)收藏的 java 項(xiàng)目和工具 某小公司RESTful、共用接口、前后端分離、接口約定的實(shí)踐 隨著互聯(lián)網(wǎng)高速發(fā)展,公司對(duì)項(xiàng)目開(kāi)發(fā)周期不斷縮短,我們面對(duì)各種需求,使用原有對(duì)接方式,各端已經(jīng)很...

    Kosmos 評(píng)論0 收藏0
  • Nacos系列:基于Nacos配置中心

    摘要:殺只雞而已,你拿牛刀來(lái)做甚釋義小團(tuán)隊(duì)小項(xiàng)目選擇簡(jiǎn)單的配置管理方式就好了,要什么配置中心,純屬?zèng)]事找事。,我就啰嗦到這里吧,下面正式介紹作為配置中心是怎么使用的。 前言 在看正文之前,我想請(qǐng)你回顧一下自己待過(guò)的公司都是怎么管理配置的,我想應(yīng)該會(huì)有以下幾種方式: 1、硬編碼沒(méi)有什么配置不配置的,直接寫在代碼里面,比如使用常量類優(yōu)勢(shì):對(duì)開(kāi)發(fā)友好,開(kāi)發(fā)清楚地知道代碼需要用到什么配置劣勢(shì):涉及秘...

    ralap 評(píng)論0 收藏0
  • SpringBoot 實(shí)戰(zhàn) (一) | 如何使用 IDEA 構(gòu)建 Spring Boot 工程

    摘要:它使用約定大于配置的理念讓你的項(xiàng)目快速運(yùn)行起來(lái)。如何使用構(gòu)建工程第一步,當(dāng)然是安裝傻瓜式教程,請(qǐng)自行百度。包名,填完和后自動(dòng)生成,默認(rèn)即可。確認(rèn)無(wú)誤,點(diǎn)完成創(chuàng)建即可。 微信公眾號(hào):一個(gè)優(yōu)秀的廢人如有問(wèn)題或建議,請(qǐng)后臺(tái)留言,我會(huì)盡力解決你的問(wèn)題。 前言 新年立了個(gè) flag,好好運(yùn)營(yíng)這個(gè)公眾號(hào)。具體來(lái)說(shuō),就是每周要寫兩篇文章在這個(gè)號(hào)發(fā)表。剛立的 flag 可不能這么快打臉。下面送上本周第...

    Ryan_Li 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<