SpringBoot项目实战,附源码

左手的ㄟ右手 2022-10-17 10:55 323阅读 0赞

SpringBoot2.0笔记

(一)SpringBoot基本操作——环境搭建及项目创建(有demo)

(二)SpringBoot基本操作——使用IDEA打war包发布及测试

(三)SpringBoot基本操作——SpringBoot整合SpringDataJpa(有demo)

(四)SpringBoot基本操作——SpringBoot使用RedisTemplate整合Redis(有demo)

(五)SpringBoot基本操作——SpringBoot使用Jedis整合Redis(有demo)

(六)SpringBoot基本操作——SpringBoot使用Junit4单元测试(有demo)

(七)SpringBoot基本操作——SpringBoot整合Shiro权限管理(完整demo+界面)

本文使用idea工具构建Springboot2.0+SpringMvc+Thymeleaf+SpringDataJPA+MySql+Redis项目

GitHub地址:https://github.com/jwwam/springbootDemo.git

此demo可直接下载运行,以下为具体创建项目过程步骤说明和遇到问题解决方法。

一、构建SpringBoot项目

1.使用idea创建Springboot项目,选择SpringInitializr选好jdk版本点击下一步

70

2.填写项目包名及java版本

70 1

3.勾选你需要的项目组件,Springboot版本我使用2.0

Lombok可以简化代码量,提供注解方式代替重复性代码,非必须,用不用看情况,有时候很方便,新手不建议用

70 24.创建web项目所以勾选Web

70 3

5.模板语言勾选Thymeleaf,这个没得说,springboot官方推荐,确实很好用,当然是指3.0版本,老版本性能有短板

70 4

6.数据库持久化推荐使用JPA方式,MySQL看情况选择

70 5

7.Nosql我这里使用Redis,看你需要,可不勾选,后续需要时再引入

70 6

8.MQ可根据自己情况选择,我这里不需要,最后I/O需要一个发邮件的服务,不用的话也不需要勾选,可后续再引入

70 7

9.下一步填写项目文件名称

70 8

10.Finish后进入项目,然后漫长的等待一段时间后项目会构建完成

70 9

二、启动SpringBoot项目

1.项目加载完毕之后打开启动器DemoApplication右键run可直接启动项目,或者右上方直接启动DemoApplication

70 10

2.不出意外的话可以直接启动不会报错,没啥好说的,我列一下可能会出现的问题,这些问题基本都是由于前面勾选项目组件(即jpa,mysql等)时引入了需要启动注入的一些依赖或数据源缺失引起的和Springboot版本也有很大的关系,以下报错不一定会出现。

(1)启动报错:Failed to configure a DataSource: ‘url’ attribute is not specified and no embedd

问题原因:新项目未做相关数据源配置, 启动springboot找不到配置引起

解决办法(选一即可):

① 启动类DemoApplication加入注解@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

禁止自动加载数据源配置

② 手动添加数据源,在application.properties加入以下配置:

  1. #数据源
  2. spring.datasource.url=jdbc:mysql://localhost:3306/demo_test?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true
  3. spring.datasource.username=root
  4. spring.datasource.password=1234
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

(2)启动报错:At least one JPA metamodel must be present!

问题原因:Springboot自动加载持久化bean,即自动加载jpa的bean造成

解决办法(选一即可)

① 启动类DemoApplication加上以下注解

  1. @EnableAutoConfiguration(exclude={
  2. JpaRepositoriesAutoConfiguration.class //禁止springboot自动加载持久化bean
  3. })

② pom加入或更改jpa启动包,按照以下格式

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-jpa</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.hibernate</groupId>
  7. <artifactId>hibernate-entitymanager</artifactId>
  8. </exclusion>
  9. <exclusion>
  10. <groupId>org.hibernate</groupId>
  11. <artifactId>hibernate-core</artifactId>
  12. </exclusion>
  13. </exclusions>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.hibernate</groupId>
  17. <artifactId>hibernate-core</artifactId>
  18. <version>5.2.10.Final</version>
  19. </dependency>

pom重新导入就好了,启动项目也不再报错。

70 11

三、Hello World页面显示及Json数据回传

1.增加一个测试控制层和页面,添加测试类FirstController.java

  1. package com.springboot.demo.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import javax.servlet.http.HttpServletRequest;
  5. /** * @ClassName: FirstController * @Auther: zhangyingqi * @Date: 2018/8/23 17:53 * @Description: 测试页面跳转 */
  6. @Controller
  7. @RequestMapping(value="/first")
  8. public class FirstController {
  9. @RequestMapping(value="/view")
  10. public String view(HttpServletRequest request){
  11. return "/demoPage/firstPage";
  12. }
  13. }

2.添加测试页面 firstPage.html

  1. <!DOCTYPE html>
  2. <html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>Hello</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. </head>
  7. <body>
  8. <div>Hello World!</div>
  9. </body>
  10. </html>

3.目录结构

70 12

4.到这里启动后就能访问到测试页面了,后面我需要加入统一日志和统一返回格式处理,都是在真实开发环境下必须的,所以我也列出来

在com.springboot.demo.base.controller包下新建BaseController.java,用来提供日志接口以及返回model的固定参数格式

  1. package com.springboot.demo.base.controller;
  2. import com.springboot.demo.base.utils.StateParameter;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.ui.ModelMap;
  6. public abstract class BaseController{
  7. protected final String success = StateParameter.SUCCESS;
  8. protected final String fail = StateParameter.FAULT;
  9. protected final Logger logger = LoggerFactory.getLogger(this.getClass());
  10. public ModelMap getModelMap(String status,Object data,String msg){
  11. ModelMap modelMap=new ModelMap();
  12. modelMap.put("status", status);
  13. modelMap.put("data", data);
  14. modelMap.put("msg", msg);
  15. return modelMap;
  16. }
  17. }

在com.springboot.demo.base.utils包下新建StateParameter.java

  1. package com.springboot.demo.base.utils;
  2. public class StateParameter {
  3. public final static String SUCCESS="1";
  4. public final static String FAULT="0";
  5. public final static String DATA="data";
  6. public final static String STATE="state";
  7. }

目录结构如图:

70 13

5.添加简单日志记录,修改控制层代码,加入logger日志记录,添加back方法返回json数据,返回getModelMap传入状态值一般页面js接收用来判断操作成功或失败,data参数下方为null,一般传入想返回的数据内容如对象等,最后一个参数msg为提示信息。

  1. @Controller
  2. @RequestMapping(value="/first")
  3. public class FirstController extends BaseController{
  4. @RequestMapping(value="/view")
  5. public String view(HttpServletRequest request){
  6. logger.info("进入测试页面");
  7. return "/demoPage/firstPage";
  8. }
  9. @RequestMapping(value="/back", method = RequestMethod.POST)
  10. @ResponseBody
  11. public ModelMap back(HttpServletRequest request){
  12. logger.info("进入json测试页面");
  13. return getModelMap(StateParameter.SUCCESS, null, "请求成功");
  14. }
  15. }

打开浏览器输入:localhost:8080/first/view 测试页面正常跳转

70 14

打开RESTClient浏览器插件测试json数据返回正常(如没有插件可更改请求为get接收,直接在浏览器请求back路径即可返回json数据),输入 http://localhost:8080/first/back 返回我们设定的 msg:请求成功

70 15

查看控制台输出日志,关于Springboot项目结合AOP和logback日志持久化入库可以查看我的这篇文章。

70 16

全文完,2018/8/23

写博文不易,转载请注明出处。

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读