Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure MySQL in Spring Boot Framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "Spring Boot framework how to configure MySQL", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Spring Boot framework how to configure MySQL" bar!

Spring Boot framework configuration MySQLRainbond open source software introduction: Rainbond is the first open source production-level serverless PaaS in China. Deeply integrate technologies such as Kubernetes-based container management, construction and delivery of multi-type CI/CD applications, and resource management of multiple data centers to provide full lifecycle solutions for cloud native applications and build an ecosystem of connectivity between applications and infrastructure, applications and infrastructure. On December 12th, Rainbond was officially open source. [Rainbond details] (http://www.rainbond.com/docs/stable/getting-started/design-concept.html)[Rainbond project address] (https://github.com/goodrain/rainbond)

The Spring Boot framework simplifies the initial construction and development process of new Spring applications. Cloud help supports the deployment of Spring Boot applications on the platform.

Cloud help provides an example of Spring Boot configuring MySQL service, and one-click deployment of Spring Boot-MySQL to the cloud city

The following is the process of creating and running the example locally:

Create an example

Create an example using spring-boot-cli

$docker run-it-- rm\-v $PWD:/app goodrainapps/spring-boot-cli:1.5.9 spring init-- dependencies=web spring-boot-mysql-demo

Enter the directory where the sample class files are stored

$cd spring-boot-mysql-demo/src/main/java/com/example/springbootmysqldemo

Add DemoApplication.java

@ Controller@SpringBootApplicationpublic class DemoApplication {@ RequestMapping ("/") @ ResponseBody String home () {return "Hello World!";} public static void main (String [] args) {SpringApplication.run (DemoApplication.class, args);}} build example

To speed up the construction of maven, a domestic mirror has been added to setting.xml. Copy the setting.xml to your spring-boot-mysql-demo.

$cd spring-boot-mysql-demo$ docker run-it-- rm\-v "$PWD": / app/build\-w / app/build maven:3.5.2-jdk-7-alpine mvn-B-DskipTests=true-s settings.xml clean install run

Execute the following command to run the Hello World example

$cd spring-boot-mysql-demo$ docker run-it-- rm-v $PWD:/app-w / app-p 8080pur8080 goodrainapps/openjdk:8u131-jre-alpine java-jar target/*.jar

Visit http://localhost:8080 to view the running results.

Configuration database

The configuration directory structure of Spring-boot-mysql-demo provided by Cloud Pang is as follows, and the content of the configuration file is for reference only.

For more information on configuration, please see below:

Connect MySQL

Add the following to connect this application to the database.

Add mysql database service to pom.xml:

Mysql mysql-connector-java 5.1.9

Add JDBC drivers:

Org.springframework.boot spring-boot-starter-jdbc

Add database connection information to application.properties:

Spring.datasource.url=jdbc:mysql://$ {MYSQL_HOST}: ${MSYQL_PORT} / demo?createDatabaseIfNotExist=truespring.datasource.username=$ {MYSQL_USER} spring.datasource.password=$ {MYSQL_PASS} spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.maxActive=10spring.datasource.maxIdle=5spring.datasource.minIdle=2spring.datasource.initialSize=5spring.datasource.removeAbandoned=true

Add DatabaseConfig.java to the source code

@ Configurationpublic class DatabaseConfig {@ Bean @ Primary @ ConfigurationProperties (prefix = "spring.datasource") public DataSource dataSource () {return new org.apache.tomcat.jdbc.pool.DataSource ()} Database initialization

Use JPA to manage the code that generates mapping relationships for entities.

Org.springframework.boot spring-boot-starter-data-jpa

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report