springboot框架中常用模板
搭建springboot框架导入依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version></p
·
搭建springboot框架导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
导入本地jar包
<dependency>
<groupId>com.hzx</groupId>
<artifactId>MvCameraControlWrapper</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/MvCameraControlWrapper.jar</systemPath>
</dependency>
zookeeper依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
</dependency>
src/main/java
yml配置文件
spring:
datasource:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db_calculation
username: root
password: 123456
devtools:
restart:
enabled: true
additional-paths: src/main/java
exclude: WEB-INF/**
freemarker:
cache: false
src/test/java
依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
需要使用注解@RunWith(SpringJUnit4ClassRunner.class)和@SpringBootTest(classes = SpotCheckStartApplication.class)
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpotCheckStartApplication.class)
public class SpotTest {
@Autowired
private SpotService spotService;
@Test
public void getAll() {
List<Item> spot = spotService.getAllSpot();
for (Item item : spot) {
System.out.println(item);
}
}
}
日志
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- property 用于配置变量,可通过${LOG_PATH}取对应的值 a -->
<!-- 定义日志的存储地址,不要在配置中使用相对路径 -->
<property name="LOG_PATH" value="D:/Java Code/testmaven06calculation/log/" />
<property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level line:%line %logger{50} %msg%n" />
<contextName>logback</contextName>
<!-- 输出到控制台,一个appender定义一种输出策略,可以定义多个a -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!-- filter中定义要输出的日志级别,默认是info -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level>
</filter>
<!-- 日志输出编码格式化 -->
<encoder>
<pattern>
${PATTERN}
</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 输出到文件debug,日期滚动记录a -->
<appender name="logDebugFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--<Prudent>true</Prudent> -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤 其他只留debug -->
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- 滚动策略 按照时间滚动TimeBasedRollingPolicy每天会生成一个日志文件a -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 文件路径 定义了日志的切分方式 防止日志填满磁盘 -->
<fileNamePattern>
${LOG_PATH}/debug/log-debug-%d{yyyy-MM-dd}.log
</fileNamePattern>
<!-- 日志保留天数 -->
<maxHistory>7</maxHistory>
<!-- 用来指定日志文件的上下限大小 到了这个值就是删除旧日志 -->
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<append>true</append>
<!-- 日志输出编码格式化 -->
<encoder>
<charset>GBK</charset>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>
<!-- 输出到文件info,日期滚动记录a -->
<appender name="logInfoFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--<Prudent>true</Prudent> -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤 其他只留info -->
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- 滚动策略 按照时间滚动TimeBasedRollingPolicy每天会生成一个日志文件a -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 文件路径 定义了日志的切分方式 防止日志填满磁盘 -->
<fileNamePattern>
${LOG_PATH}/info/log-info-%d{yyyy-MM-dd}.log
</fileNamePattern>
<!-- 日志保留天数 -->
<maxHistory>7</maxHistory>
<!-- 用来指定日志文件的上下限大小 到了这个值就是删除旧日志 -->
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<append>true</append>
<!-- 日志输出编码格式化 -->
<encoder>
<charset>GBK</charset>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>
<!-- 输出到文件WARN,日期滚动记录a -->
<appender name="logWarnFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--<Prudent>true</Prudent> -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤 其他只留info -->
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- 滚动策略 按照时间滚动TimeBasedRollingPolicy每天会生成一个日志文件a -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 文件路径 定义了日志的切分方式 防止日志填满磁盘 -->
<fileNamePattern>
${LOG_PATH}/warn/log-warn-%d{yyyy-MM-dd}.log
</fileNamePattern>
<!-- 日志保留天数 -->
<maxHistory>7</maxHistory>
<!-- 用来指定日志文件的上下限大小 到了这个值就是删除旧日志 -->
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<append>true</append>
<!-- 日志输出编码格式化 -->
<encoder>
<charset>GBK</charset>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>
<!-- 输出到文件error,日期滚动记录a -->
<appender name="logErrorFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--<Prudent>true</Prudent> -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤 其他只留info -->
<level>error</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<!-- 滚动策略 按照时间滚动TimeBasedRollingPolicy每天会生成一个日志文件a -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 文件路径 定义了日志的切分方式 防止日志填满磁盘 -->
<fileNamePattern>
${LOG_PATH}/error/log-error-%d{yyyy-MM-dd}.log
</fileNamePattern>
<!-- 日志保留天数 -->
<maxHistory>7</maxHistory>
<!-- 用来指定日志文件的上下限大小 到了这个值就是删除旧日志 -->
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<append>true</append>
<!-- 日志输出编码格式化 -->
<encoder>
<charset>GBK</charset>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>
<!-- 将上面两种策略添加到节点 -->
<root level="info">
<appender-ref ref="console" />
<appender-ref ref="logDebugFile" />
<appender-ref ref="logInfoFile" />
<appender-ref ref="logWarnFile" />
<appender-ref ref="logErrorFile" />
</root>
<!-- 开发环境 -->
<!-- <springProfile name="dev"> <root level="debug"> <appender-ref ref="logInfoFile"/> <appender-ref ref="logErrorFile"/> <appender-ref ref="console"/> </root> </springProfile> -->
<!-- 生产环境 -->
<!-- <springProfile name="pro"> <root level="INFO"> <appender-ref ref="logInfoFile"/> <appender-ref ref="logErrorFile"/> <appender-ref ref="console"/> </root> </springProfile> -->
</configuration>
application.yml
logging:
config: src/main/resources/logback-spring.xml
level:
'[testmaven06calculation.com.cal.res]': DEBUG
sqlite数据库
在springboot框架中使用sqlite数据库的方法与sqlse数据库相同,通过mybatis框架操作SQL语句。
因为sqlite数据库无法远程连接,所以需要把数据库文件赋值到resource文件夹下的static文件中。
导入依赖
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0.1</version>
</dependency>
application.yml
spring:
datasource:
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite::resource:static/sqlite/db_C808_spot_check_test_sqlite.db
username:
password:
当db文件不在resources文件夹下时,url中输入绝对路径,“sqlite”后只有一个“:”
url: jdbc:sqlite:D:\@huangzixiao\数据库\db_C808_spot_check_test_sqlite.db
mysql数据库
导入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/{databaseName}?characterEncoding=utf8&serverTimezone=GMT&useSSL=false&allowMultiQueries=true
allowMultiQueries=true表示可以在mybatis一个标签中执行多条sql语句
elasticsearch
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency>
spring:
elasticsearch:
rest:
uris: http://127.0.0.1:9200
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献4条内容
所有评论(0)