spring boot 入门及示例
需要环境:eclipse4.7.3 + jdk1.8 +maven3.6.1 + tomcat(web需要)
spring boot官网介绍:https://spring.io/guides/gs/spring-boot/
sts官网:https://spring.io/tools/sts/ 找到合适的版本
选择安装方式 如果是离线安装下载对应的离线安装包
查看自己对应的版本信息
Eclipse进行安装
在线安装复制对应的版本连接到安装目录
离线安装打开目录选择下载的文件
创建示例:
打开eclipse:file ->new -> spring starter project
运行Demo1Application.java,右键->run as->Spring Boot App,控制台出现如下结果并无报错:
这里需要注意的是:每次运行一定要先将上次的停掉,否则会报错:tomcat端口被占用。
打开浏览器,输入http://localhost:8080/hello,则会出现如下结果。成功!!!
项目部署启动方式 java -jar 项目名称 -port 端口(可以指定配置文件中的参数)
创建spring mybatis 整合项目
在pom.xml文件中添加依赖包 或者从新创建一个项目
添加的依赖包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
新建项目的过程
这样,Spring boot就搭建好了,pom.xml里已经有了Spring boot的jar包,包括我们的mysql数据连接的jar包。Spring boot内置了类似tomcat这样的中间件,所以,只要运行DemoApplication中的main方法就可以启动项目了,不过需要先配置数据库。
配置数据库连接在application.properties中
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/shop1
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
这里配置druid数据库连接池
在pom.xml中加入druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
创建对应的结构
UserMapper.xml
UserMapper.java
还可以通过注解方式进行查询
这样可以不配做mapper.xml
UserController.java
Application.properties文件配置
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.example.demo.entity
配置热部署,修改代码不需要重启,在pom.xml中
<!-- 热部署模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
如果使用Thymeleaf模板引擎,需要把模板默认缓存设置为false
#禁止thymeleaf缓存(建议:开发环境设置为false,生成环境设置为true)
spring.thymeleaf.cache=false
3、针对devtools的可以指定目录或者排除目录来进行热部署
#添加那个目录的文件需要restart
spring.devtools.restart.additional-paths=src/main/java
#排除那个目录的文件不需要restart
spring.devtools.restart.exclude=static/**,public/**
4、默认情况下,/META-INF/maven,/META-INF/resources,/resources,/static,/templates,/public这些文件夹下的文件修改不会使应用重启,但是会重新加载(devtools内嵌了一个LiveReload Server,当资源发生改变时,浏览器刷新)
5、在application.properties中配置spring.devtools.restart.enabled=false,此时restart类加载器还会初始化,但不会监视文件更新。在SprintApplication.run之前调用System.setProperty(“spring.devtools.restart.enabled”, “false”);可以完全关闭重启支持。
运行结果
Spring Boot 支持 JSP
Spring Boot 的默认视图支持是 Thymeleaf 模板引擎,但是这个我们不熟悉啊,我们还是想要使用 JSP 怎么办呢?
· 第一步:修改 pom.xml 增加对 JSP 文件的支持
<!-- servlet依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency> <!-- tomcat的支持.-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
· 第二步:配置试图重定向 JSP 文件的位置
修改 application.properties 文件,将我们的 JSP 文件重定向到 /WEB-INF/views/ 目录下:
· 第三步:新建 index.jsp 文件
在【src/main】目录下依次创建 webapp、WEB-INF、jsp目录,并创建一个 index.jsp 文件:
Index页面内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${user.username}
</body>
</html>
· 第四步 修改 HelloController
修改 @RestController 注解为 @Controller:
html和templates
静态页面
spring boot项目只有src目录,默认没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下
/static
/public
/resources
/META-INF/resources
比如,在resources建立一个static目录和index.htm静态文件,访问地址 http://localhost:8080/index.html
如果要从后台跳转到静态index.html,代码如下。
@Controller
public class HtmlController {
@GetMapping("/html")
public String html() {
return "/index.html";
}
动态页面
动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面,比如访问JSP。spring boot建议不要使用JSP,默认使用Thymeleaf来做动态页面。
在pom.xml 中添加Thymeleaf组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
UserController.java
@Controller
public class UserController { @Autowired
UserMapper userMapper; @RequestMapping(value = "/user")
public String user(HttpServletRequest request){
User user = userMapper.findUserByName("111");
request.setAttribute("user", user);
return "/index";
}
}
return "/index": 跳转到 templates/index.html动态页面,templates目录为spring boot默认配置的动态页面路径。
index.html 将后台传递的key参数打印出来
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<span th:text="${user.username}"></span> </body>
</html>
访问http://localhost:8080/user
动态和静态区别
静态页面的return默认是跳转到/static/index.html,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/index.html,注意看两者return代码也有区别,动态没有html后缀。
重定向
如果在使用动态页面时还想跳转到/static/index.html,可以使用重定向return "redirect:/index.html"。
Thymelea模板配置
在application.properties文件中增加Thymeleaf模板的配置。
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
在html文件中引用命名空间 。
<html xmlns:th="http://www.thymeleaf.org"> (非必须,建议加上)
输出内容
<p th:text="${user.username}">Welcome to our grocery store!</p>
Spring boot mybatis 逆向工程生成
Pom.xml中添加generator插件 (生成文件之后删除,如果不删除在打包的时候会重复生成)
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId> mysql</groupId>
<artifactId> mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>false</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>
src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
spring boot 入门及示例的更多相关文章
- 161103、Spring Boot 入门
Spring Boot 入门 spring Boot是Spring社区较新的一个项目.该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验 ...
- Spring Boot入门教程1、使用Spring Boot构建第一个Web应用程序
一.前言 什么是Spring Boot?Spring Boot就是一个让你使用Spring构建应用时减少配置的一个框架.约定优于配置,一定程度上提高了开发效率.https://zhuanlan.zhi ...
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...
- Spring Boot 入门教程
Spring Boot 入门教程,包含且不仅限于使用Spring Boot构建API.使用Thymeleaf模板引擎以及Freemarker模板引擎渲染视图.使用MyBatis操作数据库等等.本教程示 ...
- spring boot入门与进阶
视频课程包含: SpringBoot入门.SpringBoot进阶.Spring Cloud微服务.Spring Ecosystem 微服务相关.Spring Boot 入门 IDEA 版本.Spri ...
- spring boot入门教程——Spring Boot快速入门指南
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...
- Spring Boot 入门之缓存和 NoSQL 篇(四)
原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...
- spring boot入门与进阶教程
SpringBoot入门.SpringBoot进阶.Spring Cloud微服务.Spring Ecosystem 微服务相关.Spring Boot 入门 IDEA 版本.Spring Boot集 ...
- spring boot 入门操作(二)
spring boot入门操作 使用FastJson解析json数据 pom dependencies里添加fastjson依赖 <dependency> <groupId>c ...
随机推荐
- Verilog中关于wire使用的一些小知识
1.Verilog中如果wire连接到常量,而常量没有说明他的位宽,那么将会默认为32位 如: input [:] x ; wire [:] a; assign a = + x; 上述代码在综合的时候 ...
- innodb 关键特性(insert buffer)
一.insert buffer 性能改善 insert buffer和数据页一样,也是物理页的一个组成部分. 在innodb存储引擎中,主键是行唯一的标识符.通常应用程序中行记录的插入顺序是按照主键递 ...
- POJ-3660.Cow Contest(有向图的传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17797 Accepted: 9893 De ...
- LinkedStack
public class LinkedStack<T> { private static class Node<U>{ U item; Node<U>next; N ...
- python3 报错
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: ...
- hdu4622(hash解法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 Now you are back,and have a task to do:Given you ...
- leedcode算法解题思路
1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...
- leetcode8:字符串转整数 (atoi)
实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值 ...
- Springmvc中@RequestMapping 属性用法归纳
简介: @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestM ...
- 探究osg中的程序设计模式【目录】
前序 探究osg中的程序设计模式---开篇 探究osg中的程序设计模式---创造性模式 探究osg中的程序设计模式---创造型模式---Factory(工厂)模式 探究osg中的程序设计模式---创造 ...