一, 认识
Spring Boot 是由 Pivotal 团队提供的全新框架, 其设计目的是用来简化 Spring 应用的初始搭建以及开发过程. - 使用 springboot 以后, 搭建一个 spring 应用和开发变得很简单.
Springboot 就是一些写好了 maven 的模块, 我们在使用 SPring 就不需以传统的方式来用, 只需要以 maven 导入对应的 springboot 模块, 就能完成一大堆操作. 简单的说, 它使用 maven 的方式对 Spring 应用开发进行进一步封装和简化.
Spring Boot 使编码更简单, 使配置更简单, 使部署更简单, 使监控更简单. Springboot 就是为了简化 spring 应用搭建, 开发, 部署, 监控的开发工具.
二, 入门准备
开发环境 JDK 1.8
项目管理工具 ( Maven )
开发工具 (Eclipse/idea)
1. 创建普通 maven 项目 springboot_parent
2. 配置 pom.xml
- <!--
- spring boot 父节点依赖, 引入这个之后相关的引入
- 就不需要添加 version 配置,
- spring boot 会自动选择最合适的版本进行添加.
- -->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.5.RELEASE</version>
- </parent>
- <dependencyManagement>
- <dependencies>
- <!--springboot 版本管理 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>2.0.5.RELEASE</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
三, springBoot+springMVC
1. 通过 maven 创建一个 Web 模块
2. 配置 pom.xml
- <!-- tomcat 和 springMVC-->
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <!-- 热部署 ctrl+f9-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- <scope>true</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <!--fork : 如果没有该项配置, 可能 devtools 不会起作用, 即应用不会 restart -->
- <fork>true</fork>
- </configuration>
- </plugin>
- </plugins>
- </build>
3. 准备 tomcat 启动类
- @SpringBootApplication
- public class MVCApp {
- public static void main(String[] args) {
- SpringApplication.run(MVCApp.class);
- }
- }
4. 通过 controller 层进行访问
- @RestController// 包含 JSON 格式和 controller
- public class MvcController {
- @RequestMapping("/hello")
- public String index(){
- return "suSu";
- }
- }
四, springBoot+jsp 跳转
1. 通过 maven 创建一个 Web 模块
2. 配置 pom.xml
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!-- web 支持: 1,web mvc; 2,restful; 3,jackjson 支持; 4,aop ........ -->
- <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>
- </dependencies>
- <build>
- <finalName>springboot_jsp</finalName>
- <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
- <plugins>
- <plugin>
- <artifactId>maven-clean-plugin</artifactId>
- <version>3.1.0</version>
- </plugin>
- <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
- <plugin>
- <artifactId>maven-resources-plugin</artifactId>
- <version>3.0.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- </plugin>
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.22.1</version>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>3.2.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-install-plugin</artifactId>
- <version>2.5.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-deploy-plugin</artifactId>
- <version>2.8.2</version>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
3. 在 resources 中添加文件 application.properties 配置视图解析器
- # 页面默认前缀目录
- spring.mvc.view.prefix=/Web-INF/jsp/
- # 响应页面默认后缀
spring.mvc.view.suffix=.jsp
4. 通过 controller 层访问
- @Controller
- public class JspController {
- @RequestMapping("/index")
- public String index(){
- return "index";
- }
- }
- 5.webapp
6. 修改 working directory 配置
五, springBoot+freemarker 模板技术
1. 利用 maven 构建 webapp 模块
2. 配置 pom.xml 注意修改 mainClass 路径
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <!-- 对 freemark 的支持 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-freemarker</artifactId>
- </dependency>
- </dependencies>
- <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.su.FreemarkApp</mainClass> <!-- 主类 包含 main-->
- <layout>JAR</layout>
- </configuration>
- </plugin>
- </plugins>
- </build>
3. 在 main/resources/templates 中建立 index.ftl 模板 注意 templates 有 s
- <!DOCTYPE HTML>
- <HTML xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
- xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
- <head>
- <title>
- Hello World!
- </title>
- </head>
- <body>
- <h1>
- Hello , ${msg}
- </h1>
- </body>
- </HTML>
4. 为模板配置属性文件 application.properties
- # FreeeMarker 模板引擎配置
- # 设定 ftl 文件路径 注意 templates 有 s
- 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
5. 启动类和 controller
- @SpringBootApplication
- public class FreemarkApp {
- public static void main(String[] args) {
- SpringApplication.run(FreemarkApp.class);
- }
- }
- @Controller
- public class IndexController {
- @RequestMapping("/index")
- public String index(Model model){
- model.addAttribute("msg", "suSu");
- return "index";
- }
- }
6. 也可以支持返回 JSON 格式 时间格式在 get 上配置 @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GTM+8")
- @Controller
- public class JsonController {
- @RequestMapping("/user")
- @ResponseBody
- public List<User> list(){
- List<User> users = Arrays.asList(new User(1L, "s", new Date()),
- new User(2L, "u", new Date()),
- new User(3L, "q", new Date()));
- return users;
- }
- }
六, 支持 YAML
YAML 是便于人阅读基于 unicode 编码的各种语言的序列号标准. 它的用途广泛, 用于配置文件, 日志文件, 跨语言数据共享, 对象持久化, 复杂的数据结构.
原则:
1, 大小写敏感
2, 使用缩进表示层级关系
4, 缩进长度没有限制, 只要元素对齐就表示这些元素属于一个层级.
5, 使用 #表示注释
6, 字符串可以不用引号标注
语法:
使用 : 表示键值对 age :18
七, springBoot+springJDBC
1. 利用 maven 构建 webapp 项目
2. 配置 pom.xml
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <!-- web test springjdbc mysql-->
- <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>
- </dependencies>
- <build>
- <finalName>springboot_springjdbc</finalName>
- <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
- <plugins>
- <plugin>
- <artifactId>maven-clean-plugin</artifactId>
- <version>3.1.0</version>
- </plugin>
- <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
- <plugin>
- <artifactId>maven-resources-plugin</artifactId>
- <version>3.0.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- </plugin>
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.22.1</version>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>3.2.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-install-plugin</artifactId>
- <version>2.5.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-deploy-plugin</artifactId>
- <version>2.8.2</version>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
3. 利用 application.properties 连接数据库
- spring.datasource.url = jdbc:MySQL://localhost:3306/shop
- spring.datasource.username = root
- spring.datasource.password = 000000
- 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
4. 接着就是我们熟悉的 domain,dao,service(事物只用配置注解 @Transactional),controller 层
5. 启动项和 SpringBoot 测试类, 注意需要与 domain 写在同一层 , 否则需要做些其他配置
- // 启动
- @SpringBootApplication
- public class JDBCApp {
- public static void main(String[] args) {
- SpringApplication.run(JDBCApp.class);
- }
- }
- // 测试
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = JDBCApp.class)
- public class JDBCTest {
- @Autowired
- private IUserService userService;
- @Test
- public void test() throws Exception{
- userService.save(new User(1L,"su"));
- }
- }
八, springBoot+mybatis 及 pageHelper 分页
1. 利用 maven 构建 webapp 模块
2. 配置 pom.xml
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </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>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <!-- mysql 数据库驱动. -->
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- </dependency>
- <!--
- spring-boot mybatis 依赖:
- 请不要使用 1.0.0 版本, 因为还不支持拦截器插件,
- 1.1.1 是博主写帖子时候的版本, 大家使用最新版本即可
- -->
- <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>
- </dependencies>
- <build>
- <finalName>springboot_mybatis</finalName>
- <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
- <plugins>
- <plugin>
- <artifactId>maven-clean-plugin</artifactId>
- <version>3.1.0</version>
- </plugin>
- <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
- <plugin>
- <artifactId>maven-resources-plugin</artifactId>
- <version>3.0.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- </plugin>
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.22.1</version>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>3.2.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-install-plugin</artifactId>
- <version>2.5.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-deploy-plugin</artifactId>
- <version>2.8.2</version>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
3. 连接数据库配置 application.properties
- spring.datasource.url = jdbc:MySQL://localhost:3306/shop
- spring.datasource.username = root
- spring.datasource.password = 000000
- 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
- #为 mapper 使用 domain 配置别名
- mybatis.type-aliases-package=cn.su.domain
4. 省略 domain,mapper,service,controller 层
注意 mapper 的 java 文件与 resources 下的 mapper 的配置文件需要完全对应
5. 配置分页插件和使用
- @Configuration // 相当于 application-Xxx.xml
- public class MyBatisConfiguration {
- // 相当于配置文件里面配置 < bean>
- @Bean
- public PageHelper pageHelper(){
- PageHelper pageHelper = new PageHelper();
- Properties p = new Properties();
- // 配置分页的参数设置
- // 和 startPage 中的 pageNum 效果一样
- p.setProperty("offsetAsPageNum", "true");
- // 设置为 true 时, 使用 RowBounds 分页会进行 count 查询
- p.setProperty("rowBoundsWithCount", "true");
- // 启用合理化时, 如果 pageNum<1 会查询第一页, 如果 pageNum>pages 会查询最后一页
- p.setProperty("reasonable", "true");
- pageHelper.setProperties(p);
- return pageHelper;
- }
- }
- // 使用
- //1 表示当前页 2 表示条数
- PageHelper.startPage(1, 2);
6. 启动类和测试
- // 启动 需要扫描 mapper
- @SpringBootApplication
- @MapperScan(value = "cn.su.mapper")
- public class MyBatisApp {
- public static void main(String[] args) {
- SpringApplication.run(MyBatisApp.class);
- }
- }
- // 测试
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = MyBatisApp.class)
- public class MybatisTest {
- @Autowired
- private IUserService userService;
- @Test
- public void test1() throws Exception{
- userService.saveByInsert(new User(2L,"W"));
- }
- @Test
- public void test2() throws Exception{
- userService.saveByXml(new User(3L,"Q"));
- }
- @Test
- public void test3() throws Exception{
- List<User> users = userService.findByQuery();
- users.forEach(e->{
- System.out.println(e);
- });
- }
- }
来源: http://www.bubuko.com/infodetail-3039934.html