用SpringBoot搭建简易电商项目,SpringBoot的首先个

作者:编程

这一节关键是讲springboot搭建简易的web项目。

前几节呢,大家早就简介了SpringBoot框架的行使,从这一节早先,大家尝试着使用SpringBoot框架来一步一步搭建八个简微单商项目。当然了,那不是确实的电商项目,你能够作为是贰个CRUD案例,只是采用到了SpringBoot框架而已。

宏观简单介绍:

首先pom文件新扩充spring-boot-starter-web依赖,pom文件如下所示

开采工具:eclipse数据库:MySQL

Spring Boot是由Pivotal共青团和少先队提供的全新框架,其安插目标是用来简化新Spring应用的启幕搭建以及开荒进程。该框架使用了特定的方法来展开布局,进而使开垦职员不再须求定义样板化的安排。通过这种方法,Spring Boot致力于在旭日东升的急忙利用开辟世界(rapid application development)成为官员。

<?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>com.dalaoyang</groupId> <artifactId>springboot_web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_web</name> <description>springboot_web</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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-devtools</artifactId> <scope>runtime</scope> </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>

图片 1图片 2图片 3图片 4

自己的个体观念:spring boot正是用来简化spring的浩大常用而又复杂的布局的。八个用spring框架搭建的体系,往往需求多多依赖,相同的时候还或然会用到tomcat只怕jetty等web容器,并且还要配置许多xml文件,那一个弄好后才到写作业代码的级差。而spring boot将这么些都整合在一块儿了,只须求通过maven配置spring boot的信任和插件就曾经有了非常多默许的布局了,当然那几个安插也是能够修改的。

下一场在品种内新建IndexController,代码如下

您能够见见采纳的pom文件是报错的,让大家开荒pom文件一探毕竟。

搭建spring boot项目有3种形式,Gradle、Maven、IDE。最简易的法子是选拔IDE来营造。在AMDliJ IDEA中,提供了搭建springboot项指标意义,在创立项目时精选:

package com.dalaoyang.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.controller * @email 397600342@qq.com * @date 2018/3/13 */@RestControllerpublic class IndexController { @RequestMapping public String index(){ return "hi dalaoyang"; }}

图片 5

图片 6选择spring initializr.png

起步类SpringbootWebApplication代码如下

实属缺点和失误了web.xml文件,那么咱们手动去加多一下。

在选用spring boot版本以及组件的分界面上勾选web。

package com.dalaoyang;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringbootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringbootWebApplication.class, args); }}

图片 7

图片 8勾选web.png

安顿文件如下

在那边新建贰个WEB-INF文件夹:

项目建好之后,会活动营造项目组织以及pom.xml文件

##设置端口号server.port=8888##springboot项目默认访问是/ 默认不配置下面属性的话 可以直接通过http://ip:port/可以访问index##如果在配置文件中增加如下配置 则访问时需要访问http://ip:port/dalaoyangserver.context-path=/dalaoyang

图片 9再新建多少个web.xml文件:图片 10

<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.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-version>4.3.0.RELEASE</spring-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-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>

仿佛此二个粗略的springboot web项目搭建完结。

噢耶,pom文件不报错啦!

鉴于事先创制项目时勾选了web,所以编写翻译器已经暗许地在pom.xml中投入了spring的web相关组件,不用本身再修改pom.xml了,等待种种依赖下载实现后,就足以起来编写制定工作代码了。

接下来运维项目 采访

web.xml

会发觉src下有多少个自动生成的类

图片 11image

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>java520.top</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
@SpringBootApplicationpublic class SpringboottestApplication{ public static void main(String[]args){ SpringApplication.run(SpringboottestApplication.class,args); }}

源码下载 :大老杨码云

在pom文件中增添上SpringBoot的父信赖:

以此类是springboot项目运维的入口。然后先随意写个controller,写法和后面常用的springMVC未有分化。

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent>
@RestControllerpublic class HelloController{ @GetMapping public Object say(){ return "hello!!!"; }}

保存,可以见到Maven在机动下载jar包和pom文件了。

接下来即可运营了,没有错,就足以运作了。

图片 12

从未配备tomcat,未有铺排怎么样despatcherservlet,未有安插任何包扫描,数据格式转变器等等,以至一个xml文件都尚未。springboot内置tomcat和jetty,默许使用tomcat。

再增加上编码和jdk的配置:

图片 13拜访结果.png

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties>

自然曾经在xml里能配置的事物,用springboot的时候也足以配备,可是它造成了更讨人喜欢的properties文件。

还有jar包依赖:

图片 14计划文件.png

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>

品味配置一下tomcat的端口号:

插件注重:

server.port = 80
 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

实际spring boot的安顿文件还大概有种更加美观的格式 yml:

在java源文件夹中创设贰个开发银行类:

server: port: 80

图片 15

不仅仅yml格式越来越好掌握,而且在idea编译器中对yml格式还恐怕有properties未有的提醒意义。

代码:

图片 16提示.png

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}

因而提议选拔yml格式。

运营,发掘报错:

切实的配置项能够查看官方文书档案,不过暗许的配备已然是我们常用的安顿了。所以自身也没有需求改太多配备。

rg.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [App]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/social/config/annotation/SocialConfigurerAdapter.class] cannot be opened because it does not exist at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE] at App.main(App.java:8) [classes/:na]Caused by: java.io.FileNotFoundException: class path resource [org/springframework/social/config/annotation/SocialConfigurerAdapter.class] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:50) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:102) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:89) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE] at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:76) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE] at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:693) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getSuperClass(ConfigurationClassParser.java:857) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:328) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:190) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:292) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE] ... 13 common frames omitted

初探springboot,其优点就显现出来了,让自个儿很欢快,笔者深信不疑它必然还会有其余更美观的位置,值得深切钻研。

百度了一晃,大致意思好疑似无法在暗中同意的包里面运营,得有叁个切实可行的package。ok,那大家就给她一个package。

图片 17

再配上布署文件:

图片 18

server: port: 80 context-path: /

运维后报错:

***************************APPLICATION FAILED TO START***************************Description:Cannot determine embedded database driver class for database type NONEAction:If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

大概的乐趣啊,正是说springboot自动帮大家注入DataSource了,而小编辈在yml文件之中还并不曾布署数据库的接连音讯,所以就报错了。

近年来,大家健全一下yml文本:

server: port: 80 context-path: / spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/db_shop username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true 

数据库选拔小编本地的MySQL再次启航,终于不报错了。未来,新建一个调整器:

图片 19图片 20

测验代码:

package com.java520.top.shop.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController { @RequestMapping("/getUserById") public String getUserById(){ return "剽悍一小兔"; }}

运行项目,访谈:

图片 21

成功了,至此,大家的类型搭建完结。

本文由www.204.net发布,转载请注明来源

关键词: www.204.net