Spring Boot 框架(六) —— Spring Boot 之集成和操作数据库(MySQL、Oracle、SQL Server、MongoDB、Neo4j、Redis、Memcached)

一、Spring Boot 集成数据库

1.1 集成 MySQL 数据库

1.1.1 MySQL 依赖
在项目的pom.xml文件中加入MySQL数据库的依赖

<!-- MySQL依赖 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

1.1.2 MySQL 配置文件
application.properties配置文件中配置MySQL

## 数据库地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
## 数据库用户名
spring.datasource.username=root
## 数据库密码
spring.datasource.password=123456
## 数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

1.2 集成 Oracle 数据库

1.2.1 Oracle 依赖
在项目的pom.xml文件中加入Oracle 数据库的依赖

<!-- Oracle依赖 -->
<dependency>
    <groupId>com.oracle.ojdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <scope>runtime</scope>
</dependency>

1.2.2 Oracle 配置文件
application.properties配置文件中配置Oracle

### 数据库地址
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
### 数据库用户名
spring.datasource.username=scott
### 数据库密码
spring.datasource.password=tiger
### 数据库驱动
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

1.3 集成 SQL Server 数据库

1.3.1 SQL Server 依赖
在项目的pom.xml文件中加入SQL Server数据库的依赖

<!-- SQL Server依赖-->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <scope>runtime</scope>
</dependency>

1.3.2 SQL Server 配置文件
application.properties配置文件中配置SQL Server

## 数据库地址
spring.datasource.url=jdbc:sqlserver://192.168.16.18:1433;databaseName=test
## 数据库用户名
spring.datasource.username=sa
## 数据库密码
spring.datasource.password=123456
## 数据库驱动
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

1.4 集成 Neo4j 数据库

1.4.1 Neo4j 依赖
在项目的pom.xml文件中加入Neo4j 数据库的依赖

<!-- Neo4j依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>

1.4.2 Neo4j 配置文件
application.properties配置文件中配置Neo4j

### 数据库地址
spring.data.neo4j.uri=http://localhost:7474
### 数据库用户名
spring.data.neo4j.username=root
### 数据库密码
spring.data.neo4j.password=123456

1.5 集成 MongoDB 数据库

1.5.1 MongoDB 依赖
在项目的pom.xml文件中加入MongoDB 数据库的依赖

<!--MongoDB依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

1.5.2 MongoDB 配置文件
application.properties配置文件中配置MongoDB

##无密码
spring.data.mongodb.uri=mongodb://localhost:27017/test
##无密码root(userName):123456(password)@localhost(ip地址):27017(端口号)/test(collections/数据库)
spring.data.mongodb.uri=mongodb://root:123456@localhost:27017/test

1.6 集成 Redis 数据库

1.6.1 Redis 依赖
在项目的pom.xml文件中加入Redis 数据库的依赖

<!-- Redis依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

1.6.2 Redis 配置文件
application.properties配置文件中配置Redis

## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=localhost
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=0

1.7 集成 Memcached 数据库

1.7.1 Memcached 依赖
在项目的pom.xml文件中加入Memcached 数据库的依赖

<!-- Memcached依赖 -->
<dependency>
    <groupId>net.spy</groupId>
    <artifactId>spymemcached</artifactId>
</dependency>

1.7.2 Memcached 配置文件
application.properties配置文件中配置Memcached

## memcached地址
memcache.ip=localhost
## memecached端口
memcache.port=11211

二、application.properties常用配置说明

2.1 SPRING 相关配置

spring.config.name= # config file name (default to ‘application’)
spring.config.location= # location of config file

2.2 profile相关配置

spring.profiles= # comma list of active profiles

2.3 系统配置相关参数

spring.main.sources=
spring.main.web-environment= # detect by default
spring.main.show-banner=true
spring.main…= # see class for all properties

2.4 日志配置相关参数

logging.path=/var/logs
logging.file=myapp.log
logging.config=IDENTITY (ContextIdApplicationContextInitializer)

spring.application.name=
spring.application.index=

2.5 tomcat相关配置参数

server.port=8080
server.address= # bind to a specific NIC
server.session-timeout= # session timeout in seconds
server.context-path= # the context path, defaults to ‘/’
server.servlet-path= # the servlet path, defaults to ‘/’
server.tomcat.access-log-pattern= # log pattern of the access log
server.tomcat.access-log-enabled=false # is access logging enabled
server.tomcat.protocol-header=x-forwarded-proto # ssl forward headers
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp)
server.tomcat.background-processor-delay=30; # in seconds
server.tomcat.max-threads = 0 # number of threads in protocol handler
server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding

2.6 springmvc相关配置参数

http.mappers.json-pretty-print=false # pretty print JSON
http.mappers.json-sort-keys=false # sort keys
spring.mvc.locale= # set fixed locale, e.g. en_UK
spring.mvc.date-format= # set fixed date format, e.g. dd/MM/yyyy
spring.mvc.message-codes-resolver-format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.view.prefix= # MVC view prefix
spring.view.suffix= # … and suffix
spring.resources.cache-period= # cache timeouts in headers sent to browser
spring.resources.add-mappings=true # if default mappings should be added

2.7 thymeleaf相关配置参数

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset= is added
spring.thymeleaf.cache=true # set to false for hot refresh

2.8 freemark相关配置参数

spring.freemarker.allowRequestOverride=false
spring.freemarker.allowSessionOverride=false
spring.freemarker.cache=true
spring.freemarker.checkTemplateLocation=true
spring.freemarker.contentType=text/html
spring.freemarker.exposeRequestAttributes=false
spring.freemarker.exposeSessionAttributes=false
spring.freemarker.exposeSpringMacroHelpers=false
spring.freemarker.prefix=
spring.freemarker.requestContextAttribute=
spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
spring.freemarker.templateEncoding=UTF-8
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.viewNames= # whitelist of view names that can be resolved

2.9 groovy模板相关配置参数

spring.groovy.template.allowRequestOverride=false
spring.groovy.template.allowSessionOverride=false
spring.groovy.template.cache=true
spring.groovy.template.configuration.*= # See Groovy’s TemplateConfiguration
spring.groovy.template.contentType=text/html
spring.groovy.template.prefix=classpath:/templates/
spring.groovy.template.suffix=.tpl
spring.groovy.template.templateEncoding=UTF-8
spring.groovy.template.viewNames= # whitelist of view names that can be resolved

2.10 velocity模板相关配置参数

spring.velocity.allowRequestOverride=false
spring.velocity.allowSessionOverride=false
spring.velocity.cache=true
spring.velocity.checkTemplateLocation=true
spring.velocity.contentType=text/html
spring.velocity.dateToolAttribute=
spring.velocity.exposeRequestAttributes=false
spring.velocity.exposeSessionAttributes=false
spring.velocity.exposeSpringMacroHelpers=false
spring.velocity.numberToolAttribute=
spring.velocity.prefix=
spring.velocity.properties.*=
spring.velocity.requestContextAttribute=
spring.velocity.resourceLoaderPath=classpath:/templates/
spring.velocity.suffix=.vm
spring.velocity.templateEncoding=UTF-8
spring.velocity.viewNames= # whitelist of view names that can be resolved

2.11 INTERNATIONALIZATION

spring.messages.basename=messages
spring.messages.cacheSeconds=-1
spring.messages.encoding=UTF-8

2.12 安全相关配置参数

security.user.name=user # login username
security.user.password= # login password
security.user.role=USER # role assigned to the user
security.require-ssl=false # advanced settings …
security.enable-csrf=false
security.basic.enabled=true
security.basic.realm=Spring
security.basic.path= #
security.headers.xss=false
security.headers.cache=false
security.headers.frame=false
security.headers.contentType=false
security.headers.hsts=all # none / domain / all
security.sessions=stateless # always / never / if_required / stateless
security.ignored=false

2.13 数据源相关配置参数

spring.datasource.name= # name of the data source
spring.datasource.initialize=true # populate using data.sql
spring.datasource.schema= # a schema (DDL) script resource reference
spring.datasource.data= # a data (DML) script resource reference
spring.datasource.platform= # the platform to use in the schema resource (schema-${platform}.sql)
spring.datasource.continueOnError=false # continue even if can’t be initialized
spring.datasource.separator=; # statement separator in SQL initialization scripts
spring.datasource.driverClassName= # JDBC Settings…
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.max-active=100 # Advanced configuration…
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
spring.datasource.validation-query=
spring.datasource.test-on-borrow=false
spring.datasource.test-on-return=false
spring.datasource.test-while-idle=
spring.datasource.time-between-eviction-runs-millis=
spring.datasource.min-evictable-idle-time-millis=
spring.datasource.max-wait-millis=

2.14 mongdb相关配置参数

spring.data.mongodb.host= # the db host
spring.data.mongodb.port=27017 # the connection port (defaults to 27107)
spring.data.mongodb.uri=mongodb://localhost/test # connection URL
spring.data.mongo.repositories.enabled=true # if spring data repository support is enabled

2.15 springDataJPA相关配置参数

spring.jpa.properties.*= # properties to set on the JPA connection
spring.jpa.openInView=true
spring.jpa.show-sql=true
spring.jpa.database-platform=
spring.jpa.database=
spring.jpa.generate-ddl=false # ignored by Hibernate, might be useful for other vendors
spring.jpa.hibernate.naming-strategy= # naming classname
spring.jpa.hibernate.ddl-auto= # defaults to create-drop for embedded dbs
spring.data.jpa.repositories.enabled=true # if spring data repository support is enabled

2.16 solr相关配置参数

spring.data.solr.host=http://127.0.0.1:8983/solr
spring.data.solr.zkHost=
spring.data.solr.repositories.enabled=true # if spring data repository support is enabled

2.17 elasticsearch相关配置参数

spring.data.elasticsearch.cluster-name= # The cluster name (defaults to elasticsearch)
spring.data.elasticsearch.cluster-nodes= # The address(es) of the server node (comma-separated; if not specified starts a client node)
spring.data.elasticsearch.local=true # if local mode should be used with client nodes
spring.data.elasticsearch.repositories.enabled=true # if spring data repository support is enabled

2.18 flyway相关配置参数

flyway.locations=classpath:db/migrations # locations of migrations scripts
flyway.schemas= # schemas to update
flyway.initVersion= 1 # version to start migration
flyway.prefix=V
flyway.suffix=.sql
flyway.enabled=true
flyway.url= # JDBC url if you want Flyway to create its own DataSource
flyway.user= # JDBC username if you want Flyway to create its own DataSource
flyway.password= # JDBC password if you want Flyway to create its own DataSource

2.19 liquibase相关配置参数

liquibase.change-log=classpath:/db/changelog/db.changelog-master.yaml
liquibase.contexts= # runtime contexts to use
liquibase.default-schema= # default database schema to use
liquibase.drop-first=false
liquibase.enabled=true

2.20 JMX

spring.jmx.enabled=true # Expose MBeans from Spring

2.21rabbitmq相关配置参数

spring.rabbitmq.host= # connection host
spring.rabbitmq.port= # connection port
spring.rabbitmq.addresses= # connection addresses (e.g. myhost:9999,otherhost:1111)
spring.rabbitmq.username= # login user
spring.rabbitmq.password= # login password
spring.rabbitmq.virtualhost=
spring.rabbitmq.dynamic=

2.22 redis相关配置参数

spring.redis.host=localhost # server host
spring.redis.password= # server password
spring.redis.port=6379 # connection port
spring.redis.pool.max-idle=8 # pool settings …
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1

2.23 activemq相关配置参数

spring.activemq.broker-url=tcp://localhost:61616 # connection URL
spring.activemq.user=
spring.activemq.password=
spring.activemq.in-memory=true # broker kind to create if no broker-url is specified
spring.activemq.pooled=false

2.24 hornetq相关配置参数

spring.hornetq.mode= # connection mode (native, embedded)
spring.hornetq.host=localhost # hornetQ host (native mode)
spring.hornetq.port=5445 # hornetQ port (native mode)
spring.hornetq.embedded.enabled=true # if the embedded server is enabled (needs hornetq-jms-server.jar)
spring.hornetq.embedded.serverId= # auto-generated id of the embedded server (integer)
spring.hornetq.embedded.persistent=false # message persistence
spring.hornetq.embedded.data-directory= # location of data content (when persistence is enabled)
spring.hornetq.embedded.queues= # comma separate queues to create on startup
spring.hornetq.embedded.topics= # comma separate topics to create on startup
spring.hornetq.embedded.cluster-password= # customer password (randomly generated by default)

2.25 JMS

spring.jms.pub-sub-domain= # false for queue (default), true for topic

2.26 springbatch相关配置参数

spring.batch.job.names=job1,job2
spring.batch.job.enabled=true
spring.batch.initializer.enabled=true
spring.batch.schema= # batch schema to load

2.27 aop相关配置参数

spring.aop.auto=
spring.aop.proxy-target-class=

2.28 FILE ENCODING

spring.mandatory-file-encoding=false

2.29 SPRING SOCIAL

spring.social.auto-connection-views=true # Set to true for default connection views or false if you provide your own

2.30 SPRING SOCIAL FACEBOOK

spring.social.facebook.app-id= # your application’s Facebook App ID
spring.social.facebook.app-secret= # your application’s Facebook App Secret

2.31 SPRING SOCIAL LINKEDIN

spring.social.linkedin.app-id= # your application’s LinkedIn App ID
spring.social.linkedin.app-secret= # your application’s LinkedIn App Secret

2.32 SPRING SOCIAL TWITTER

spring.social.twitter.app-id= # your application’s Twitter App ID
spring.social.twitter.app-secret= # your application’s Twitter App Secret

2.33 SPRING MOBILE SITE PREFERENCE

spring.mobile.sitepreference.enabled=true # enabled by default

2.34 SPRING MOBILE DEVICE VIEWS

spring.mobile.devicedelegatingviewresolver.enabled=true # disabled by default
spring.mobile.devicedelegatingviewresolver.normalPrefix=
spring.mobile.devicedelegatingviewresolver.normalSuffix=
spring.mobile.devicedelegatingviewresolver.mobilePrefix=mobile/
spring.mobile.devicedelegatingviewresolver.mobileSuffix=
spring.mobile.devicedelegatingviewresolver.tabletPrefix=tablet/
spring.mobile.devicedelegatingviewresolver.tabletSuffix=
----------------------------------------
ACTUATOR PROPERTIES
----------------------------------------

2.35 MANAGEMENT HTTP SERVER

management.port= # defaults to ‘server.port’
management.address= # bind to a specific NIC
management.contextPath= # default to ‘/

2.36 ENDPOINTS

endpoints.autoconfig.id=autoconfig
endpoints.autoconfig.sensitive=true
endpoints.autoconfig.enabled=true
endpoints.beans.id=beans
endpoints.beans.sensitive=true
endpoints.beans.enabled=true
endpoints.configprops.id=configprops
endpoints.configprops.sensitive=true
endpoints.configprops.enabled=true
endpoints.configprops.keys-to-sanitize=password,secret
endpoints.dump.id=dump
endpoints.dump.sensitive=true
endpoints.dump.enabled=true
endpoints.env.id=env
endpoints.env.sensitive=true
endpoints.env.enabled=true
endpoints.health.id=health
endpoints.health.sensitive=false
endpoints.health.enabled=true
endpoints.info.id=info
endpoints.info.sensitive=false
endpoints.info.enabled=true
endpoints.metrics.id=metrics
endpoints.metrics.sensitive=true
endpoints.metrics.enabled=true
endpoints.shutdown.id=shutdown
endpoints.shutdown.sensitive=true
endpoints.shutdown.enabled=false
endpoints.trace.id=trace
endpoints.trace.sensitive=true
endpoints.trace.enabled=true

2.37 MVC ONLY ENDPOINTS

endpoints.jolokia.path=jolokia
endpoints.jolokia.sensitive=true
endpoints.jolokia.enabled=true # when using Jolokia
endpoints.error.path=/error

2.38 JMX ENDPOINT

endpoints.jmx.enabled=true
endpoints.jmx.domain= # the JMX domain, defaults to ‘org.springboot’
endpoints.jmx.unique-names=false
endpoints.jmx.enabled=true
endpoints.jmx.staticNames=

2.39 JOLOKIA

jolokia.config.*= # See Jolokia manual

2.40 REMOTE SHELL

shell.auth=simple # jaas, key, simple, spring
shell.command-refresh-interval=-1
shell.command-path-pattern= # classpath*:/commands/, classpath*:/crash/commands/
shell.config-path-patterns= # classpath*:/crash/
shell.disabled-plugins=false # don’t expose plugins
shell.ssh.enabled= # ssh settings …
shell.ssh.keyPath=
shell.ssh.port=
shell.telnet.enabled= # telnet settings …
shell.telnet.port=
shell.auth.jaas.domain= # authentication settings …
shell.auth.key.path=
shell.auth.simple.user.name=
shell.auth.simple.user.password=
shell.auth.spring.roles=

2.41 GIT INFO

spring.git.properties= # resource ref to generated git info properties file
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TwoYellowGuns

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值