服务器之家

服务器之家 > 正文

SpringBoot框架搭建教程分享

时间:2020-12-21 10:43     来源/作者:dali_lyc

SpringBoot几乎集成了SpringMVC的所有内容,以及tomcat容器,同时去除了繁复的xml配置文件,开发起来十分方便;页面配合thymeleaf模板渲染也是非常简单,如果是前后端分离的项目,那么SpringBoot就专门负责提供restful风格的api接口,通过json格式与前端进行数据交互。

下面pom.xml里面一些依赖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <groupId>com.test</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <modules>
  <module>common</module>
  <module>web</module>
 </modules>
 <packaging>pom</packaging>
 
 <name>demo</name>
 <description>a project for Spring Boot</description>
 
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
  <relativePath/>
 </parent>
 
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
    <exclusion>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <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-devtools</artifactId>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
 
  <dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-jdbc</artifactId>
   <version>7.0.47</version>
  </dependency>
 
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>4.3.6.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>4.3.6.RELEASE</version>
  </dependency>
 
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
 
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.1.1</version>
  </dependency>
 
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.19</version>
  </dependency>
 
  <dependency>
   <groupId>net.sf.json-lib</groupId>
   <artifactId>json-lib</artifactId>
   <version>2.4</version>
   <classifier>jdk15</classifier>
   <exclusions>
    <exclusion>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
 
  <dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>3.0.1</version>
  </dependency>
 
  <!--log4j2-->
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-slf4j-impl</artifactId>
   <version>2.4.1</version>
  </dependency>
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-api</artifactId>
   <version>2.4.1</version>
  </dependency>
  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-core</artifactId>
   <version>2.4.1</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>jcl-over-slf4j</artifactId>
   <version>1.7.12</version>
  </dependency>
 
 </dependencies>
 
 <build>
  <finalName>demo</finalName>
  <resources>
   <resource>
    <directory>src/main/resources</directory>
    <excludes>
     <exclude>application-dev.properties</exclude>
     <exclude>application-prod.properties</exclude>
     <exclude>application.properties</exclude>
    </excludes>
    <filtering>true</filtering>
   </resource>
   <resource>
    <filtering>true</filtering>
    <directory>src/main/resources</directory>
    <includes>
     <include>application-${environment}.properties</include>
     <include>application.properties</include>
    </includes>
   </resource>
  </resources>
 </build>
 
 <profiles>
  <profile>
   <id>dev</id>
   <properties>
    <environment>dev</environment>
   </properties>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>prod</id>
   <properties>
    <environment>prod</environment>
   </properties>
  </profile>
 
 </profiles>
</project>

下面是SpringBoot的启动配置文件application.properties:

?
1
2
3
4
5
6
7
8
spring.thymeleaf.cache=false
server.port=8021
server.context-path=/demo
 
spring.datasource.url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=XXX
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

下面是SpringBoot的启动类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.test.demo.web;
import com.test.demo.web.filter.AccessFilter;
import com.test.demo.web.interceptor.AccessInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
import javax.sql.DataSource;
 
@SpringBootApplication(scanBasePackages = "com.test.demo")
@MapperScan("com.test.demo.common.dao")
@Configuration
public class WebApplication { @Bean
 @ConfigurationProperties(prefix = "spring.datasource")
 public DataSource dataSource() {
  return new org.apache.tomcat.jdbc.pool.DataSource();
 }
 
 // 提供SqlSession
 @Bean
 public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
  SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
  sqlSessionFactoryBean.setDataSource(dataSource());
  PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
  sqlSessionFactoryBean.setTypeAliasesPackage("com.test.demo.common.model");
  sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
  return sqlSessionFactoryBean.getObject();
 }
 
 // 事务配置
 @Bean
 public PlatformTransactionManager transactionManager() {
  return new DataSourceTransactionManager(dataSource());
 }
 
 // 主启动函数
 public static void main(String[] args) {
  SpringApplication.run(WebApplication.class, args);
 }
 
 // 过滤器配置
 @Bean
 public FilterRegistrationBean someFilterRegistration() {
  FilterRegistrationBean registration = new FilterRegistrationBean();
  AccessFilter accessFilter = new AccessFilter();
  registration.setFilter(accessFilter);
  registration.addUrlPatterns("/admin/*");
  registration.setName("accessFilter");
  return registration;
 }
 
 // 拦截器配置
 @Configuration
 public class addInterceptor extends WebMvcConfigurerAdapter {
  public void addInterceptors(InterceptorRegistry registry) {
   registry.addInterceptor(new AccessInterceptor()).addPathPatterns("/admin/**");
  }
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/dali-lyc/p/7428012.html

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
返回顶部