SpingBoot的认识和基本使用
Posted xiaohuziguai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpingBoot的认识和基本使用相关的知识,希望对你有一定的参考价值。
认识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中添加即可)。
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.0.5.RELEASE</version> 5 </parent> 6 7 <dependencyManagement> 8 <dependencies> 9 <!--springboot版本管理--> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-dependencies</artifactId> 13 <version>2.0.5.RELEASE</version> 14 <type>pom</type> 15 <scope>import</scope> 16 </dependency> 17 </dependencies> 18 </dependencyManagement>
spingBoot对于web的支持
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 <!-- 热部署--> 7 <dependency> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-devtools</artifactId> 10 <optional>true</optional> 11 <scope>true</scope> 12 </dependency> 13 </dependencies> 14 15 <build> 16 <plugins> 17 <plugin> 18 <groupId>org.springframework.boot</groupId> 19 <artifactId>spring-boot-maven-plugin</artifactId> 20 <configuration> 21 <!--fork : 如果没有该项配置,可能devtools不会起作用,即应用不会restart --> 22 <fork>true</fork> 23 </configuration> 24 </plugin> 25 </plugins> 26 </build> 27 </project>
SpringBoot做jsp跳转依赖包
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>4.11</version> 6 <scope>test</scope> 7 </dependency> 8 <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> 9 <dependency> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-web</artifactId> 12 </dependency> 13 14 <!-- servlet 依赖. --> 15 <dependency> 16 <groupId>javax.servlet</groupId> 17 <artifactId>javax.servlet-api</artifactId> 18 </dependency> 19 20 <dependency> 21 <groupId>javax.servlet</groupId> 22 <artifactId>jstl</artifactId> 23 </dependency> 24 25 <!-- tomcat 的支持. --> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-tomcat</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>org.apache.tomcat.embed</groupId> 32 <artifactId>tomcat-embed-jasper</artifactId> 33 </dependency> 34 </dependencies> 35 36 <build> 37 <finalName>springboot_jsp</finalName> 38 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 39 <plugins> 40 <plugin> 41 <artifactId>maven-clean-plugin</artifactId> 42 <version>3.1.0</version> 43 </plugin> 44 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 45 <plugin> 46 <artifactId>maven-resources-plugin</artifactId> 47 <version>3.0.2</version> 48 </plugin> 49 <plugin> 50 <artifactId>maven-compiler-plugin</artifactId> 51 <version>3.8.0</version> 52 </plugin> 53 <plugin> 54 <artifactId>maven-surefire-plugin</artifactId> 55 <version>2.22.1</version> 56 </plugin> 57 <plugin> 58 <artifactId>maven-war-plugin</artifactId> 59 <version>3.2.2</version> 60 </plugin> 61 <plugin> 62 <artifactId>maven-install-plugin</artifactId> 63 <version>2.5.2</version> 64 </plugin> 65 <plugin> 66 <artifactId>maven-deploy-plugin</artifactId> 67 <version>2.8.2</version> 68 </plugin> 69 </plugins> 70 </pluginManagement> 71 </build> 72 </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
1 spring.datasource.url = jdbc:mysql://localhost:3306/shop 2 spring.datasource.username = root 3 spring.datasource.password = admin 4 spring.datasource.driverClassName = com.mysql.jdbc.Driver 5 spring.datasource.max-active=20 6 spring.datasource.max-idle=8 7 spring.datasource.min-idle=8 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); } }
最后完成测试
1 package cn.newsoft; 2 3 import cn.newsoft.domain.Employee; 4 import cn.newsoft.service.IEmployeeService; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.boot.test.context.SpringBootTest; 9 import org.springframework.test.context.junit4.SpringRunner; 10 11 @RunWith(SpringRunner.class) 12 @SpringBootTest(classes = JdbcApp.class)//这个class是启动类的 13 public class EmployeeTest { 14 15 @Autowired 16 private IEmployeeService employeeService; 17 @Test 18 public void test() throws Exception{ 19 Employee employee = new Employee(); 20 employee.setName("zhsnag "); 21 employeeService.save(employee); 22 } 23 24 }
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的认识和基本使用的主要内容,如果未能解决你的问题,请参考以下文章