目录
1.起步依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
data-jpa,thymeleaf,
2.自动配置
Spring Boot的自动楝涵盖安全、集成、持久化、Web开发等东西
3.应用程序
(1)领域模型(JPA实体),@Entity,id属性有@Id和@GeneratedValue注解,说明这个字段是实体的唯一标志,自动生成值
(2)定义仓库接口
继承JpaRepository接口
Spring Data只需定义仓库接口,在应用程序启动后,该接口在运行时会自动实现(如何实现呢)
(3)创建Web界面
一个Controller类,@Controller和@RequestMapping(“路径”)
方法:调用(2)的方法
@RequestMapping(value = "/{XXX}", method = RequestMethod.GET)
@RequestMapping(value = "/{XXX}", method = RequestMethod.POST)
(4)thymeleaf 模板的html页面
路径即(3)提到的路径
4.总结
一个自动配置的Spring Boot程序必备:有JPA实体,Jpa仓库接口,响应页面请求的Controller类,html页面
5.注释要点
@Entity
@Documented
@Target(TYPE)
@Retention(RUNTIME)
public @interface Entity {
/**
* (Optional) The entity name. Defaults to the unqualified
* name of the entity class. This name is used to refer to the
* entity in queries. The name must not be a reserved literal
* in the Jakarta Persistence query language.
*/
String name() default "";
}
@Controller
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}