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 build a springboot-mysql project framework based on docker environment

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to build a springboot-mysql project framework based on docker environment". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the cloud age, applications created by developers must be easily distributed on the network, that is, applications must be detached from the display of the underlying physical hardware, and must be accessible "anytime, anywhere". We usually want to build a project framework quickly, and it takes a certain amount of energy to build the database environment. In order to isolate from the external system environment, the recommended use of docker can make us pay more attention to business processing.

Experimental environment

Docker 17.12.0-ce

Mysql Image version 5.7

Jdk 1.8

Springboot 2.1.4.RELEASE

Development tool: IntelliJ IDEA

Environment building

First of all, install docker, there are many ways to install docker online, I will not repeat it here.

After building the docker environment, pull the mysql image as follows:

Docker pull mysql:5.7

If you do not specify a mysql version, the latest latest version will be pulled by default. Check whether the mysql image is installed successfully:

Docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEmysql 5.7 f6509bac4980 11 days ago 373MB

Run the container, where the port mapping is set and the initial password of mysql is set. The default user name is root:

Docker run-d-- name mysql-p 3306 MYSQL_ROOT_PASSWORD=123456 mysql:5.7

Check the container running status:

Docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe87ae06931c8 mysql:5.7 "docker-entrypoint.s..." 4 seconds ago Up 3 seconds 0.0.0.0 3306/tcp 3306-> 33060/tcp mysql

The container is running normally, enter the container, create a database test and a test table tbl_test:

Docker exec-it mysql / bin/bashroot@e87ae06931c8:/# mysql-uroot-pmysql > create database test;mysql > create table operatelog (id INT, userId varchar, operateDesc varchar)

In this way, the mysql environment is built.

Actual combat source code

The springboot-mysql project under https://github.com/vincentduan/mavenProject

Combing the actual combat steps

The steps of this actual combat are as follows:

Create a springboot project

Create a dao and connect to the mysql using JdbcTemplate

Create the interface and implementation of the Service layer

Create a mysql connection pool configuration

Create a controller and develop a http service interface that invokes services in the service layer

Package the springboot project using docker encapsulation.

Create a springboot project

To create a springboot web project springboot-mysql,pom.xml with IntelliJ IDEA, the contents are as follows:

4.0.0 com.vincent springboot-mysql 1.0-SNAPSHOT UTF-8 1.8 1.8 org.springframework.boot spring-boot-dependencies 2.1.4.RELEASE import pom Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-configuration-processor true mysql mysql-connector-java org.springframework.boot spring-boot-starter- Jdbc com.alibaba druid 1.1.6 com.alibaba fastjson 1.2.40 org.springframework.boot spring-boot-maven-plugin 2.1. 4.RELEASE cn.ac.iie.App repackage Org.apache.maven.plugins maven-surefire-plugin true creates Dao layer @ Repositorypublic class OperatelogDao {@ Autowired private JdbcTemplate jdbcTemplate Public void addOperatelog (int id) {String sql = "insert into operatelog (id) values (" + id + "); jdbcTemplate.execute (sql);} create an interface to the Service layer and implement public interface OperatelogService {/ / Test simply inserting a piece of data void insert (int id);}

The corresponding implementation class is as follows:

@ Servicepublic class OperatelogServiceImpl implements OperatelogService {@ Autowired OperatelogDao operatelogDao; public void insert (int id) {operatelogDao.addOperatelog (id);}}

What this method does is simple: insert a piece of data

Mysql connection pool configuration

Add a configuration in application.properties:

The IP address of the server where spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://docker resides: 3306/testspring.datasource.username=rootspring.datasource.password=123456

Configure the connection pool, create a new configuration class, and assemble the properties in the configuration file into the connection pool, as follows:

@ SpringBootConfigurationpublic class DBConfiguration {@ Autowired private Environment environment; @ Bean public DataSource createDataSource () {DruidDataSource druidDataSource = new DruidDataSource (); druidDataSource.setUrl (environment.getProperty ("spring.datasource.url")); druidDataSource.setUsername (environment.getProperty ("spring.datasource.username")); druidDataSource.setPassword (environment.getProperty ("spring.datasource.password")); druidDataSource.setDriverClassName (environment.getProperty ("spring.datasource.driver-class-name")) Return druidDataSource;}} create controller

Create a controller as OperatelogController, which defines a http interface, and invokes the services of the Service layer, as follows:

@ RestController@RequestMapping ("operatelog") public class OperatelogController {@ Autowired OperatelogService operatelogService; @ GetMapping ("insert") public String insert (@ RequestParam ("id") int id) {operatelogService.queryOperateDesc (); return "SUCCESS";}} verification effect

Run the springboot (execute mvn spring-boot:run under the folder where the pom.xml is located)

Enter: http://localhost:8080/operatelog/insert?id=1 in the browser

SUCCESS is returned in the browser to check that there is one more piece of data in the database; the execution is successful

In addition,

If you want to put the current springboot into a docker container to run, you can also package the current project into jar, upload it to the server, create a new directory, and then create a new Dockerfile file.

The Dockerfile content is as follows:

FROM java:8-alpineADD test-0.0.1-SNAPSHOT.jar app.jarEXPOSE 8080ENTRYPOINT ["java", "- jar", "/ app.jar"]

Under the current directory, build the image, and then run:

Docker build-t. Docker run-d-p 8088 springboot-mysql 8080 test "how to build a springboot-mysql project framework based on springboot-mysql environment" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report