Guest User

Untitled

a guest
Jan 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. com.test__
  2. |
  3. |
  4. |controller__
  5. | |
  6. | |
  7. | |UserController.java
  8. |
  9. |domain______
  10. | |
  11. | |
  12. | |User.java
  13. |
  14. |
  15. |repository___
  16. | |
  17. | |
  18. | |UserRepository.java
  19. |
  20. |service______
  21. | |
  22. | |
  23. | |UserService.java
  24. |
  25. |ServletInitializer.java
  26. |TestApplication.java
  27. __________|
  28.  
  29. package com.test.controller;
  30.  
  31. import com.test.domain.User;
  32. import com.test.service.UserService;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.web.bind.annotation.*;
  35.  
  36. import java.util.List;
  37.  
  38. @CrossOrigin
  39. @RestController
  40. @RequestMapping("/user")
  41. public class UserController {
  42.  
  43. @Autowired
  44. private UserService userService;
  45.  
  46. @GetMapping
  47. public List<User> getAllUsers(){
  48. return userService.findAll();
  49. }
  50.  
  51. @PostMapping
  52. public void deleteUser(Long id){
  53. userService.delete(id);
  54. }
  55.  
  56. @PutMapping
  57. public User signUpUser(User user){
  58. return userService.save(user);
  59. }
  60. @PostMapping("/find")
  61. public User findUserById(Long id){
  62. return userService.findOne(id);
  63. }
  64.  
  65. }
  66.  
  67. package com.test.domain;
  68.  
  69. import javax.persistence.Entity;
  70. import javax.persistence.GeneratedValue;
  71. import javax.persistence.GenerationType;
  72. import javax.persistence.Id;
  73.  
  74. @Entity
  75. public class User {
  76.  
  77. @Id
  78. @GeneratedValue(strategy = GenerationType.IDENTITY)
  79. private Long id;
  80.  
  81. private String firstName;
  82.  
  83. private String lastName;
  84.  
  85. public User() {
  86. }
  87.  
  88. public User(String firstName, String lastName) {
  89. this.firstName = firstName;
  90. this.lastName = lastName;
  91. }
  92.  
  93. public Long getId() {
  94. return id;
  95. }
  96.  
  97. public void setId(Long id) {
  98. this.id = id;
  99. }
  100.  
  101. public String getFirstName() {
  102. return firstName;
  103. }
  104.  
  105. public void setFirstName(String firstName) {
  106. this.firstName = firstName;
  107. }
  108.  
  109. public String getLastName() {
  110. return lastName;
  111. }
  112.  
  113. public void setLastName(String lastName) {
  114. this.lastName = lastName;
  115. }
  116.  
  117. @Override
  118. public String toString() {
  119. return "User{" +
  120. "id=" + id +
  121. ", firstName='" + firstName + ''' +
  122. ", lastName='" + lastName + ''' +
  123. '}';
  124. }
  125. }
  126.  
  127. package com.test.repository;
  128.  
  129. import com.test.domain.User;
  130. import org.springframework.data.jpa.repository.JpaRepository;
  131.  
  132. public interface UserRepository extends JpaRepository<User,Long> {
  133. }
  134.  
  135. package com.test.service;
  136.  
  137. import com.test.domain.User;
  138. import org.springframework.beans.factory.annotation.Autowired;
  139.  
  140. import java.util.List;
  141.  
  142. public interface UserService {
  143.  
  144. List<User> findAll();
  145.  
  146. User findOne(Long id);
  147.  
  148. User save(User user);
  149.  
  150. void delete(Long id);
  151.  
  152.  
  153. }
  154.  
  155. package com.test.service.impl;
  156.  
  157. import com.test.domain.User;
  158. import com.test.repository.UserRepository;
  159. import com.test.service.UserService;
  160. import org.springframework.beans.factory.annotation.Autowired;
  161. import org.springframework.stereotype.Service;
  162.  
  163. import java.util.List;
  164.  
  165. @Service
  166. public class UserServiceImpl implements UserService {
  167.  
  168. @Autowired
  169. private UserRepository userRepository;
  170.  
  171. @Override
  172. public List<User> findAll() {
  173. return userRepository.findAll();
  174. }
  175.  
  176. @Override
  177. public User findOne(Long id) {
  178. return userRepository.findOne(id);
  179. }
  180.  
  181. @Override
  182. public User save(User user) {
  183. return userRepository.save(user);
  184. }
  185.  
  186. @Override
  187. public void delete(Long id) {
  188. userRepository.delete(id);
  189. }
  190. }
  191.  
  192. package com.test;
  193.  
  194. import org.springframework.boot.builder.SpringApplicationBuilder;
  195. import org.springframework.boot.web.support.SpringBootServletInitializer;
  196.  
  197. public class ServletInitializer extends SpringBootServletInitializer {
  198.  
  199. @Override
  200. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  201. return application.sources(TestApplication.class);
  202. }
  203.  
  204. }
  205.  
  206. package com.test;
  207.  
  208. import org.springframework.boot.SpringApplication;
  209. import org.springframework.boot.autoconfigure.SpringBootApplication;
  210.  
  211. @SpringBootApplication
  212. public class TestApplication {
  213.  
  214. public static void main(String[] args) {
  215. SpringApplication.run(TestApplication.class, args);
  216. }
  217. }
  218.  
  219. application.properties
  220.  
  221. spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
  222. spring.datasource.username=root
  223. spring.datasource.password=root
  224. spring.datasource.hikari.connection-timeout=60000
  225. spring.datasource.hikari.maximum-pool-size=5
  226. spring.jpa.hibernate.ddl-auto=update
  227. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
  228. spring.jpa.show-sql=true
  229.  
  230. <groupId>com</groupId>
  231. <artifactId>test</artifactId>
  232. <version>0.0.1-SNAPSHOT</version>
  233. <packaging>war</packaging>
  234.  
  235. <name>test</name>
  236. <description>Demo project for Spring Boot</description>
  237.  
  238. <parent>
  239. <groupId>org.springframework.boot</groupId>
  240. <artifactId>spring-boot-starter-parent</artifactId>
  241. <version>1.5.9.RELEASE</version>
  242. <relativePath/> <!-- lookup parent from repository -->
  243. </parent>
  244.  
  245. <properties>
  246. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  247. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  248. <java.version>1.8</java.version>
  249. </properties>
  250.  
  251. <dependencies>
  252. <dependency>
  253. <groupId>org.springframework.boot</groupId>
  254. <artifactId>spring-boot-starter-data-jpa</artifactId>
  255. </dependency>
  256. <dependency>
  257. <groupId>org.springframework.boot</groupId>
  258. <artifactId>spring-boot-starter-jdbc</artifactId>
  259. </dependency>
  260. <dependency>
  261. <groupId>org.springframework.boot</groupId>
  262. <artifactId>spring-boot-starter-web</artifactId>
  263. </dependency>
  264.  
  265. <dependency>
  266. <groupId>mysql</groupId>
  267. <artifactId>mysql-connector-java</artifactId>
  268. <scope>runtime</scope>
  269. </dependency>
  270. <dependency>
  271. <groupId>org.springframework.boot</groupId>
  272. <artifactId>spring-boot-starter-tomcat</artifactId>
  273. <scope>provided</scope>
  274. </dependency>
  275. <dependency>
  276. <groupId>org.springframework.boot</groupId>
  277. <artifactId>spring-boot-starter-test</artifactId>
  278. <scope>test</scope>
  279. </dependency>
  280. </dependencies>
  281.  
  282. <build>
  283. <plugins>
  284. <plugin>
  285. <groupId>org.springframework.boot</groupId>
  286. <artifactId>spring-boot-maven-plugin</artifactId>
  287. </plugin>
  288. </plugins>
  289. </build>
  290.  
  291.  
  292. </project>
  293.  
  294. <html>
  295.  
  296. <head>
  297. <meta charset="UTF-8">
  298. <title>Document</title>
  299. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  300. </head>
  301.  
  302. <body>
  303.  
  304. <ul id="list">
  305.  
  306.  
  307. </ul>
  308.  
  309. <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  310. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  311. </body>
  312.  
  313. <script>
  314. $.ajax({
  315. "url": "http://localhost:8080/user",
  316. 'method': 'GET',
  317. 'contentType': 'application/json',
  318. 'dataType': 'json',
  319. 'headers': {
  320. 'Content-Type': 'application/json;charset=utf-8'
  321. },
  322. 'success': function(data) {
  323. for(var i = 0;i<data.length;i++){
  324. $('#list').append("<li>" + "<b>Name:</b>" + data[i].firstName + "<br>" + "<b>Last name:</b> " + data[i].lastName + "</li>");
  325. }
  326. },
  327. 'error': function(error) {
  328. console.log(error);
  329. }
  330. });
  331.  
  332. </script>
  333.  
  334. </html>
  335.  
  336. /____
  337. |
  338. |.cashe
  339. |
  340. |.softaculous
  341. |
  342. |public_html
  343. |
  344. |webapps____
  345. | |
  346. | |ROOT
  347. | |
  348. | |docs
  349. | |
  350. | |examples
  351. | |
  352. | |host-manager
  353. | |
  354. | |manager
  355. | |
  356. |___________|
  357. |
  358. _____|www
Add Comment
Please, Sign In to add comment