Guest User

Untitled

a guest
Jul 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. *****************************
  2. APPLICATION FAILED TO START
  3. *****************************
  4.  
  5. Description:
  6.  
  7. Field jdbcTemplate in com.kubamadry.dao.MySqlStudentDao required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.
  8.  
  9.  
  10. Action:
  11.  
  12. Consider defining a bean of type 'org.springframework.jdbc.core.JdbcTemplate' in your configuration.
  13.  
  14. Disconnected from the target VM, address: '127.0.0.1:49838', transport: 'socket'
  15.  
  16. Process finished with exit code 1
  17.  
  18. package com.kubamadry;
  19.  
  20. import org.springframework.boot.SpringApplication;
  21. import org.springframework.boot.autoconfigure.SpringBootApplication;
  22.  
  23. @SpringBootApplication
  24. public class Main {
  25. public static void main(String args[]) {
  26. SpringApplication.run(Main.class, args);
  27. }
  28. }
  29.  
  30. package com.kubamadry.entity;
  31.  
  32. public class Student {
  33. private int id;
  34. private String name;
  35. private String course;
  36. }
  37. //Constructor, getters and setters
  38.  
  39. package com.kubamadry.controller;
  40.  
  41. import com.kubamadry.entity.Student;
  42. import com.kubamadry.service.StudentService;
  43. import org.springframework.beans.factory.annotation.Autowired;
  44. import org.springframework.http.MediaType;
  45. import org.springframework.web.bind.annotation.*;
  46.  
  47. import java.util.Collection;
  48.  
  49. @RestController
  50. @RequestMapping("/students")
  51. public class StudentController {
  52.  
  53. @Autowired
  54. private StudentService studentService;
  55.  
  56. @RequestMapping(method = RequestMethod.GET)
  57. public Collection<Student> getAllStudents() {
  58. return studentService.getAllStudents();
  59. }
  60.  
  61. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  62. public Student getStudentById(@PathVariable int id) {
  63. return studentService.getStudentById(id);
  64. }
  65.  
  66. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  67. public void deleteStudentById(@PathVariable int id) {
  68. studentService.deleteStudentById(id);
  69. }
  70.  
  71. @RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  72. public void updateStudent(@RequestBody Student student) {
  73. studentService.updateStudent(student);
  74. }
  75.  
  76. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  77. public void addStudent(@RequestBody Student student) {
  78. studentService.addStudent(student);
  79. }
  80. }
  81.  
  82. package com.kubamadry.dao;
  83.  
  84. import com.kubamadry.entity.Student;
  85. import org.springframework.beans.factory.annotation.Autowired;
  86.  
  87. import org.springframework.jdbc.core.RowMapper;
  88. import org.springframework.stereotype.Repository;
  89. import org.springframework.jdbc.core.JdbcTemplate;
  90.  
  91. import java.sql.ResultSet;
  92. import java.sql.SQLException;
  93. import java.util.Collection;
  94. import java.util.List;
  95.  
  96. @Repository("mysql")
  97. public class MySqlStudentDao implements StudentDao {
  98.  
  99. @Autowired
  100. private JdbcTemplate jdbcTemplate;
  101.  
  102. @Override
  103. public Collection<Student> getAllStudents() {
  104. final String sql = "SELECT * FROM students";
  105. List<Student> students = jdbcTemplate.query(sql, new RowMapper<Student>() {
  106. @Override
  107. public Student mapRow(ResultSet resultSet, int i) throws SQLException {
  108. Student student = new Student();
  109. student.setId(resultSet.getInt("id"));
  110. student.setName(resultSet.getString("name"));
  111. student.setCourse(resultSet.getString("course"));
  112. return student;
  113. }
  114. });
  115. return students;
  116. }
  117.  
  118. @Override
  119. public Student getStudentById(int id) {
  120. return null;
  121. }
  122.  
  123. @Override
  124. public void deleteStudentById(int id) {
  125. //
  126. }
  127.  
  128. @Override
  129. public void updateStudent(Student student) {
  130. //
  131. }
  132.  
  133. @Override
  134. public void addStudent(Student student) {
  135. //
  136. }
  137. }
  138.  
  139. spring.datasource.url=jdbc:mysql://localhost/students
  140. spring.datasource.username=root
  141. spring.datasource.password=password123
  142.  
  143. <?xml version="1.0" encoding="UTF-8"?>
  144. <project xmlns="http://maven.apache.org/POM/4.0.0"
  145. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  146. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  147. <modelVersion>4.0.0</modelVersion>
  148.  
  149. <groupId>com.kubamadry</groupId>
  150. <artifactId>workingProject</artifactId>
  151. <version>1.0-SNAPSHOT</version>
  152.  
  153. <parent>
  154. <groupId>org.springframework.boot</groupId>
  155. <artifactId>spring-boot-starter-parent</artifactId>
  156. <version>2.0.3.RELEASE</version>
  157. </parent>
  158.  
  159. <dependencies>
  160. <dependency>
  161. <groupId>org.springframework.boot</groupId>
  162. <artifactId>spring-boot-starter-web</artifactId>
  163. </dependency>
  164.  
  165. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  166. <dependency>
  167. <groupId>mysql</groupId>
  168. <artifactId>mysql-connector-java</artifactId>
  169. <version>6.0.5</version>
  170. </dependency>
  171.  
  172. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  173. <dependency>
  174. <groupId>org.springframework</groupId>
  175. <artifactId>spring-jdbc</artifactId>
  176. <version>4.3.4.RELEASE</version>
  177. </dependency>
  178.  
  179. </dependencies>
  180.  
  181. <properties>
  182. <java.version>1.8</java.version>
  183. </properties>
  184.  
  185.  
  186. <build>
  187. <plugins>
  188. <plugin>
  189. <groupId>org.springframework.boot</groupId>
  190. <artifactId>spring-boot-maven-plugin</artifactId>
  191. </plugin>
  192. </plugins>
  193. </build>
  194.  
  195. </project>
Add Comment
Please, Sign In to add comment