当前位置: 代码迷 >> Java Web开发 >> 急小弟我做了一个增删改查spring mvc+hibernate 看看哪里写错了
  详细解决方案

急小弟我做了一个增删改查spring mvc+hibernate 看看哪里写错了

热度:551   发布时间:2016-04-16 22:06:22.0
急啊!我做了一个增删改查spring mvc+hibernate 看看哪里写错了
spring用的注解,hibernate用的xml,是一个简单的学校管理系统



【1】SchoolAction.java

package com.xc.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.xc.dao.SchoolDao;
import com.xc.pojo.SchoolInfo;
import com.xc.service.SchoolService;


@Controller
public class SchoolAction {
@Resource(name = "schoolService")
private SchoolService schoolService;

/**
 * 根据id跳转到学校介绍详细信息 
 * @return
 */
@RequestMapping("/schoolIntroduce")
public String findSchoolIntroduce(HttpServletRequest request){
String id = request.getParameter("id");
SchoolInfo schoolinfo=schoolService.findSchoolBySchoolId(Integer.parseInt(id));
request.setAttribute("list", schoolinfo);
return "school_introduce";
}
/**
 * 根据省份查找相应的学校 
 * @return
 */
@RequestMapping("/toAllStudent")
@ResponseBody
public String findAllStudent(HttpServletRequest request){
String province = request.getParameter("province");
List<SchoolInfo> listSchool=schoolService.findSchoolBySchoolProvince(province);
request.setAttribute("schoollist", listSchool);
return "school_introduce";
}
/**
 * 添加学校信息  
 * @return
 */
@RequestMapping("/toAddSchool")
public String addStudent(SchoolInfo schoolinfo, HttpServletRequest request)
throws Exception {
schoolService.insertSchool(schoolinfo);
    return "addSchool";
}

/**
 * 删除学校信息  
 * @return
 */
@RequestMapping("/toDelSchool")
public String delSchool(Integer id,HttpServletResponse reponse) {
schoolService.deleteSchool(id);
return "deleteSuccess";
}
/**
 * 修改学校
 * 
 * @param school
 * @return
 */
@RequestMapping(value = "updateSchool", method = { RequestMethod.POST })
public String updateSchool(@ModelAttribute("school") SchoolInfo school) {
SchoolInfo schoolinfo = schoolService.findSchoolBySchoolMc(school.getSchoolMc());
school.setIsValid(schoolinfo.getIsValid());
schoolService.updateSchool(schoolinfo);
return "updateSuccess";
}

}
【2】SchoolDao.java

package com.xc.dao;

import java.util.List;

import javax.annotation.PostConstruct;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.xc.pojo.SchoolInfo;


@Repository("schoolDao")
public class SchoolDao extends HibernateDaoSupport{

@Autowired
@Qualifier("sessionFactory")
protected SessionFactory sessionFactory;

@PostConstruct
protected void injectSessionFactory() {
super.setSessionFactory(sessionFactory);

}

/**
 * 根据学校Id查询学校详细信息  
 * @param id
 * @return
 */
public SchoolInfo findSchoolBySchoolId(Integer id){
String hql="from SchoolInfo si where si.id = '"+id+"'";
return (SchoolInfo) getHibernateTemplate().find(hql).get(0);
}

/**
 * 根据学校所在省份查询所拥有的学校信息  
 * @param province
 * @return
 */
@SuppressWarnings("unchecked")
public List<SchoolInfo> findSchoolBySchoolProvince(String province){
String hql="from SchoolInfo si where si.province= '"+province+"'";
return getHibernateTemplate().find(hql);
}
/**
 * 根据学校名查看学校信息  
 * @param 
 * @return
 */
@SuppressWarnings("unchecked")
public SchoolInfo findSchoolBySchoolMc(String schoolMc){
String hql="from SchoolInfo si where si.schoolMc= '"+schoolMc+"'";
return (SchoolInfo) getHibernateTemplate().find(hql);
}

/**
 * 查询所有学校信息  
 * @param 
 * @return
 */
@SuppressWarnings("unchecked")
public List<SchoolInfo> findAllSchool(){
String hql = "from SchoolInfo ";
List list = getHibernateTemplate().find(hql);
  相关解决方案