親自動手搭建微服務框架和測試環境-6-Spring Boot

1版本說明

GA= General Availability正式發佈版本,官方推薦使用此版本

RCx=Release Candidate:候選版本,即將成爲正式發佈

SNAPSHOT: 快照版,可以穩定使用,仍在繼續改進

Mx=stands for milestone:里程碑版本,開發版本,並不穩定

1.x:最新發布版1.5.17 GA,發佈日2018.10.16

2.x:最新發布版爲2.0.6GA,發佈日2018.10.16

 

版本依賴關係如下:

Spring boot 版本

Spring Framework

jdk 版本

maven 版本

1.2.0 版本之前


6

3

1.2.0

4.1.3+

6

3.2+

1.2.1

4.1.3+

7

3.2+

1.2.3

4.1.5+

7

3.2+

1.3.4

4.2.6+

7

3.2+

1.3.6

4.2.7+

7

3.2+

1.3.7

4.2.7+

7

3.2+

1.3.8

4.2.8+

7

3.2+

1.4.0

4.3.2+

7

3.2+

1.4.1

4.3.3

7

3.2+

1.4.2

4.3.4

7

3.2+

1.4.3

4.3.5

7

3.2+

1.4.4

4.3.6

7

3.2+

1.4.5

4.3.7

7

3.2+

1.4.6

4.3.8

7

3.2+

1.4.7

4.3.9

7

3.2+

1.5.0

4.3.6

7

3.2+

1.5.2

4.3.7

7

3.2+

1.5.3

4.3.8

7

3.2+

1.5.4

4.3.9

7

3.2+

1.5.5

4.3.10

7

3.2+

1.5.7

4.3.11

7

3.2+

1.5.8

4.3.12

7

3.2+

1.5.9

4.3.13

7

3.2+

2.0.0

5.0.2

8

3.2+

 

2下載安裝

Eclipse 4.8.0àHelpàEclipse MarketPlace

選擇PopularàSpring Tools 3 Add-on

image.png



安裝完後重啓生效。

3依賴

1Java 8或者9

2Spring Framewok 5.0.10.RELEASE或者以上版本

3、構建採用maven3.2+或者gradle4.x

4servlet容器採用tomcat8.5或者jetty9.4或者undertow1.4servlet版本3.1

4源碼

1SpringBoot源碼位置:

https://github.com/spring-projects/spring-boot

命令行編譯可以少很多問題,進出現一次JDK問題(見7maven的問題2),如下:

D:\eclipse\workspace\spring-boot\spring-boot-project\spring-boot> mvn clean install -Dmaven.test.skip=true

image.png


eclipse中編譯出現較多問題:

1Repository Path 'D:\eclipse\workspace\spring-boot\spring-boot-project\spring-boot-starters\spring-boot-starter\target\classes' does not exist.

Classes缺少,增加即可

2Repository Path ' D:\eclipse\workspace\spring-boot\spring-boot-project\spring-boot-starters\spring-boot-starter-logging\target\classes' does not exist.

Classes缺少,增加即可

3copy (include-loader-jar) on project spring-boot-loader-tools: Artifact has not been packaged yet. When used on reactor artifact, copy should be executed after packaging: see MDEP-187.

不怎麼影響使用,把maven庫的包刪除(D:\maven-repository\org\apache\maven\plugins\ maven-dependency-plugin),重新下載即可

(4) test (default-test) on project spring-boot: There are test failures.

執行maven install時需要測試代碼,如果測試有錯誤,則報此錯誤,解決辦法是:

<artifactId>maven-surefire-plugin</artifactId><configuration>增加一行:

<configuration>

<testFailureIgnore>true</testFailureIgnore>

</configuration>

 

另外一個辦法是找到測試失敗的原因,提示:sslWantsClientAuthenticationSucceedsWithoutClientCertificate(org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactoryTests)  Time elapsed: 0.022 s  <<< ERROR!

java.lang.IllegalArgumentException: The location [C:\Users\ADMINI~1\AppData\Local\Temp\tomcat.14722600924658181848.0] specified for the base directory is not a directory

這個目錄確實不存在了,手工新建一個。

如果在程序中新建,可以使用:

@Bean

    MultipartConfigElement multipartConfigElement() {

        MultipartConfigFactory factory = new MultipartConfigFactory();

        factory.setLocation("C:\Users\ADMINI~1\AppData\Local\Temp\ tomcat.14722600924658181848.0");

        return factory.createMultipartConfig();

}

只是在此處不適合。

5Failed to clean project: Failed to delete D:\eclipse\workspace\spring-boot\spring-boot-project\spring-boot-tools\target

進入這個目錄提示:

image.png


退出eclipse(等eclipse進程退完全),這個目錄已經消失,重新進eclipse錯誤就沒有了。

同樣的錯誤發生在spring-boot-configuration-processor等多個項目,還存在一種情況:在資源管理器中發現該目錄正常,手工刪除後解決(後來發現子目錄有問題,無法進去)。

具體原因未知,可能和磁盤有關。

 

2Spring Framework源碼位置:

https://github.com/spring-projects/spring-framework

 

3Spring Initializr源碼位置:

https://github.com/spring-io/initializr

 

2.1.5配置

1、快速生成

快速創建一個SpringBoot項目可以使用Spring Initializr,有現成的,如下網址:

https://start.spring.io/

 

2、配置文件

先加載application.yml,再加載Application.propertiesproperties的配置會覆蓋yml的配置,也就是說yml的配置優先級低。

1mongodb配置

spring.data.mongodb.uri=mongodb://edison:123@localhost:27017/education

Mongo 2.x使用:

spring.data.mongodb.host=localhost

spring.data.mongodb.port=27017

spring.data.mongodb.username=edison

spring.data.mongodb.password=123

spring.data.mongodb.authentication-database=education

Mongo 3.0不支持:

spring.data.mongodb.host=localhost

spring.data.mongodb.port=27017

 

2)日誌配置

#logging.pattern.console="%d - %msg%n"

logging.pattern.file='%d ***** %msg %n'

logging.path=d:/logs/

#logging.file='test.log'

最後一句無效,原因未知。

6啓動

eclipseànewàproject中新建一個demo項目,如下:

image.png


Next>後如下:

image.png


修改項目信息,繼續點下一步後:

image.png


選擇依賴後點下一步:

image.png


這是總結,點finish後自動生成項目,需要自動下載包,建議設置maven mirror到阿里雲。

 

項目右鍵àdebug asàmaven build會先後出現兩個問題,參見第7maven問題1、問題2;可以直接項目右鍵à debug asàSpring boot app啓動,不會出現上面兩個問題。

 

在瀏覽器chrome中輸入http://localhost:8080,如下:

image.png


增加一個控制器,如下

image.png


在瀏覽器中驗證,如下:


image.png



7問題

問題1、無法找到SpringApplication

第一種方法:不使用eclipse自帶的maven,建立新的本地倉庫,運行maven install後解決

第二種方法:把SpringBoot的版本從2.0.6改爲2.0.5

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.5.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

 

 

問題2、無法引入org.springframework.boot.autoconfigure.SpringBootApplication

刪除本地倉庫的autoconfigure,運行項目右鍵菜單mavenàUpdate project

 

問題3、無法引入@RestController@RequestMapping

eclipse點擊代碼右鍵按提示操作解決

 

問題4、無法引入@SpringBootApplication

刪除本地倉庫的autoconfigure,運行項目右鍵菜單mavenàUpdate project

問題5:不能讀取application.properties

application.properties或者application.yml移到config目錄中,有人認爲放在resources目錄中實際上是錯的。

官方文檔說放在以下四個目錄:

1)當前目錄的 “/config”的子目錄下

2)當前目錄下

3classpath根目錄的“/config”包下

4classpath的根目錄下

 

問題6:無法訪問@RequestMapping中指定的鏈接

image.png


原因1Application類和Controller類不再一個包中,解決辦法:Application類和Controller類放到一個包中

 

問題7Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService' ……Validation failed for query for method public abstract java.util.List com.edison.dblib.UserRepository.findUser(java.lang.String)!

原因是JPA使用的Hibernate需要傳送java中的model名字,而不是使用表名,因此需要把:

    @Query("select id,username,sex,address from User u where u.username=:name")

    List<UserModel> findUser (@Param("name") String username);

改爲:

    @Query("select id,username,sex,address from UserModel u where u.username=:name")

    List<UserModel> findUserModel(@Param("name") String username);

問題8Consider defining a bean of type 'com.edison.dblib.UserDao' in your configuration

Description:

Field userDao in com.edison.dblib.CustomUserService required a bean of type 'com.edison.dblib.UserDao' that could not be found.

Action:

Consider defining a bean of type 'com.edison.dblib.UserDao' in your configuration.

解決:使用@Mapper註解

//@Repository

@Mapper

public interface UserDao {

public SysUser findByUserName(String username);

}

問題9The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.

解決辦法:數據庫連接URL後,加上?serverTimezone=UTC


8邏輯圖

image.png


9 Starter

啓動器

功能

POM

spring-boot-starter

核心啓動器:自動配置支持,日誌,YAML

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

spring-boot-starter-activemq

基於Apache ActiveMQ支持JMS消息

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-activemq</artifactId>

spring-boot-starter-amqp

支持Spring AMQPRabbitMQ

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-aop

基於Spring AOPAspectJ實現AOP

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-artemis

基於Apache Artemis支持JMS消息

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-batch

支持Apache Batch

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-cache

支持Spring Frameworkcache

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-cloud-connectors

支持Spring Cloud Connectors,簡化到雲平臺的連接,例如Cloud FoundryHeroku

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-cassandra

支持Spring Data CassandraCassandra分佈式數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-cassandra-reactive

支持Spring Data Cassandra ReactiveCassandra分佈式數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-couchbase

支持Spring Data CouchbaseCouchbase文檔型數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-couchbase-reactive

支持Spring Data Couchbase ReactiveCouchbase文檔型數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-elasticsearch

支持Spring Data ElasticsearchElasticsearch搜索和分析引擎

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-jpa

支持基於HibernateSpring Data JPA

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-ldap

支持Spring Data LDAP

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-mongodb

支持Spring Data MongoDBMongoDB文檔型數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-mongodb-reactive

支持Spring Data MongoDB ReactiveMongoDB文檔型數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-neo4j

支持Spring Data Neo4jNeo4j圖形數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-redis

支持Spring Data redisredis鍵值數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data- redis-reacive

支持Spring Data redis reactiveredis內存數據庫

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-rest

支持Spring Data rest和基於RESTSpring Data數據倉庫輸出

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-solr

支持Spring Data solrsolr搜索引擎

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-data-freemarker

基於freemarker視圖構建MVC web應用

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-groovy-templates

基於groovy templates視圖構建MVC web應用

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-hateosa

基於Spring MVC Spring HATEOSA構建基於RESTful的超媒體web應用

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-integration

支持Spring integration

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-jdbc

支持基於HikariCP連接池的JDBC

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-jersey

基於JAX-RS Jersey構建基於RESTfulweb應用。可替代 spring-boot-starter-web

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-jooq

基於jOOQ訪問SQL數據庫。可替代spring-boot-starter-jdbc或者spring-boot-starter-data-jpa

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-json

讀寫JSON

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-jta-atomikos

基於AtomikosJTA分佈式事務

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-jta-bitronix

基於BitronixJTA分佈式事務

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-jta-narayana

基於narayanaJTA分佈式事務

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-mail

基於Spring Framework email sending支持JAVA Mail

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-mustache

基於mustache視圖構建web應用

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-quartz

基於quartz實現任務調度器

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-security

支持Spring Security

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-test

基於JunitHamcrest或者Mockito實現Spring Boot應用的測試

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-thymeleaf

基於thymeleaf視圖構建MVC web應用

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-validation

基於Hibernate Validator實現Java Bean校驗

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-web

基於Spring MVC構建RESTful風格的web應用,使用內嵌tomcat作爲默認容器

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-web-services

使用Spring Web Services

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-webflux

基於Spring FrameworkReactive Web功能構建Webflux風格的web應用

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-websocket

基於Spring FrameworkWebsocket功能構建WebSocket應用

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-actuator

驅動器,用於生產準備,提供應用程序監控和管理功能。不同於此條目之上的應用程序啓動器,這是生產啓動器,唯一

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-jetty

技術啓動器。使用jetty作爲內嵌servlet容器。可代替spring-boot-starter-tomcat

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-log4j2

技術啓動器。使用log4j2作爲日誌組件。可代替spring-boot-starter-logging

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter- logging

技術啓動器。使用Logback作爲日誌組件。默認的日誌啓動器

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-reactive-netty

技術啓動器。使用Reactive Netty作爲內嵌反應式Http服務器

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-tomcat

技術啓動器。使用tomcat作爲內嵌servlet容器。被spring-boot-starter-web使用的默認servlet容器

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>

spring-boot-starter-undertow

技術啓動器。使用undertow作爲內嵌servlet容器。可替代spring-boot-starter-tomcat

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-</artifactId>


10 Annotation

1Boot註解

註解

解釋

@SpringBootApplication

自動包括@Configuration@EnableAutoConfiguration@EnableWebMvc@ComponentScan。屬於Spring Boot註解。

@SpringBootTest

SpringBoot自1.4.0版本開始引入的一個用於測試的註解classes屬性指定啓動類,SpringBootTest.WebEnvironment.RANDOM_PORT經常和測試類中@LocalServerPort一起在注入屬性時使用。會隨機生成一個端口號。

@SpringBootConfiguration

繼承自@Configuration。在Spring Boot項目中推薦使用@SpringBootConfiguration替代@Configuration

@Configuration

標註應用程序上下文。等同於Spring Framework的XML配置文件。使用註解可以進行類型安全檢查

@EnableAutoConfiguration

自動配置。告訴SpringBoot開始增加基於classpath的bean,和具有不同屬性設置的其他bean

@EnableWebMvc

使能web應用,激活諸如設置DispatcherServlet等關鍵行爲。當SpringBoot發現classpath有spring-webmvc時,自動增加該標註

@ComponentScan

自動尋找Java包中存在的其他組件、配置和服務。可自動裝配bean。

@Component

可配合CommandLineRunner使用。在SpringBoot應用程序啓動後執行基礎任務。註解在類上,表示通用bean,value不寫表示默認類名首字母小寫。

@Service

業務層bean,註解在類上

@Repository

數據訪問層bean,註解在類上

@Controller

控制層bean,註解在類上

@ImportResource

調用XML配置文件

@Autowired

默認byType按類型注入(DI),默認屬性required=true,表示找不到匹配bean時自動拋出異常。和@Qualifier結合使用,則自動注入由byType變爲byName。註解在成員變量、方法和構造函數上。屬於Spring Framework

@Resource

默認byName按名稱注入(DI)。註解在成員變量、方法和構造函數上。屬於Java EE的註解

@Bean

聲明當前方法返回一個bean。註解在方法上。

@Scope

指定創建bean實例的方式。註解在類上。創建方式包括如下:

(1)singleton: 表示在spring容器中的單例,通過spring容器獲得該bean時總是返回唯一的實例。爲指定@Scope,默認此方式

(2)prototype:表示每次獲得bean都會生成一個新的對象

(3)request:表示在一次http請求內有效(只適用於web應用)

(4)session:表示在一個用戶會話內有效(只適用於web應用)

(5)globalSession:表示在全局會話內有效(只適用於web應用)

@value

從配置文件中讀取值。註解在變量上。例如:@Value(value = “#{message}”)

@ConfigurationProperties

給對象賦值,將註解轉換爲對象

@Profile

在不同情況下選擇實例化

@RestController

自動包括@Controller和@ResponseBody

@ResponseBody

返回結果直接寫入 HTTP 響應正文(ResponseBody)中,一般在異步獲取數據時使用。在@RequestMapping 後,返回值通常解析爲跳轉路徑,加上@Responsebody 後返回結果就從跳轉路徑改爲直接寫入HTTP 響應正文中。註解在方法中。

@Controller

用於定義控制器類,在spring項目中由控制器負責將用戶發來的URL請求轉發到對應的服務接口(service層)註解在類中配合@RequestMapping。

@RequestBody

該註解用於讀取Request請求的body部分數據,使用系統默認配置的HttpMessageConverter進行解析,然後把相應的數據綁定到要返回的對象上;再把HttpMessageConverter返回的對象數據綁定到 controller中方法的參數上。GET、POST和PUT判斷和處理不同。

@PathVariable

綁定Request參數到Controller方法的參數中。Required=false可以在value不存在時不拋出異常。URL:http://host:port/path/參數值

@RequestParam

綁定Request參數到Controller方法的參數中。Required=false可以在value不存在時不拋出異常。URL:http://host:port/path?參數名=參數值

@RequestMapping

屬性有value(默認),method,consumes,produces,params,headers。

Value:請求相對url地址

Method:方法類型,忽略則自動判定get或post

Consumes:提交的內容類型

Produces:返回的內容類型

Params:請求必須包含的參數

Headers:請求必須包含的頭

@GetMapping

@RequestMapping(method = RequestMethod.GET)

@PutMapping

@RequestMapping(method = RequestMethod.PUT)

@PostMapping

@RequestMapping(method = RequestMethod.POST)

@DeleteMapping

@RequestMapping(method = RequestMethod.DELTE)

@EnableCaching

加入Cache Manager

@SuppressWarning

移除警告

@Modifying

表示數據庫操作是增刪改。和@Transactional一起使用

@Query

指定自定義查詢語句。支持JPQL或者原生SQL

@RepositoryRestResource

配合spring-boot-starter-data-rest使用。Spring Data REST自動創建此接口的實現後,@RepositoryRestResource註解讓Spring MVC在path處創建RESTful入口點。

@RunWith


@AutoConfigureMockMvc


@Test


@AutoConfigureWebTestClient


@DirtiesContext


@MockBean


@JsonTest


@WebMvcTest


@WebFluxTest


@DataJpaTest


@AutoConfigureTestDatabase


@JooqTest


@DataMongoTest


@DataNeo4jTest


@DataRedisTest


@JdbcTest


@Import


@AutoConfigureRestDocs


@LocalServerPort


@TestConfiguration


@Override


@ImportAutoConfiguration


@EnableBatchProcessing


@ContextConfiguration


@Value


@PayloadRoot


@ResponsePayload


@Endpoint


@RequestPayload


@JsonCreator


@JsonProperty



2JPA註解

註解

解釋

@Entity

聲明類爲實體或表。

@Table

聲明表名。和@Entity一起使用,表示表名和實體名不同

@Basic

指定非約束明確的各個字段。

@Embedded

指定類或它的值是一個可嵌入的類的實例的實體的屬性。

@Id

指定的類的屬性,用於識別(一個表中的主鍵)。

@GeneratedValue

指定如何標識屬性可以被初始化,例如自動、手動、或從序列表中獲得的值。strategy=IDENTITY:自增長,strategy=AUTO:自動(默認),strategy=SEQUENCE:採用@SequenceGenerator的序列生成器,strategy=TABLE:使用表產生

@Transient

表示不持久,即:該值永遠不會存儲在數據庫中。沒有此註解則表示默認@Basic

@Basic

持久的。fetch=FetchType.LAZY表示加載方式

@Column

指定持久屬性欄屬性。屬性name:字段名,unique:是否唯一,nullable:是否空,Length:長度,insertable:是否可插入,updateable:是否可更新,columnDifinition:DDL,secondaryTable:從表名。

@Temporal

和@Column一起使用。屬性value:TemporalType.DATE:日期,TemporalType.TIME:時間,TemporalType.TIMESTAMP:時間戳

@JsonIgnore

作用是json序列化時將Java bean中的一些屬性忽略掉,序列化和反序列化都受影響。

@JsonIgnoreProperties

類註解,作用是json序列化時將java bean中的一些屬性忽略掉,序列化和反序列化都受影響

@JsonFormat

用於屬性或者方法上(最好是屬性上),可以方便的把Date類型直接轉化爲我們想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")

@JsonSerialize

此註解用於屬性或者getter方法上,用於在序列化時嵌入我們自定義的代碼,比如序列化一個double時在其後面限制兩位小數點。

@JsonDeserialize

此註解用於屬性或者setter方法上,用於在反序列化時可以嵌入我們自定義的代碼,類似於上面的@JsonSerialize

@JsonManagedReference

放在父類中

@JsonBackReference

放在子類中

@SequenceGenerator

指定在@GeneratedValue註解中指定的屬性的值。它創建了一個序列。

@TableGenerator

指定在@GeneratedValue批註指定屬性的值發生器。它創造了的值生成的表。

@AccessType

這種類型的註釋用於設置訪問類型。如果設置@AccessType(FIELD),則可以直接訪問變量並且不需要getter和setter,但必須爲public。如果設置@AccessType(PROPERTY),通過getter和setter方法訪問Entity的變量。

@JoinColumn

指定一個實體組織或實體的集合。這是用在多對一和一對多關聯。

@UniqueConstraint

指定的字段和用於主要或輔助表的唯一約束。

@ColumnResult

參考使用select子句的SQL查詢中的列名。

@ManyToMany

定義了連接表之間的多對多一對多的關係。對應hibernate配置文件

@ManyToOne

定義了連接表之間的多對一的關係。對應hibernate配置文件

@OneToMany

定義了連接表之間存在一個一對多的關係。對應hibernate配置文件

@OneToOne

定義了連接表之間有一個一對一的關係。對應hibernate配置文件

@NamedQueries

指定命名查詢的列表。

@NamedQuery

指定使用靜態名稱的查詢。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章