Spring Boot快速构建我们的开发
来源:互联网 发布:淘宝加入购物车不提示 编辑:程序博客网 时间:2024/06/11 14:40
相信现在很多公司都在用Spring、SpringMVC框架,然而在使用这些框架的时候,我们或多或少都需要配置一下xml,Spring boot让这一切都便得简单,只需要我们使用一个命令mvn spring-boot:run就可以把一个web项目启动起来,但是在创建spring-boot项目,我们必须安装maven、gradle这种构建工具,这里我以maven为例子来构建第一个Spring boot项目:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<!-- -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- DAO层,spring data jpa ,大大简化了我们的dao层开发 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 前台模板thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 数据库配置 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
从配置文件中我们可以看出,我们的WEB应用无非就是DAO层、Service层、Controller层等,上面这个配置文件也已经把我们的DAO层做好了,我们DAO层次之需要继承Spring data jpa的JpaRepository接口,dao层就出来了,那么看看我们的实体层:
实体
package demo;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Account {
@Id
@javax.persistence.GeneratedValue()
private Long id;
private String name;
private String email;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
控制层代码:
package demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WelcomeController {
@RequestMapping("/")
public String welcome(){
return "welcome";
}
}
DAO层“:
package demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AccountRepository extends JpaRepository<Account,Long> {
}
视图层:
welcome.html
启动类:
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
启动类我们只需要注解,@component@ service,@Repository @Controller将自动注册为Spring bean或自动注入spring组件包括 @Configuration类*/,
@ComponentScan开启组件扫描,@EnableAutoConfiguration 开启自动配置,开启之后,SpringBoot自动去注册DispatcherServlet,再也不需要我们
去配置web.xml等。
现在我们直接去启动过一个项目,用mvn spring-boot:run 启动,Spring会自动给我们装载一个tomcat,我们直接访问:localhost:8080就可以看到我们
的页面,做为一个新手我们可以去http://start.spring.io/,这里是Spring的官网,我们只需要选择前台模板和DAO层技术,如果你只想演示一个demo不选择任何也可以
本例子选择了thymeleaf和JPA,spring会自动下载一个你想的项目,下载回来之后,我们需要在pom.xml添加数据库配置:
<!-- 数据库配置 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
还需要在templates建立一个模板页面,否则会出错,使用thymeleaf则建立一个html的静态页面即可。然后用mvn spring-boot:run 就可以直接跑我们的项目了
如果有疑问加群:319404523,欢迎大家加入讨论
- Spring Boot快速构建我们的开发
- 快速构建Spring Boot项目
- 使用spring boot快速构建spring框架的maven项目
- Spring boot快速开发
- 采用Gradle快速构建基于Spring boot的MVC应用
- 使用Spring Boot快速构建基于SQLite数据源的应用
- 使用Spring Boot快速构建基于SQLite数据源的应用
- 使用Spring Boot快速构建基于SQLite数据源的应用
- 关于spring boot - 快速构建spring程序
- Java日志-Spring Boot(1)idea快速构建Spring Boot
- 使用Spring Boot快速构建应用
- 使用Spring Boot快速构建应用
- Spring Boot框架快速构建应用
- 使用Spring Boot快速构建应用
- 使用Spring Boot快速构建应用
- 使用 Spring Boot 快速构建应用服务
- 使用Spring Boot快速构建应用
- spring boot快速构建权限系统
- Problem 026——UVa 12108 - Extraordinarily Tired Students
- 如何解决热点数据更新问题
- poj 1178 Camelot
- POJ 3176-Cow Bowling(dp_数字三角形)
- 最近做的一个小工具
- Spring Boot快速构建我们的开发
- 利用反射动态调用类成员(1)
- BUG
- 飞行器的分类四轴飞行器实践教程大学霸
- 小集群上SGE作业系统初探
- Android性能优化
- windows tasklist不能用
- freetype提取字模
- VM10安装CentOS 6