我是Java和Spring的新手,我正在为Sql Server中的其中一个表执行简单的CRUD项目。但是它不会在get查询中返回任何结果。 以下是代码文件:
以下是pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.luv2code.springboot</groupId>
<artifactId>cruddemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cruddemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</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>
以下是CruddemoApplication.java文件,它是启动文件:
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);
}
}
package com.luv2code.springboot.cruddemo.dao;
import java.util.List;
import com.luv2code.springboot.cruddemo.entity.StudentDetails;
public interface StudentDetailDAO {
public List<StudentDetails> findAll();
}
package com.luv2code.springboot.cruddemo.dao;
import java.sql.Connection;
import java.util.List;
import javax.persistence.EntityManager;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.luv2code.springboot.cruddemo.entity.StudentDetails;
@Repository
public class StudentDetailsDAOHibernateImpl implements StudentDetailDAO {
private EntityManager entityManager;
@Autowired
public StudentDetailsDAOHibernateImpl(EntityManager theEntityManager)
{
this.entityManager = theEntityManager;
}
@Override
@Transactional
public List<StudentDetails> findAll() {
List<StudentDetails> students = null;
try
{
Session currentSession = entityManager.unwrap(Session.class);
Query<StudentDetails> theQuery = currentSession.createQuery("from StudentDetails",StudentDetails.class);
students = theQuery.getResultList();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return students;
}
}
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 = "StudentDetails")
public class StudentDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="StudentID")
private Integer StudentID;
@Column(name="Name")
private String Name;
@Column(name="Surname")
private String Surname;
@Column(name="City")
private String City;
public StudentDetails() {}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSurname() {
return Surname;
}
public void setSurname(String surname) {
Surname = surname;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public StudentDetails(String name, String surname, String city) {
Name = name;
Surname = surname;
City = city;
}
@Override
public String toString() {
return "StudentDetails [Name=" + Name + ", Surname=" + Surname + ", City=" + City + "]";
}
}
package com.luv2code.springboot.cruddemo.rest;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.luv2code.springboot.cruddemo.dao.StudentDetailDAO;
import com.luv2code.springboot.cruddemo.entity.StudentDetails;
@RestController
@RequestMapping("api")
public class StudentDetailsController {
private StudentDetailDAO studentDetailDAO;
public StudentDetailsController(StudentDetailDAO theStudentDetailDAO)
{
this.studentDetailDAO = theStudentDetailDAO;
}
@GetMapping(value ="/students")
public List<StudentDetails> findAll() {
return studentDetailDAO.findAll();
}
}
下面是我要在其中添加MS Sql服务器详细信息的application.properties。
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=India
spring.datasource.username=sa
spring.datasource.password=Temp1234
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.hibernate.ddl-auto = update
答案 0 :(得分:0)
在private StudentDetailDAO studentDetailDAO;
类的StudentDetailsController
中添加自动装配注释,因此将具有:
@Autowired
private StudentDetailDAO studentDetailDAO;