From df3c881e20f5b32778d4596dc13decd7c0c604c2 Mon Sep 17 00:00:00 2001 From: codeyu Date: Tue, 10 Apr 2018 15:33:34 +0800 Subject: [PATCH 1/3] improve --- app/src/main/resources/application.properties | 11 +++++++++-- app/src/main/resources/templates/index.ftl | 5 ++++- docker-compose.yaml | 7 ++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/src/main/resources/application.properties b/app/src/main/resources/application.properties index ccc21b3..aa5c3d1 100644 --- a/app/src/main/resources/application.properties +++ b/app/src/main/resources/application.properties @@ -1,5 +1,12 @@ -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 diff --git a/app/src/main/resources/templates/index.ftl b/app/src/main/resources/templates/index.ftl index 6e29a58..cf3810b 100644 --- a/app/src/main/resources/templates/index.ftl +++ b/app/src/main/resources/templates/index.ftl @@ -1,11 +1,14 @@ +<#assign base = request.contextPath /> Docker Compose with Spring Boot, MySQL, NGINX + + -

Docker Compose with Spring Boot, MySQL, NGINX

+

Docker Compose with Spring Boot, MySQL, NGINX

\ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index aecf61b..75991c4 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,10 +5,12 @@ services: image: nginx:1.13 restart: always ports: - - 80:80 - - 443:443 + - "80:80" + - "443:443" volumes: - ./nginx/conf.d:/etc/nginx/conf.d + depends_on: + - app mysql: container_name: some-mysql @@ -32,5 +34,4 @@ services: - "8080" command: mvn clean spring-boot:run depends_on: - - nginx - mysql From c6f00b8f90ca0451b3373ecb4e8fc69ff5fa423e Mon Sep 17 00:00:00 2001 From: codeyu Date: Tue, 10 Apr 2018 18:10:42 +0800 Subject: [PATCH 2/3] up --- README.md | 2 +- app/pom.xml | 28 +++++++-- .../springboot/controller/UserController.java | 23 ++++++++ .../hellokoding/springboot/domain/User.java | 57 +++++++++++++++++++ .../springboot/repository/UserRepository.java | 8 +++ .../springboot/service/UserService.java | 9 +++ .../service/impl/UserServiceImpl.java | 24 ++++++++ app/src/main/resources/application.properties | 2 +- .../main/resources/db/migration/V1__init.sql | 8 +++ .../resources/db/migration/V2__testdata.sql | 2 + docker-compose.yaml | 2 +- 11 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/hellokoding/springboot/controller/UserController.java create mode 100644 app/src/main/java/com/hellokoding/springboot/domain/User.java create mode 100644 app/src/main/java/com/hellokoding/springboot/repository/UserRepository.java create mode 100644 app/src/main/java/com/hellokoding/springboot/service/UserService.java create mode 100644 app/src/main/java/com/hellokoding/springboot/service/impl/UserServiceImpl.java create mode 100644 app/src/main/resources/db/migration/V1__init.sql create mode 100644 app/src/main/resources/db/migration/V2__testdata.sql diff --git a/README.md b/README.md index c7c38ff..271b3af 100644 --- a/README.md +++ b/README.md @@ -16,4 +16,4 @@ ## Run - Run command `docker-compose up` -- Access to http://localhost/ +- Access to http://localhost:8084/ diff --git a/app/pom.xml b/app/pom.xml index 0c98b9f..c8d4939 100644 --- a/app/pom.xml +++ b/app/pom.xml @@ -5,14 +5,17 @@ dockercompose-springboot-mysql-nginx dockercompose-springboot-mysql-nginx - org.springframework.boot - spring-boot-starter-parent - 1.5.8.RELEASE - + org.springframework.boot + spring-boot-starter-parent + 2.0.0.RELEASE + + - 1.8 - + UTF-8 + UTF-8 + 1.8 + @@ -23,10 +26,23 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.flywaydb + flyway-core + + + org.springframework.boot + spring-boot-starter-web + mysql mysql-connector-java + + org.springframework.boot + spring-boot-starter-test + test + diff --git a/app/src/main/java/com/hellokoding/springboot/controller/UserController.java b/app/src/main/java/com/hellokoding/springboot/controller/UserController.java new file mode 100644 index 0000000..3d0aba1 --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/controller/UserController.java @@ -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); + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/hellokoding/springboot/domain/User.java b/app/src/main/java/com/hellokoding/springboot/domain/User.java new file mode 100644 index 0000000..481b7d1 --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/domain/User.java @@ -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; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/hellokoding/springboot/repository/UserRepository.java b/app/src/main/java/com/hellokoding/springboot/repository/UserRepository.java new file mode 100644 index 0000000..f568d39 --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/repository/UserRepository.java @@ -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 findByUsername(String username); +} \ No newline at end of file diff --git a/app/src/main/java/com/hellokoding/springboot/service/UserService.java b/app/src/main/java/com/hellokoding/springboot/service/UserService.java new file mode 100644 index 0000000..18e7af4 --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/service/UserService.java @@ -0,0 +1,9 @@ +package com.hellokoding.springboot.service; + +import com.hellokoding.springboot.domain.User; + + +public interface UserService { + + User findUserByName(String username); +} \ No newline at end of file diff --git a/app/src/main/java/com/hellokoding/springboot/service/impl/UserServiceImpl.java b/app/src/main/java/com/hellokoding/springboot/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..f914404 --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/service/impl/UserServiceImpl.java @@ -0,0 +1,24 @@ +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; + +/** + * 城市业务逻辑实现类 + * + * Created by xchunzhao on 02/05/2017. + */ +@Service +public class UserServiceImpl implements UserService { + + @Autowired + private UserRepository UserRepository; + + public User findUserByName(String username) { + return UserRepository.findByUsername(username); + } + +} \ No newline at end of file diff --git a/app/src/main/resources/application.properties b/app/src/main/resources/application.properties index aa5c3d1..9180e9e 100644 --- a/app/src/main/resources/application.properties +++ b/app/src/main/resources/application.properties @@ -13,7 +13,7 @@ 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 \ No newline at end of file diff --git a/app/src/main/resources/db/migration/V1__init.sql b/app/src/main/resources/db/migration/V1__init.sql new file mode 100644 index 0000000..26643b8 --- /dev/null +++ b/app/src/main/resources/db/migration/V1__init.sql @@ -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; \ No newline at end of file diff --git a/app/src/main/resources/db/migration/V2__testdata.sql b/app/src/main/resources/db/migration/V2__testdata.sql new file mode 100644 index 0000000..7e3434f --- /dev/null +++ b/app/src/main/resources/db/migration/V2__testdata.sql @@ -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'); \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 75991c4..af60e1b 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,7 +5,7 @@ services: image: nginx:1.13 restart: always ports: - - "80:80" + - "8084:80" - "443:443" volumes: - ./nginx/conf.d:/etc/nginx/conf.d From 65389424d762f6fe11c975ba9c2abfdce3e1a9c2 Mon Sep 17 00:00:00 2001 From: zhu yu Date: Wed, 11 Apr 2018 17:50:50 +0800 Subject: [PATCH 3/3] Update UserServiceImpl.java --- .../springboot/service/impl/UserServiceImpl.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/src/main/java/com/hellokoding/springboot/service/impl/UserServiceImpl.java b/app/src/main/java/com/hellokoding/springboot/service/impl/UserServiceImpl.java index f914404..e58d5d3 100644 --- a/app/src/main/java/com/hellokoding/springboot/service/impl/UserServiceImpl.java +++ b/app/src/main/java/com/hellokoding/springboot/service/impl/UserServiceImpl.java @@ -6,11 +6,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -/** - * 城市业务逻辑实现类 - * - * Created by xchunzhao on 02/05/2017. - */ @Service public class UserServiceImpl implements UserService { @@ -21,4 +16,4 @@ public User findUserByName(String username) { return UserRepository.findByUsername(username); } -} \ No newline at end of file +}