1.什么是springboot
是用来简化spring应用的初始化搭建以及开发过程。
springboot根本上并不是一个框架,它就是一些maven库的集合,maven或者gradle项目导入相应依赖即可使用Spring Boot,而且无需自行管理这些库的版本。
2.springboot的功能
首先Spring boot通过spring boot starter项目管理其提供的所有依赖的版本,
其中常见的依赖有:
spring-boot-starter-actuaor:生产准备的特性,主要用监控和管理应用
spring-boot-starter-security:对spring-security的支持
spring-boot-starter-aop:对面向切面的支持,包括spring-aop和Aspectj
spring-boot-starter-web:web支持,包括tomcat和spring-webmvc的支持
Spring-boot-starter-jdbc:springboot对jdbc支持
Spring-boot-starter-data jpa:springboot对data jpa支持
Spring-boot-starter-mybatis:springboot对mybatis支持
Spring-boot-starter-test:springboot对test支持
Spring-boot-starter-redis:对象redis的支持
Spring-boot-starter-es:对全文检索工具es的支持
Spring-boot-starter-sorl:对全文检索工具sorl的支持
3.使用java代码跳转jsp页面
3.1首先创建一个测试的项目来使用
其中需要引入pom.xml的依赖有:
3.2然后我们新建一个maven类型有webapp的子模块然后引入的依赖有:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat 的支持. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
3.3然后创建一个入口APP
3.4创建一个配置文件application.properties
3.5创建一个controller层和一个jsp的页面进行展示
3.6整体结构展示
然后页面就可以进行展示了
4.freemaker的支持
4.1引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
4.2在application.properties中配置属性有
# FreeeMarker 模板引擎配置
# 设定ftl文件路径
spring.freemarker.tempalte-loader-path=classpath:/templates
# 关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
4.3然后你准备展示的页面
在ftl文件中写入你准备的数据4.4然后在controller层直接跳转页面即可
5.配置文件YAML
Springboot除了支持application.properties的配置,还支持yaml,而且企业中也是用的最多的。
YAML的原则:
1、大小写敏感
2、使用缩进表示层级关系
4、缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级。
5、使用#表示注释
6、字符串可以不用引号标注
简单使用的方式:一般定义一个文件application.yml,以下是配置active对应选择的子yml文件:配置
spring:
profiles:
active: test
然后定义子yml文件:application-test.yml: 配置(port是端口号)
server:
port: 9000
6.对项目打包开启APP的操作
6.1首先在你需要打包的项目对应的pom.xml中配置如下
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>cn.itsource.freemark.FreemarkTest</mainClass> <!--主类 包含main-->
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
需要注意的是其中后写你的APP的入口的权限命名
6.2 然后cmd进入项目 运行**
mvn clean package spring-boot:repackage
6.3 进入target目录 运行jar包:
java -jar xxx.jar
如果是yml配置那么(dev就是你的选择的子yml文件)
java -jar -Dspring.profiles.active=dev xxx.jar
7.springboot集成springjdbc的保存数据的操作
7.1先引入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<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>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
7.2测试层需要配置
springjdbcApp就是我们的APP入口
@RunWith(SpringRunner.class)
@SpringBootTest(classes = springjdbcApp.class)
public class test {
7.3application.properties的配置内容
spring.datasource.url = jdbc:mysql://localhost:3306/springboottest
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
7.4写好保存数据的流程即可保存数据
8.使用springboot操作mybatis
8.1引入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<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>
</dependency>
<!-- mysql 数据库驱动. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--
spring-boot mybatis依赖:
请不要使用1.0.0版本,因为还不支持拦截器插件,
-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--
MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
将其作为一个plugin装入到SqlSessionFactory中。
Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。
Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper
-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
8.2保存数据和分页查询的布局如下
8.3配置application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/springboottest
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
#如下是配置mybatis支持别名
mybatis.type-aliases-package=cn.itsource.mybatis.domain
8.4在其入口需要扫描mapper层如下
8.5然后xml的配置正确即可成功保存数据
8.6要支持分页需配置pageHelper的bean如下
然后即可成功保存和分页查询数据了