认识SpingBoot

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。 -使用springboot以后,搭建一个spring应用和开发变得很简单.

Spring Boot并不是一个框架,从根本上将,它就是一些maven库的集合,maven或者gradle项目导入相应依赖即可使用Spring Boot,而且无需自行管理这些库的版本。

Springboot就是一些写好了maven的模块,我们在使用SPring就不需以传统的方式来用,只需要以maven导入对应的springboot模块,就能完成一大堆操作。简单的说,它使用maven的方式对Spring应用开发进行进一步封装和简化。

简单而言,即Spring Boot使编码更简单,使配置更简单,使部署更简单,使监控更简单。!

Springboot就是为了简化spring应用搭建,开发,部署,监控的开发工具。

Spring boot入门

创建maven项目

导入Spring Boot依赖

spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加(在最外层的pom.xml中添加即可)。

     <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>

spingBoot对于web的支持

  <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>
<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>
</project>

SpringBoot做jsp跳转依赖包

     <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>
</project>
    配置application.properties对jsp支持
添加src/main/resources/application.properties: # 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello Angel From application
测试
@Controller
public class TestController { @RequestMapping("/index")
public String index(){
return "index";
}
}

SpingBoot对Freemark的静态化支持

 <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.newsoft.FreemaekApp</mainClass> <!--主类 包含main-->
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
</project>

然后准备一个静态模板

<!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>

配置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

spingBoot对于spingJdbc的支持

配置文件

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!--引入Maven依赖-mysql,jdbc-->
<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>
</dependencies> <build>
<finalName>spingboot_jdbc</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>
</project>

准备一个application.properties

 spring.datasource.url = jdbc:mysql://localhost:3306/shop
spring.datasource.username = root
spring.datasource.password = admin
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

然后建立dao层 service层都是一样的创建

创建一个启动类

package cn.newsoft;

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

最后完成测试

 package cn.newsoft;

 import cn.newsoft.domain.Employee;
import cn.newsoft.service.IEmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest(classes = JdbcApp.class)//这个class是启动类的
public class EmployeeTest { @Autowired
private IEmployeeService employeeService;
@Test
public void test() throws Exception{
Employee employee = new Employee();
employee.setName("zhsnag ");
employeeService.save(employee);
} }

SpringBoot对于Mybatis的支持

配置文件

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依赖: -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--
MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
将其作为一个plugin装入到SqlSessionFactory中。 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>
</project>

配置application.properties

spring.datasource.url = jdbc:mysql://localhost:3306/shop
spring.datasource.username = root
spring.datasource.password = admin
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.type-aliases-package=cn.newsoft.domain//别名

准备一个mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.newsoft.mapper.UserMapper">
<!--void save(User user);-->
<insert id="saveone" parameterType="User" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into t_user(name) values(#{name})
</insert> <select id="queryAll" resultType="User">
select * from t_user
</select>
</mapper>

然后mapper层操作和mybatis一样的

sevice层

package cn.newsoft.service.impl;

import cn.newsoft.domain.User;
import cn.newsoft.mapper.UserMapper;
import cn.newsoft.service.IUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service
@Transactional
public class IUserServiceImpl implements IUserService { @Autowired
private UserMapper userMapper; @Override
public void save(User user) {
userMapper.save(user);
} @Override
public void saveone(User user) {
userMapper.saveone(user);
} @Override
public List<User> queryAll() {//分页
PageHelper.startPage(1, 2);
List<User> users = userMapper.queryAll();
return users;
}
}

然后准备启动类

package cn.newsoft;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan(value = "cn.newsoft.mapper")
public class MybatisApp {
public static void main(String[] args) {
SpringApplication.run(MybatisApp.class);
}
}

最后完成测试

package cn.newsoft;

import cn.newsoft.domain.User;
import cn.newsoft.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisApp.class)
public class MybatisTest { @Autowired
private IUserService userService; @Test
public void test() throws Exception{
User user = new User();
user.setName("hhh ");
userService.save(user);
} @Test
public void tes1t() throws Exception{
User user = new User();
user.setName("hh");
userService.saveone(user);
} @Test
public void test2() throws Exception{
List<User> users = userService.queryAll();
for (User user : users) {
System.out.println(user);
}
} }

注意在分页时需要写一个分页类,专门用来分页

package cn.newsoft.config;

import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration; import java.util.Properties; @Configuration
public class MyBatisConfiguration {
@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;
}
}

SpingBoot的认识和基本使用的更多相关文章

  1. vue打包发布在spingboot项目中 vue-router路由的处理

    (原) 以下例子springboot后端地址为:localhost:7080/pingandai vue前端地址为:locahost:8080/pingandai/ 1.如果路由模式设置的是histo ...

  2. spingBoot整合mybatis+generator+pageHelper

    spingBoot整合mybatis+generator+pageHelper 环境/版本一览: 开发工具:Intellij IDEA 2018.1.4 springboot: 2.0.4.RELEA ...

  3. spingboot集成jpa(一)

    springboot + jpa 练习 spingboot集成jpa(一):最基本的环境搭建 spingboot集成jpa(二):使用单元测试 1. pom.xml中添加依赖 <!-- jdbc ...

  4. 13本热门书籍免费送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度学习、小程序开发等)

    七月第一周,网易云社区联合清华大学出版社为大家送出13本数据分析以及移动开发的书籍(Python.SpingBoot.Entity Framework.Ionic.MySQL.深度学习.小程序开发等) ...

  5. spingboot 2.1.3 与 elasticsearch7 兼容问题

    pom 加入 elasticsearch7  的依赖, <dependency> <groupId>org.elasticsearch</groupId> < ...

  6. 【SpringBoot】SpingBoot整合AOP

    https://blog.csdn.net/lmb55/article/details/82470388 [SpringBoot]SpingBoot整合AOPhttps://blog.csdn.net ...

  7. Spingboot+Mybatis+Oracle项目配置

    配置过程参考: 项目创建:http://how2j.cn/k/springboot/springboot-eclipse/1640.html 集成Mybatis使用Oracle:https://www ...

  8. SpingBoot项目搭建(详细)

    SpingBoot (原创:黑小子-余) springboot官网:->点击<- spring官网:->点击<- 一.SpringBoot简介 Spring Boot是由Piv ...

  9. 用Spingboot获得微信小程序的Code以及openid和sessionkey

    ​ 这篇文章主要写的是怎么用spingboot来获取微信小程序的Code以及openid和sessionke,我觉得已经很详细了 我们要获得openid和sessionkey,就必须先要获得code, ...

随机推荐

  1. 13-matlab图片转化

    图片格式: 处理函数: rgb2gray() gray2rgb()

  2. handler通信机制

    package com.example.gp08_day26_handler3; import android.os.Bundle; import android.os.Handler; import ...

  3. 接触mybatis使用

    1.mybatis mybatis是一个自定义sql.存储过程和高级映射的持久层框架,是Apache下的顶级项目. mybatis可以让程序员将主要精力放在sql上,通过mybatis提供的映射方式. ...

  4. PAT 1077 互评成绩计算(20)(代码+思路)

    1077 互评成绩计算(20 分) 在浙大的计算机专业课中,经常有互评分组报告这个环节.一个组上台介绍自己的工作,其他组在台下为其表现评分.最后这个组的互评成绩是这样计算的:所有其他组的评分中,去掉一 ...

  5. 实验 Attacks on TCP/IP Protocols

    ------- 转载请注明出处,博客园-lasgalen-http://www.cnblogs.com/lasgalen/p/4555648.html ------- 1 实验目的 进行这个实验的目的 ...

  6. JVM家族史考【转】

    说起Java虚拟机,许多Java程序员都会潜意识地把它与Sun(虽然太阳已然西落,但永远值得被记忆) HotSpot虚拟机等同看待,也许还有一些程序员会注意到BEA JRockit和IBM J9,但大 ...

  7. Laravel 日期时间处理包 Carbon 的应用

    在编写 PHP 应用时经常需要处理日期和时间,这篇文章带你了解一下 Carbon – 继承自 PHP DateTime 类的 API 扩展,它使得处理日期和时间更加简单.Laravel 中默认使用的时 ...

  8. OSGi 系列(三)之 bundle 详解

    OSGi 系列(三)之 bundle 详解 1. 什么是 bundle bundle 是以 jar 包形式存在的一个模块化物理单元,里面包含了代码,资源文件和元数据(metadata),并且 jar ...

  9. 使用JConsole监控HBase内存状态

    使用JConsole或者VisualVM等工具监控HBase状态时,需要修改HBase的配置文件,以解决随机端口问题. 文件名:hbase-env.sh export HBASE_JMX_BASE=& ...

  10. freeRadius与NetGear WNAP210的简使用

    1.下载安装freeRadius for win 2.2 2.配置client.cnf文件 加入 client 192.168.0.0/16{ secret=1111122222 shortname= ...