首先用自動生成的那個頁面,index.jsp:
html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <h2>Hello World!</h2> <a href="save.m">保存</a> <a href="update.m">更新</a> <a href="delete.m">刪除</a> <a href="get.m">查詢</a> <h1>操做結果:</h1> <h2>${result}</h2> </body> </html>
編寫一個簡單的實體類,
java
SimpleBean:git
package simples.bean; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class SimpleBean { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column private Long id; @Column private String name; @Column private String remark; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "SimpleBean [id=" + id + ", name=" + name + ", remark=" + remark + "]"; } }
再編寫一個Controller,github
SimpleController:web
package simples.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import simples.bean.SimpleBean; import simples.service.SimpleService; @Controller public class SimpleController { @Autowired private SimpleService simpleService; @RequestMapping("/save") public String save(ModelMap modelMap){ System.out.println("SimpleController.save()"); SimpleBean bean = new SimpleBean(); bean.setName("bean111"); bean.setRemark("bean1"); simpleService.save(bean); modelMap.addAttribute("result", "保存成功"); return "index"; } @RequestMapping("/update") public String update(ModelMap modelMap){ System.out.println("SimpleController.update()"); SimpleBean bean = simpleService.get(1L); bean.setName("simple1"); bean.setRemark("simple2"); simpleService.update(bean); modelMap.addAttribute("result", "更新成功"); return "index"; } @RequestMapping("/delete") public String delete(ModelMap modelMap){ System.out.println("SimpleController.delete()"); simpleService.delete(1L); modelMap.addAttribute("result", "刪除成功"); return "index"; } @RequestMapping("/get") public String get(ModelMap modelMap){ System.out.println("SimpleController.get()"); SimpleBean bean = simpleService.get(1L); modelMap.addAttribute("result", bean); return "index"; } }
service的接口,
spring
SimpleService:session
package simples.service; import simples.bean.SimpleBean; public interface SimpleService { void save(SimpleBean bean); void update(SimpleBean bean); void delete(Long id); SimpleBean get(Long id); }
service的實現類,
app
SimpleServiceImpl:框架
package simples.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import simples.bean.SimpleBean; import simples.dao.SimpleDao; import simples.service.SimpleService; @Service public class SimpleServiceImpl implements SimpleService { @Autowired private SimpleDao simpleDao; public void save(SimpleBean bean) { simpleDao.save(bean); } public void update(SimpleBean bean) { simpleDao.update(bean); } public void delete(Long id) { simpleDao.delete(id); } public SimpleBean get(Long id) { return simpleDao.get(id); } }
Dao的接口,ssh
SimpleDao:
package simples.dao; import simples.bean.SimpleBean; public interface SimpleDao { void save(SimpleBean bean); void update(SimpleBean bean); void delete(Long id); SimpleBean get(Long id); }
Dao的實現類,
SimpleDaoImpl:
package simples.dao.impl; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import simples.bean.SimpleBean; import simples.dao.SimpleDao; @Repository public class SimpleDaoImpl implements SimpleDao { @Autowired private SessionFactory sessionFactory; private Session getSession() { return sessionFactory.getCurrentSession(); } public void save(SimpleBean bean) { getSession().save(bean); } public void update(SimpleBean bean) { getSession().update(bean); } public void delete(Long id) { Session session = getSession(); Object bean = session.get(SimpleBean.class, id); session.delete(bean); } public SimpleBean get(Long id) { return (SimpleBean) getSession().get(SimpleBean.class, id); } }
下面演示最基本的功能,打開index.jsp頁面後應該是這個頁面:
點擊保存後:
再點擊查詢:
到這裏,SpringMVC+Spring3+Hibernate4框架的搭建就完成了,剩下2個功能能夠本身測試。
最後附上項目地址: