67,513
社区成员
发帖
与我相关
我的任务
分享
package com.cn.wuxiong.spring.school.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name = "aastudent", catalog = "itsm")
@NamedQueries({
@NamedQuery(name = "findAllStudents", query = "SELECT s FROM Student s"),
@NamedQuery(name = "findStudentById", query = "SELECT s FROM Student s where s.id=?") })
public class Student {
private Long id;
private String stuNo;
private String name;
private Integer age;
private String sex;
public Student() {
};
public Student(Long id, String stuNo, String name, Integer age, String sex) {
this.id = id;
this.stuNo = stuNo;
this.name = name;
this.age = age;
this.sex = sex;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "stuNo", nullable = false)
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "age", nullable = false)
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "sex", nullable = false)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package com.cn.wuxiong.spring.school.dao;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.cn.wuxiong.spring.school.domain.Student;
public interface StudentDao extends PagingAndSortingRepository<Student, Long>,StudentDaoEx {//
}
package com.cn.wuxiong.spring.school.dao;
import org.springframework.stereotype.Repository;
import com.cn.wuxiong.spring.school.domain.Student;
@Repository
public interface StudentDaoEx {
public int getCount();
public void persist(Student st);
public void testFunc();
}
package com.cn.wuxiong.spring.school.dao;
import org.springframework.stereotype.Repository;
import com.cn.wuxiong.spring.school.domain.Student;
@Repository
public class StudentDaoImpl implements StudentDaoEx {
@Override
public void testFunc(){
System.out.println("#############Test DAO");
}
public void persist(Student st){
System.out.println(st);
}
public int getCount(){
System.out.println("数量为:3");
return 3;
}
}
package com.cn.wuxiong.spring.school.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.cn.wuxiong.spring.school.dao.StudentDao;
@org.springframework.stereotype.Service
@Transactional
public class SchoolService {
@Autowired
private StudentDao dao;
public void testService() {
// dao.testFunc();
System.out.println("###########################Test service");
}
}
package com.cn.wuxiong.spring.school.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.cn.wuxiong.spring.school.service.SchoolService;
@Controller
@RequestMapping(value = "/itsm/school")
public class SchoolCtrl {
@Autowired
private SchoolService schoolService;
@RequestMapping(value="/student.do", method = RequestMethod.GET)
public String pagingList() {
schoolService.testService();
return "school/studentManagement";
}
// 新建
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createPage(Model model) {
model.addAttribute("action_type", "create");
return "school/add_update_student";
}
}
public interface StudentDao extends PagingAndSortingRepository<Student, Long>,StudentDaoEx {//
}
我初步估计是应为StudentDao集成了StudentDaoEx导致的,因为你这里的StudentDao是spring自己实现的,但是我估计他只针对PagingAndSortingRepository里面定义的方法,而一旦你继承了自己StudentDaoEx增加了方法spring无法实现,因此也就直接无法给你实例化出一个StudentDao.
Spring Data我很久前了解过,你去掉后面继承的 StudentDaoEx试下能不能实例化成功<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-lazy-init="true">
<context:annotation-config />
<!-- 扫描组件路径 -->
<context:component-scan base-package="com.cn.wuxiong.spring.*" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<!-- 异常跳转页面 -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Throwable">error/500</prop>
</props>
</property>
</bean>
</beans>