亲自动手搭建微服务框架和测试环境-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

指定使用静态名称的查询。


猜你喜欢

转载自blog.51cto.com/5526964/2426372