我最近一直致力于spring + hibernate + mysql集成。为此,我尝试构建一个添加,编辑,删除和搜索学生的基本程序。我首先创建了模型类并添加了必要的JPA注释:
package com.joseph.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO) // for autonumber
private int studentId;
@Column
private String firstname;
@Column
private String lastname;
@Column
private int yearLevel;
public Student(){}
public Student(int studentId, String firstname, String lastname,
int yearLevel) {
super();
this.studentId = studentId;
this.firstname = firstname;
this.lastname = lastname;
this.yearLevel = yearLevel;
}
/* Getters and Setters */
}
然后我构建了DAO接口:
package com.joseph.dao;
import java.util.List;
import com.joseph.model.Student;
public interface StudentDao {
public void add(Student student);
public void edit(Student student);
public void delete(int studentId);
public Student getStudent(int studentId);
public List getAllStudent();
}
为了从SQL获取必要的数据,我按如下方式实现了dao接口:
package com.joseph.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.joseph.dao.StudentDao;
import com.joseph.model.Student;
@Repository
public class StudentDaoImpl implements StudentDao {
@Autowired
private SessionFactory session;
@Override
public void add(Student student) {
session.getCurrentSession().save(student);
}
@Override
public void edit(Student student) {
session.getCurrentSession().update(student);
}
@Override
public void delete(int studentId) {
session.getCurrentSession().delete(getStudent(studentId));
}
@Override
public Student getStudent(int studentId) {
return (Student)session.getCurrentSession().get(Student.class, studentId);
}
@Override
public List getAllStudent() {
return session.getCurrentSession().createQuery("from Student").list();
}
}
但我在StudentDaoImpl.java
课程中添加(学生)方法时出错:
此行有多个标记 - StudentDaoImpl类型的方法add(Student)必须覆盖或实现超类型方法 - 实现com.joseph.dao.StudentDao.add
此错误与同一类中的所有其他方法类似。 我该如何解决这个问题?
答案 0 :(得分:1)
您应该从界面上的方法中删除公共:
package com.joseph.dao;
import java.util.List;
import com.joseph.model.Student;
public interface StudentDao {
void add(Student student);
void edit(Student student);
void delete(int studentId);
Student getStudent(int studentId);
List getAllStudent();
}
答案 1 :(得分:0)
您的代码在我的测试环境(InteliJ,JDK7 +)下100%正常工作,因此它纯粹与您的IDE连接。
尝试重新启动IDE或重新导入此项目。
我还可以分享一些提示:
super()
,编译器会自动添加它,seconsly你的上层是Object
,所以没有必要line:)以下是通用DAO类的示例:
public class GenericDaoImpl <D>{
protected SessionFactory session;
protected Class clazz;
public GenericDaoImpl(SessionFactory session, Class clazz){
this.session = session;
this.clazz = clazz;
}
@Override
public void add(D entity) {
session.getCurrentSession().save(entity);
}
@Override
public void update(D entity) {
session.getCurrentSession().update(entity);
}
@Override
public void remove(Long id) {
session.getCurrentSession().delete(get(id));
}
@Override
public Student get(Long id) {
return (Student)session.getCurrentSession().get(clazz, id);
}
@Override
public List getAll() {
return session.getCurrentSession().createQuery("from " + clazz.getSimpleName()).list();
}
}
要创建StudentDao,只需执行以下操作:
public interface StudentDao{
void add(Student student);
void update(Student student);
void remove(int id);
Student get(int id);
List getAll();
}
最后你的Dao实施:
public class StudentDaoImpl extends GenericDaoImpl<Student> implements StudentDao{
@Autowired
public StudentDaoImpl(SessionFactory sessionFactory){
super(sessionFactory, Student.class);
}
}
使用这种方法,您可以在所有Dao中重用大部分代码:)因此维护的代码更少,易于使用。