diff --git a/spring-boot-data-jpa-mysql/.gitignore b/spring-boot-data-jpa-mysql/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..82eca336e352c9026decda294ff678968050edfc --- /dev/null +++ b/spring-boot-data-jpa-mysql/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/spring-boot-data-jpa-mysql/.mvn/wrapper/maven-wrapper.jar b/spring-boot-data-jpa-mysql/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 0000000000000000000000000000000000000000..01e67997377a393fd672c7dcde9dccbedf0cb1e9 Binary files /dev/null and b/spring-boot-data-jpa-mysql/.mvn/wrapper/maven-wrapper.jar differ diff --git a/spring-boot-data-jpa-mysql/.mvn/wrapper/maven-wrapper.properties b/spring-boot-data-jpa-mysql/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000000000000000000000000000000000000..71793467167cbb12f5e5b4fdc98309da02da1c16 --- /dev/null +++ b/spring-boot-data-jpa-mysql/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip diff --git a/spring-boot-data-jpa-mysql/mvnw b/spring-boot-data-jpa-mysql/mvnw new file mode 100644 index 0000000000000000000000000000000000000000..5551fde8e7dba1e37a1821e8b26374893e2a9e2e --- /dev/null +++ b/spring-boot-data-jpa-mysql/mvnw @@ -0,0 +1,286 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven2 Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + # TODO classpath? +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + wget "$jarUrl" -O "$wrapperJarPath" + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + curl -o "$wrapperJarPath" "$jarUrl" + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/spring-boot-data-jpa-mysql/mvnw.cmd b/spring-boot-data-jpa-mysql/mvnw.cmd new file mode 100644 index 0000000000000000000000000000000000000000..e5cfb0ae9eab80c314b7d79c164d4a00772824b8 --- /dev/null +++ b/spring-boot-data-jpa-mysql/mvnw.cmd @@ -0,0 +1,161 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven2 Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" +FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + echo Found %WRAPPER_JAR% +) else ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" + echo Finished downloading %WRAPPER_JAR% +) +@REM End of extension + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/spring-boot-data-jpa-mysql/pom.xml b/spring-boot-data-jpa-mysql/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..c3408a242d1b4df20703cf3968975b77e3decc6b --- /dev/null +++ b/spring-boot-data-jpa-mysql/pom.xml @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>com.luv2code.springboot</groupId> + <artifactId>cruddemo</artifactId> + <version>0.0.1-SNAPSHOT</version> + <packaging>jar</packaging> + + <name>cruddemo</name> + <description>Demo project for Spring Boot</description> + + <parent> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-parent</artifactId> + <version>2.1.0.RELEASE</version> + <relativePath/> <!-- lookup parent from repository --> + </parent> + + <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> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-devtools</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> + + +</project> diff --git a/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/CruddemoApplication.java b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/CruddemoApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..dbcf3dad9ca34815650515a88267364f5fdb8860 --- /dev/null +++ b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/CruddemoApplication.java @@ -0,0 +1,12 @@ +package com.luv2code.springboot.cruddemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CruddemoApplication { + + public static void main(String[] args) { + SpringApplication.run(CruddemoApplication.class, args); + } +} diff --git a/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/dao/EmployeeRepository.java b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/dao/EmployeeRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..34b155d71e448c2fee6da985590073beeea61335 --- /dev/null +++ b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/dao/EmployeeRepository.java @@ -0,0 +1,11 @@ +package com.luv2code.springboot.cruddemo.dao; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.luv2code.springboot.cruddemo.entity.Employee; + +public interface EmployeeRepository extends JpaRepository<Employee, Integer> { + + // that's it ... no need to write any code LOL! + +} diff --git a/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/entity/Employee.java b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/entity/Employee.java new file mode 100644 index 0000000000000000000000000000000000000000..deffc00237968b78480cdb757bda7f0edc690b54 --- /dev/null +++ b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/entity/Employee.java @@ -0,0 +1,95 @@ +package com.luv2code.springboot.cruddemo.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="employee") +public class Employee { + + // define fields + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name="id") + private int id; + + @Column(name="first_name") + private String firstName; + + @Column(name="last_name") + private String lastName; + + @Column(name="email") + private String email; + + + // define constructors + + public Employee() { + + } + + public Employee(String firstName, String lastName, String email) { + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + } + + // define getter/setter + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + 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; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + // define tostring + + @Override + public String toString() { + return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]"; + } + +} + + + + + + + + + + + diff --git a/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/rest/EmployeeRestController.java b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/rest/EmployeeRestController.java new file mode 100644 index 0000000000000000000000000000000000000000..244568dac694f6a3e44336e7853a79e3a39bfd05 --- /dev/null +++ b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/rest/EmployeeRestController.java @@ -0,0 +1,102 @@ +package com.luv2code.springboot.cruddemo.rest; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.luv2code.springboot.cruddemo.entity.Employee; +import com.luv2code.springboot.cruddemo.service.EmployeeService; + +@RestController +@RequestMapping("/api") +public class EmployeeRestController { + + private EmployeeService employeeService; + + @Autowired + public EmployeeRestController(EmployeeService theEmployeeService) { + employeeService = theEmployeeService; + } + + // expose "/employees" and return list of employees + @GetMapping("/employees") + public List<Employee> findAll() { + return employeeService.findAll(); + } + + // add mapping for GET /employees/{employeeId} + + @GetMapping("/employees/{employeeId}") + public Employee getEmployee(@PathVariable int employeeId) { + + Employee theEmployee = employeeService.findById(employeeId); + + if (theEmployee == null) { + throw new RuntimeException("Employee id not found - " + employeeId); + } + + return theEmployee; + } + + // add mapping for POST /employees - add new employee + + @PostMapping("/employees") + public Employee addEmployee(@RequestBody Employee theEmployee) { + + // also just in case they pass an id in JSON ... set id to 0 + // this is to force a save of new item ... instead of update + + theEmployee.setId(0); + + employeeService.save(theEmployee); + + return theEmployee; + } + + // add mapping for PUT /employees - update existing employee + + @PutMapping("/employees") + public Employee updateEmployee(@RequestBody Employee theEmployee) { + + employeeService.save(theEmployee); + + return theEmployee; + } + + // add mapping for DELETE /employees/{employeeId} - delete employee + + @DeleteMapping("/employees/{employeeId}") + public String deleteEmployee(@PathVariable int employeeId) { + + Employee tempEmployee = employeeService.findById(employeeId); + + // throw exception if null + + if (tempEmployee == null) { + throw new RuntimeException("Employee id not found - " + employeeId); + } + + employeeService.deleteById(employeeId); + + return "Deleted employee id - " + employeeId; + } + +} + + + + + + + + + + diff --git a/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/service/EmployeeService.java b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/service/EmployeeService.java new file mode 100644 index 0000000000000000000000000000000000000000..55e6ababcdfb2b5371adad332157a7fcc5fdc732 --- /dev/null +++ b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/service/EmployeeService.java @@ -0,0 +1,17 @@ +package com.luv2code.springboot.cruddemo.service; + +import java.util.List; + +import com.luv2code.springboot.cruddemo.entity.Employee; + +public interface EmployeeService { + + public List<Employee> findAll(); + + public Employee findById(int theId); + + public void save(Employee theEmployee); + + public void deleteById(int theId); + +} diff --git a/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/service/EmployeeServiceImpl.java b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/service/EmployeeServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..6c726d2a4b5bbfbcfbc48fe0af873bcf529e2f10 --- /dev/null +++ b/spring-boot-data-jpa-mysql/src/main/java/com/luv2code/springboot/cruddemo/service/EmployeeServiceImpl.java @@ -0,0 +1,60 @@ +package com.luv2code.springboot.cruddemo.service; + +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.luv2code.springboot.cruddemo.dao.EmployeeRepository; +import com.luv2code.springboot.cruddemo.entity.Employee; + +@Service +public class EmployeeServiceImpl implements EmployeeService { + + private EmployeeRepository employeeRepository; + + @Autowired + public EmployeeServiceImpl(EmployeeRepository theEmployeeRepository) { + employeeRepository = theEmployeeRepository; + } + + @Override + public List<Employee> findAll() { + return employeeRepository.findAll(); + } + + @Override + public Employee findById(int theId) { + Optional<Employee> result = employeeRepository.findById(theId); + + Employee theEmployee = null; + + if (result.isPresent()) { + theEmployee = result.get(); + } + else { + // we didn't find the employee + throw new RuntimeException("Did not find employee id - " + theId); + } + + return theEmployee; + } + + @Override + public void save(Employee theEmployee) { + employeeRepository.save(theEmployee); + } + + @Override + public void deleteById(int theId) { + employeeRepository.deleteById(theId); + } + +} + + + + + + diff --git a/spring-boot-data-jpa-mysql/src/main/resources/application.properties b/spring-boot-data-jpa-mysql/src/main/resources/application.properties new file mode 100644 index 0000000000000000000000000000000000000000..ce74dd9ae7428cf5ba4d95888fcdf63bac54d777 --- /dev/null +++ b/spring-boot-data-jpa-mysql/src/main/resources/application.properties @@ -0,0 +1,6 @@ +# +# JDBC properties +# +spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory?useSSL=false&serverTimezone=UTC +spring.datasource.username=springstudent +spring.datasource.password=springstudent diff --git a/spring-boot-data-jpa-mysql/src/test/java/com/luv2code/springboot/cruddemo/CruddemoApplicationTests.java b/spring-boot-data-jpa-mysql/src/test/java/com/luv2code/springboot/cruddemo/CruddemoApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..c4c21192a4428fe92ad4d690d9520051267e7fd8 --- /dev/null +++ b/spring-boot-data-jpa-mysql/src/test/java/com/luv2code/springboot/cruddemo/CruddemoApplicationTests.java @@ -0,0 +1,16 @@ +package com.luv2code.springboot.cruddemo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class CruddemoApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/springboot_01/.gitignore b/springboot_mongodb/.gitignore similarity index 100% rename from springboot_01/.gitignore rename to springboot_mongodb/.gitignore diff --git a/springboot_01/.mvn/wrapper/MavenWrapperDownloader.java b/springboot_mongodb/.mvn/wrapper/MavenWrapperDownloader.java similarity index 100% rename from springboot_01/.mvn/wrapper/MavenWrapperDownloader.java rename to springboot_mongodb/.mvn/wrapper/MavenWrapperDownloader.java diff --git a/springboot_01/.mvn/wrapper/maven-wrapper.jar b/springboot_mongodb/.mvn/wrapper/maven-wrapper.jar similarity index 100% rename from springboot_01/.mvn/wrapper/maven-wrapper.jar rename to springboot_mongodb/.mvn/wrapper/maven-wrapper.jar diff --git a/springboot_01/.mvn/wrapper/maven-wrapper.properties b/springboot_mongodb/.mvn/wrapper/maven-wrapper.properties similarity index 100% rename from springboot_01/.mvn/wrapper/maven-wrapper.properties rename to springboot_mongodb/.mvn/wrapper/maven-wrapper.properties diff --git a/springboot_01/bin/.gitignore b/springboot_mongodb/bin/.gitignore similarity index 100% rename from springboot_01/bin/.gitignore rename to springboot_mongodb/bin/.gitignore diff --git a/springboot_01/bin/.mvn/wrapper/MavenWrapperDownloader.class b/springboot_mongodb/bin/.mvn/wrapper/MavenWrapperDownloader.class similarity index 100% rename from springboot_01/bin/.mvn/wrapper/MavenWrapperDownloader.class rename to springboot_mongodb/bin/.mvn/wrapper/MavenWrapperDownloader.class diff --git a/springboot_01/bin/.mvn/wrapper/maven-wrapper.jar b/springboot_mongodb/bin/.mvn/wrapper/maven-wrapper.jar similarity index 100% rename from springboot_01/bin/.mvn/wrapper/maven-wrapper.jar rename to springboot_mongodb/bin/.mvn/wrapper/maven-wrapper.jar diff --git a/springboot_01/bin/.mvn/wrapper/maven-wrapper.properties b/springboot_mongodb/bin/.mvn/wrapper/maven-wrapper.properties similarity index 100% rename from springboot_01/bin/.mvn/wrapper/maven-wrapper.properties rename to springboot_mongodb/bin/.mvn/wrapper/maven-wrapper.properties diff --git a/springboot_01/bin/mvnw b/springboot_mongodb/bin/mvnw similarity index 100% rename from springboot_01/bin/mvnw rename to springboot_mongodb/bin/mvnw diff --git a/springboot_01/bin/mvnw.cmd b/springboot_mongodb/bin/mvnw.cmd similarity index 100% rename from springboot_01/bin/mvnw.cmd rename to springboot_mongodb/bin/mvnw.cmd diff --git a/springboot_01/bin/pom.xml b/springboot_mongodb/bin/pom.xml similarity index 100% rename from springboot_01/bin/pom.xml rename to springboot_mongodb/bin/pom.xml diff --git a/springboot_01/bin/src/main/java/com/example/springboot_01/Springboot01Application.class b/springboot_mongodb/bin/src/main/java/com/example/springboot_01/Springboot01Application.class similarity index 100% rename from springboot_01/bin/src/main/java/com/example/springboot_01/Springboot01Application.class rename to springboot_mongodb/bin/src/main/java/com/example/springboot_01/Springboot01Application.class diff --git a/springboot_01/bin/src/main/resources/application.properties b/springboot_mongodb/bin/src/main/resources/application.properties similarity index 100% rename from springboot_01/bin/src/main/resources/application.properties rename to springboot_mongodb/bin/src/main/resources/application.properties diff --git a/springboot_01/bin/src/test/java/com/example/springboot_01/Springboot01ApplicationTests.class b/springboot_mongodb/bin/src/test/java/com/example/springboot_01/Springboot01ApplicationTests.class similarity index 100% rename from springboot_01/bin/src/test/java/com/example/springboot_01/Springboot01ApplicationTests.class rename to springboot_mongodb/bin/src/test/java/com/example/springboot_01/Springboot01ApplicationTests.class diff --git a/springboot_01/mvnw b/springboot_mongodb/mvnw similarity index 100% rename from springboot_01/mvnw rename to springboot_mongodb/mvnw diff --git a/springboot_01/mvnw.cmd b/springboot_mongodb/mvnw.cmd similarity index 100% rename from springboot_01/mvnw.cmd rename to springboot_mongodb/mvnw.cmd diff --git a/springboot_01/pom.xml b/springboot_mongodb/pom.xml similarity index 100% rename from springboot_01/pom.xml rename to springboot_mongodb/pom.xml diff --git a/springboot_01/src/main/java/com/example/springboot_01/Springboot01Application.java b/springboot_mongodb/src/main/java/com/example/springboot_01/Springboot01Application.java similarity index 100% rename from springboot_01/src/main/java/com/example/springboot_01/Springboot01Application.java rename to springboot_mongodb/src/main/java/com/example/springboot_01/Springboot01Application.java diff --git a/springboot_01/src/main/java/com/example/springboot_01/controllers/StudentController.java b/springboot_mongodb/src/main/java/com/example/springboot_01/controllers/StudentController.java similarity index 100% rename from springboot_01/src/main/java/com/example/springboot_01/controllers/StudentController.java rename to springboot_mongodb/src/main/java/com/example/springboot_01/controllers/StudentController.java diff --git a/springboot_01/src/main/java/com/example/springboot_01/models/Student.java b/springboot_mongodb/src/main/java/com/example/springboot_01/models/Student.java similarity index 100% rename from springboot_01/src/main/java/com/example/springboot_01/models/Student.java rename to springboot_mongodb/src/main/java/com/example/springboot_01/models/Student.java diff --git a/springboot_01/src/main/java/com/example/springboot_01/repositories/StudentRepository.java b/springboot_mongodb/src/main/java/com/example/springboot_01/repositories/StudentRepository.java similarity index 100% rename from springboot_01/src/main/java/com/example/springboot_01/repositories/StudentRepository.java rename to springboot_mongodb/src/main/java/com/example/springboot_01/repositories/StudentRepository.java diff --git a/springboot_01/src/main/resources/application.properties b/springboot_mongodb/src/main/resources/application.properties similarity index 100% rename from springboot_01/src/main/resources/application.properties rename to springboot_mongodb/src/main/resources/application.properties diff --git a/springboot_01/src/test/java/com/example/springboot_01/Springboot01ApplicationTests.java b/springboot_mongodb/src/test/java/com/example/springboot_01/Springboot01ApplicationTests.java similarity index 100% rename from springboot_01/src/test/java/com/example/springboot_01/Springboot01ApplicationTests.java rename to springboot_mongodb/src/test/java/com/example/springboot_01/Springboot01ApplicationTests.java