SpringBoot框架搭建教程分享

网友投稿 279 2023-04-15


SpringBoot框架搭建教程分享

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

下面pom.xml里面一些依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

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

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:node实现定时发送邮件的示例代码
下一篇:Angular2 组件交互实例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~