Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- com.test__
- |
- |
- |controller__
- | |
- | |
- | |UserController.java
- |
- |domain______
- | |
- | |
- | |User.java
- |
- |
- |repository___
- | |
- | |
- | |UserRepository.java
- |
- |service______
- | |
- | |
- | |UserService.java
- |
- |ServletInitializer.java
- |TestApplication.java
- __________|
- package com.test.controller;
- import com.test.domain.User;
- import com.test.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import java.util.List;
- @CrossOrigin
- @RestController
- @RequestMapping("/user")
- public class UserController {
- @Autowired
- private UserService userService;
- @GetMapping
- public List<User> getAllUsers(){
- return userService.findAll();
- }
- @PostMapping
- public void deleteUser(Long id){
- userService.delete(id);
- }
- @PutMapping
- public User signUpUser(User user){
- return userService.save(user);
- }
- @PostMapping("/find")
- public User findUserById(Long id){
- return userService.findOne(id);
- }
- }
- package com.test.domain;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- @Entity
- public class User {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- private String firstName;
- private String lastName;
- public User() {
- }
- public User(String firstName, String lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long 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;
- }
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", firstName='" + firstName + ''' +
- ", lastName='" + lastName + ''' +
- '}';
- }
- }
- package com.test.repository;
- import com.test.domain.User;
- import org.springframework.data.jpa.repository.JpaRepository;
- public interface UserRepository extends JpaRepository<User,Long> {
- }
- package com.test.service;
- import com.test.domain.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.util.List;
- public interface UserService {
- List<User> findAll();
- User findOne(Long id);
- User save(User user);
- void delete(Long id);
- }
- package com.test.service.impl;
- import com.test.domain.User;
- import com.test.repository.UserRepository;
- import com.test.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.List;
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserRepository userRepository;
- @Override
- public List<User> findAll() {
- return userRepository.findAll();
- }
- @Override
- public User findOne(Long id) {
- return userRepository.findOne(id);
- }
- @Override
- public User save(User user) {
- return userRepository.save(user);
- }
- @Override
- public void delete(Long id) {
- userRepository.delete(id);
- }
- }
- package com.test;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.boot.web.support.SpringBootServletInitializer;
- public class ServletInitializer extends SpringBootServletInitializer {
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
- return application.sources(TestApplication.class);
- }
- }
- package com.test;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class TestApplication {
- public static void main(String[] args) {
- SpringApplication.run(TestApplication.class, args);
- }
- }
- application.properties
- spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
- spring.datasource.username=root
- spring.datasource.password=root
- spring.datasource.hikari.connection-timeout=60000
- spring.datasource.hikari.maximum-pool-size=5
- spring.jpa.hibernate.ddl-auto=update
- spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
- spring.jpa.show-sql=true
- <groupId>com</groupId>
- <artifactId>test</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>test</name>
- <description>Demo project for Spring Boot</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.9.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-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- <scope>provided</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>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
- </head>
- <body>
- <ul id="list">
- </ul>
- <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
- </body>
- <script>
- $.ajax({
- "url": "http://localhost:8080/user",
- 'method': 'GET',
- 'contentType': 'application/json',
- 'dataType': 'json',
- 'headers': {
- 'Content-Type': 'application/json;charset=utf-8'
- },
- 'success': function(data) {
- for(var i = 0;i<data.length;i++){
- $('#list').append("<li>" + "<b>Name:</b>" + data[i].firstName + "<br>" + "<b>Last name:</b> " + data[i].lastName + "</li>");
- }
- },
- 'error': function(error) {
- console.log(error);
- }
- });
- </script>
- </html>
- /____
- |
- |.cashe
- |
- |.softaculous
- |
- |public_html
- |
- |webapps____
- | |
- | |ROOT
- | |
- | |docs
- | |
- | |examples
- | |
- | |host-manager
- | |
- | |manager
- | |
- |___________|
- |
- _____|www
Add Comment
Please, Sign In to add comment