SpringBoot框架搭建教程分享
程序员文章站
2024-02-25 11:11:28
springboot几乎集成了springmvc的所有内容,以及tomcat容器,同时去除了繁复的xml配置文件,开发起来十分方便;页面配合thymeleaf模板渲染也是非...
springboot几乎集成了springmvc的所有内容,以及tomcat容器,同时去除了繁复的xml配置文件,开发起来十分方便;页面配合thymeleaf模板渲染也是非常简单,如果是前后端分离的项目,那么springboot就专门负责提供restful风格的api接口,通过json格式与前端进行数据交互。
下面pom.xml里面一些依赖
<?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:
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/**"); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: java实现字符串反转