【发布时间】:2019-12-22 09:29:46
【问题描述】:
我正在使用 Spring 在 JAVA 中创建一个基本的 CRUD 应用程序。 我有一个具有以下配置的 mysql 表 数据库名称: “first_crud_api” 表名: tb1_employee 我在 Employee 表中有 5 列,如下所示.......................... ...
我正在尝试使用 api 一次从表中检索所有数据...
这是一个 maven 项目,我正在使用端口 8080 中的 Tomcat 运行我的 Spring 应用程序。 当我点击“http://localhost:8080/api/employee”时,我收到错误说明
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Dec 22 14:32:57 IST 2019
There was an unexpected error (type=Internal Server Error, status=500).
org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Employee is not mapped [from Employee]
请帮助解决问题...我卡住了
应用类:.....
@SpringBootApplication
@Configuration
@EnableWebMvc
@ComponentScan("com.sring.crud.*")
@EnableAutoConfiguration
public static void main(String[] args) {
SpringApplication.run(SpringCrudApplication.class, args);
}
@Bean
public EmployeeDAO employeeDAO(){
return new EmployeeDAOImpl();
}
@Bean
public EmployeeService employeeService(){
return new EmployeeServiceImpl();
}
}
Repository 接口及其实现如下
package com.sring.crud.DAO;
import java.util.List;
import com.sring.crud.model.Employee;
public interface EmployeeDAO {
List<Employee> get();
Employee get(int id);
void save (Employee employee);
void delete (int id);
}
存储库方法的实现:
package com.sring.crud.DAO;
import java.util.List;
import javax.persistence.EntityManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.sring.crud.model.Employee;
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
@Autowired
private EntityManager entitymanager;
@SuppressWarnings("deprecation")
@Override
public List<Employee> get() {
// TODO Auto-generated method stub
Session currentSession = entitymanager.unwrap(Session.class);
Query<Employee> query = currentSession.createQuery("from Employee",Employee.class);
return query.getResultList();
}
@Override
public Employee get(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public void save(Employee employee) {
// TODO Auto-generated method stub
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
}
}
Entity类如下:
package com.sring.crud.model;
import java.sql.Date;
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 = "tb1_employee")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private Integer id;
@Column(name="name")
private String name;
@Column(name="gender")
private String gender;
@Column(name="department")
private String department;
@Column(name="dob")
private Date dob;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", department=" + department + ", dob="
+ dob + "]";
}
}
服务接口及其实现如下............
package com.sring.crud.service;
import java.util.List;
import com.sring.crud.model.Employee;
public interface EmployeeService {
List<Employee> get();
Employee get(int id);
void save (Employee employee);
void delete (int id);
}
实施....
package com.sring.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sring.crud.DAO.EmployeeDAO;
import com.sring.crud.model.Employee;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
EmployeeDAO employeeDAO;
@Transactional
@Override
public List<Employee> get() {
// TODO Auto-generated method stub
return employeeDAO.get();
}
@Transactional
@Override
public Employee get(int id) {
// TODO Auto-generated method stub
return null;
}
@Transactional
@Override
public void save(Employee employee) {
// TODO Auto-generated method stub
}
@Transactional
@Override
public void delete(int id) {
// TODO Auto-generated method stub
}
}
【问题讨论】:
-
SpringCrudApplication类位于哪个包? -
包名是“com.spring.crud.SpringCRUDApplication”
-
由于所有必需的bean都位于应用程序类的包中,因此您无需执行组件扫描。你试过在没有这个注释的情况下运行它吗?
@ComponentScan("com.sring.crud.*")
标签: java spring hibernate spring-boot jpa