Spring-boot:使用自动配置写一个程序

目录

1.起步依赖

2.自动配置

3.应用程序


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 "";

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值