创建一个SpringBoot项目

前序:在Spring Boot出现之前,Spring项目会存在多个配置文件,比如web.xml,像SSM项目,会有很多xml.SpringBoot的出现就是简化这些xml,现在只需要配置一个application.properties,就会移除我们以前的很多烦恼,作为一个pring Boot 并不是用来替代 Spring 的解决方案,而是和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具。同时它集成了大量常用的第三方库配置,Spring Boot应用中这些第三方库几乎可以是零配置的开箱即用(out-of-the-box),大部分的 Spring Boot 应用都只需要非常少量的配置代码(基于 Java 的配置),开发者能够更加专注于业务逻辑。

现在SpringBoot已经研发到2.0,我们就创建一个2.0项目,一旦上手,开发非常快,SpringBoot是基于maven开发,有很多第三方查价。SpringBoot提供了一系列Starter,你可以自己开发自己的Starter然后导进去自己的Maven仓库

名称作用
spring-boot-starter-webWeb开发支持,自带tomcat
spring-boot-starter-aopAop开发支持,使用AspectJ
spring-boot-starter-jdbcSpring JDBC
spring-boot-starter-data-jpaJPA方式访问数据库,使用HIbernate作为JPA实现
spring-boot-starter-data-elasticsearch集成Elasticsearch,默认访问localhost:9200
spring-boot-starter-data-data-redis集成Redis,使用JRedis,默认连接localhost:6379
spring-boot-starter-cache缓存,支持多种缓存方式,如本地、redis、Ehcache等
spring-boot-devtools应用程序快速重启的工具,提升开发体验
spring-boot-starter-data-mongodb集成MongoDB,默认访问mongodb://localhost/test
spring-boot-starter-data-neo4j集成neo4j,默认访问localhost:7474
spring-boot-starter-data-gemfire集成分布式缓存
spring-boot-starter-data-solr基于Apache lucene的搜索平台,默认访问http://localhost:8983/solr
spring-boot-starter-data-cassandra集成neo4j,默认访问localhost:7474
spring-boot-starter-data-ldap集成Idap
spring-boot-starter-activemq消息集成ActiveMQ支持
spring-boot-starter-amqp消息集成AMQP协议支持,如支持RabbitMQ
spring-boot-starter-jta-atomikos分布式事务支持,使用atomikos
spring-boot-starterjta-bitronix一个开源的分布式事务支持
spring-boot-starter-test包含JUnit、SpringTest、Hamcrest、Mockito等测试工具
spring-boot-starte-webserviceswebservice支持
spring-boot-starte-websocketwebsocket支持
spring-boot-starte-jerseyREST应用和Jersey支持
spring-boot-starte-freemarkerFreemaker支持

一、hello SpringBoot


开发工具eclipse/idea,听说Idea现在非常火,但是本人用eclipse用的非常久了,用着非常顺手,习惯一个新的开发工具,要熟练掌握很费事。需要Maven、JDK8这些都还没安装,你就去安装吧。

1.1 创建一个Mave工程


不会新建Maven项目的建议学习下Maven
先新创建一个Maven项目new—项目—maven—Maven Project,会创建好一个新的pom文件
在这里插入图片描述

导入我们需要的第三方工具

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.yuyi</groupId>
	<artifactId>springBoot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springBoot</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web-services</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.3</version>
		</dependency>
		<!-- springboot websocket -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<packaging>war</packaging>
</project>

1.2 Hello SpringBoot


创建一个Application.java,这个类放在最外层,这样启动的时候会全部扫到

package com.yuyi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application{

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

创建一个Controller

package com.yuyi.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author lcc
 * @data :2018年8月22日 下午3:25:13
 */
 /** 
  @Controller 是返回页面 
  @RestController  是返回的JSON
 */
 
@RestController
public class IndexController {
    
    @RequestMapping("login")
    public String login(){
        return "hello SpringBoot";
    }
    
}

这个时候启动我们刚刚创建的Application.java 访问http://localhost:8080/login ,如图片所示启动成功,要是有其他错误,可以留言访问
在这里插入图片描述

在这里插入图片描述

好了一个基本的SpringBoot项目创建成功

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值