SpringBoot框架搭建教程分享

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

十余年的岱岳网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整岱岳建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“岱岳网站设计”,“岱岳网站推广”以来,每个客户项目都认真落实执行。

下面pom.xml里面一些依赖

<?xml version="1.0" encoding="UTF-8"?>

 4.0.0

 com.test
 demo
 0.0.1-SNAPSHOT
 
  common
  web
 
 pom

 demo
 a project for Spring Boot

 
  org.springframework.boot
  spring-boot-starter-parent
  1.5.1.RELEASE
  
 

 
  UTF-8
  UTF-8
  1.8
 

 
  
   org.springframework.boot
   spring-boot-starter
   
    
     org.springframework.boot
     spring-boot-starter-logging
    
   
  
  
   org.springframework.boot
   spring-boot-starter-test
   test
  
  
   org.springframework.boot
   spring-boot-starter-web
  
  
   org.springframework.boot
   spring-boot-devtools
   true
  
  
   org.springframework.boot
   spring-boot-starter-thymeleaf
  

  
   org.apache.tomcat
   tomcat-jdbc
   7.0.47
  

  
   org.springframework
   spring-tx
   4.3.6.RELEASE
  
  
   org.springframework
   spring-jdbc
   4.3.6.RELEASE
  

  
   MySQL
   mysql-connector-java
  

  
   org.mybatis.spring.boot
   mybatis-spring-boot-starter
   1.1.1
  

  
   com.alibaba
   druid
   1.0.19
  

  
   net.sf.json-lib
   json-lib
   2.4
   jdk15
   
    
     commons-logging
     commons-logging
    
   
  

  
   commons-httpclient
   commons-httpclient
   3.0.1
  

  
  
   org.apache.logging.log4j
   log4j-slf4j-impl
   2.4.1
  
  
   org.apache.logging.log4j
   log4j-api
   2.4.1
  
  
   org.apache.logging.log4j
   log4j-core
   2.4.1
  
  
   org.slf4j
   jcl-over-slf4j
   1.7.12
  

 

 
  demo
  
   
    src/main/resources
    
     application-dev.properties
     application-prod.properties
     application.properties
    
    true
   
   
    true
    src/main/resources
    
     application-${environment}.properties
     application.properties
    
   
  
 

 
  
   dev
   
    dev
   
   
    true
   
  
  
   prod
   
    prod
   
  

 


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

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的启动类:

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/**");
  }
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


文章题目:SpringBoot框架搭建教程分享
地址分享:http://ybzwz.com/article/gsjgsi.html