我正在使用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
}
}
hibernate查询方法检查下
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。