开发者社区> 问答> 正文

JAVA Spring Basic CRUD API实现引发错误

我正在使用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

    }

}

实体类如下:

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

    }

}

展开
收起
几许相思几点泪 2019-12-22 18:36:58 932 0
1 条回答
写回答
取消 提交回答
  • 技术架构师 阿里云开发者社区技术专家博主 CSDN签约专栏技术博主 掘金签约技术博主 云安全联盟专家 众多开源代码库Commiter

    hibernate查询方法检查下

    2020-01-04 23:56:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载