spring mvc与hibernate整合完成增删改查
2013-07-13 21:05
369 查看
CREATE TABLE `teams` ( `id` int(6) NOT NULL AUTO_INCREMENT, `name` varchar(40) NOT NULL, `rating` int(6) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
application.properties
#DB properties: db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/hibnatedb db.username=root db.password= #Hibernate Configuration: hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect hibernate.show_sql=true entitymanager.packages.to.scan=com.sprhib.model
package com.sprhib.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.sprhib.model.Team; import com.sprhib.service.TeamService; @Controller @RequestMapping(value="/team") public class TeamController { @Autowired private TeamService teamService; @RequestMapping(value="/add", method=RequestMethod.GET) public ModelAndView addTeamPage() { ModelAndView modelAndView = new ModelAndView("add-team-form"); modelAndView.addObject("team", new Team()); return modelAndView; } @RequestMapping(value="/add", method=RequestMethod.POST) public ModelAndView addingTeam(@ModelAttribute Team team) { ModelAndView modelAndView = new ModelAndView("home"); teamService.addTeam(team); String message = "Team was successfully added."; modelAndView.addObject("message", message); return modelAndView; } @RequestMapping(value="/list") public ModelAndView listOfTeams() { ModelAndView modelAndView = new ModelAndView("list-of-teams"); List<Team> teams = teamService.getTeams(); modelAndView.addObject("teams", teams); return modelAndView; } @RequestMapping(value="/edit/{id}", method=RequestMethod.GET) public ModelAndView editTeamPage(@PathVariable Integer id) { ModelAndView modelAndView = new ModelAndView("edit-team-form"); Team team = teamService.getTeam(id); modelAndView.addObject("team",team); return modelAndView; } @RequestMapping(value="/edit/{id}", method=RequestMethod.POST) public ModelAndView edditingTeam(@ModelAttribute Team team, @PathVariable Integer id) { ModelAndView modelAndView = new ModelAndView("home"); teamService.updateTeam(team); String message = "Team was successfully edited."; modelAndView.addObject("message", message); return modelAndView; } @RequestMapping(value="/delete/{id}", method=RequestMethod.GET) public ModelAndView deleteTeam(@PathVariable Integer id) { ModelAndView modelAndView = new ModelAndView("home"); teamService.deleteTeam(id); String message = "Team was successfully deleted."; modelAndView.addObject("message", message); return modelAndView; } }list-of-teams.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <?xml version="1.0" encoding="ISO-8859-1" ?> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>List of teams</title> </head> <body> <h1>List of teams</h1> <p>Here you can see the list of the teams, edit them, remove or update.</p> <table border="1px" cellpadding="0" cellspacing="0" > <thead> <tr> <th width="10%">id</th><th width="15%">name</th><th width="10%">rating</th><th width="10%">actions</th> </tr> </thead> <tbody> <c:forEach var="team" items="${teams}"> <tr> <td>${team.id}</td> <td>${team.name}</td> <td>${team.rating}</td> <td> <a href="${pageContext.request.contextPath}/team/edit/${team.id}.html">Edit</a><br/> <a href="${pageContext.request.contextPath}/team/delete/${team.id}.html">Delete</a><br/> </td> </tr> </c:forEach> </tbody> </table> <p><a href="${pageContext.request.contextPath}/index.html">Home page</a></p> </body> </html>其实根本没有任何.html文件,都被spring映射成其它的了。
package com.sprhib.dao; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.sprhib.model.Team; @Repository public class TeamDAOImpl implements TeamDAO { @Autowired private SessionFactory sessionFactory; private Session getCurrentSession() { return sessionFactory.getCurrentSession(); } public void addTeam(Team team) { getCurrentSession().save(team); } public void updateTeam(Team team) { Team teamToUpdate = getTeam(team.getId()); teamToUpdate.setName(team.getName()); teamToUpdate.setRating(team.getRating()); getCurrentSession().update(teamToUpdate); } public Team getTeam(int id) { Team team = (Team) getCurrentSession().get(Team.class, id); return team; } public void deleteTeam(int id) { Team team = getTeam(id); if (team != null) getCurrentSession().delete(team); } @SuppressWarnings("unchecked") public List<Team> getTeams() { return getCurrentSession().createQuery("from Team").list(); } }
原文:http://fruzenshtein.com/spring-mvc-hibernate-maven-crud/
源代码:http://pan.baidu.com/share/link?shareid=1833084276&uk=3878681452
相关文章推荐
- spring入门(12)-spring与hibernate整合完成增删改查的操作(继承HibernateDaoSupport调用hibernateTemplate类)
- spring入门(11)-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)
- spring入门(12)-spring与hibernate整合完成增删改查的操作(继承HibernateDaoSupport调用hibernateTemplate类)
- spring入门-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)
- spring入门之spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)
- spring入门(11)-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)
- 整合Spring MVC,mybatis,hibernate,freemarker框架实现的自定义注解Validator验证机制实现对敏感词过滤的代码分享
- 继完成hibernate配置后的增删改查例子
- 整合Spring MVC,mybatis,hibernate,freemarker框架实现的自定义注解Validator验证机制实现对敏感词过滤的代码分享
- spring mvc高级篇(五):Spring+SpringMVC+Hibernate整合(采用泛型优化)
- 4000 Hibernate--与Spring整合及增删改查
- Spring mvc整合mybatis基于mysql数据库实现用户增删改查及其分页显示的完整入门实例【转】
- Spring mvc 源码 和 Spring+springmvc+hibernate整合源码
- Maven整合Spring,Spring mvc4.1.6,Hibernate4.3.10项目搭建总结
- java框架整合例子(spring、spring mvc、spring data jpa、hibernate)
- spring mvc高级篇(六):Spring+SpringMVC+Hibernate整合(采用泛型+注解优化)
- Spring MVC系列讲座二:与Hibernate的整合开发配置!
- 解决spring mvc +hibernate整合时使用jackson返回json数据无限循环问题的官方解决方案
- spring mvc 与JPA/Hibernate的整合示例
- spring spring mvc hibernate整合(注解)(xml)