Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

## Run
- Run command `docker-compose up`
- Access to http://localhost/
- Access to http://localhost:8084/
28 changes: 22 additions & 6 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
<name>dockercompose-springboot-mysql-nginx</name>
<description>dockercompose-springboot-mysql-nginx</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<java.version>1.8</java.version>
</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>
Expand All @@ -23,10 +26,23 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.hellokoding.springboot.controller;

import com.hellokoding.springboot.domain.User;
import com.hellokoding.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

@Autowired
private UserService UserService;

@RequestMapping(value = "/api/user", method = RequestMethod.GET)
public User findOneUser(@RequestParam(value = "userName", required = true) String userName) {
return UserService.findUserByName(userName);
}

}
57 changes: 57 additions & 0 deletions app/src/main/java/com/hellokoding/springboot/domain/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.hellokoding.springboot.domain;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank
@Column(unique = true)
@Size(min = 1, max = 100)
private String username;

@NotBlank
@Size(max = 50)
private String firstName;

@Size(max = 50)
private String lastName;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hellokoding.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.hellokoding.springboot.domain.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hellokoding.springboot.service;

import com.hellokoding.springboot.domain.User;


public interface UserService {

User findUserByName(String username);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.hellokoding.springboot.service.impl;

import com.hellokoding.springboot.repository.UserRepository;
import com.hellokoding.springboot.domain.User;
import com.hellokoding.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository UserRepository;

public User findUserByName(String username) {
return UserRepository.findByUsername(username);
}

}
13 changes: 10 additions & 3 deletions app/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
spring.freemarker.template-loader-path: classpath:/templates
spring.freemarker.suffix: .ftl

spring.freemarker.suffix=.ftl
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request

spring.datasource.url=jdbc:mysql://mysql:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=hellokoding
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
8 changes: 8 additions & 0 deletions app/src/main/resources/db/migration/V1__init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE users (
id bigint(20) NOT NULL AUTO_INCREMENT,
username varchar(100) NOT NULL,
first_name varchar(50) NOT NULL,
last_name varchar(50) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY UK_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2 changes: 2 additions & 0 deletions app/src/main/resources/db/migration/V2__testdata.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSERT INTO users(username, first_name, last_name) VALUES('jeffer', 'yun', 'zhao');
INSERT INTO users(username, first_name, last_name) VALUES('codeyu', 'wukong', 'sun');
5 changes: 4 additions & 1 deletion app/src/main/resources/templates/index.ftl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<#assign base = request.contextPath />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Docker Compose with Spring Boot, MySQL, NGINX</title>
<link rel="stylesheet" type="text/css" href="${base}/css/main.css"/>
<script type="text/javascript" src="${base}/js/main.js"></script>
</head>
<body>
<h1>Docker Compose with Spring Boot, MySQL, NGINX</h1>
<h1 class="hello-title">Docker Compose with Spring Boot, MySQL, NGINX</h1>
<p></p>
</body>
</html>
7 changes: 4 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ services:
image: nginx:1.13
restart: always
ports:
- 80:80
- 443:443
- "8084:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
depends_on:
- app

mysql:
container_name: some-mysql
Expand All @@ -32,5 +34,4 @@ services:
- "8080"
command: mvn clean spring-boot:run
depends_on:
- nginx
- mysql