1、导入jar

2、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 <!-- 通过上下文参数指定spring配置文件的位置 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:beans.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:springMVC.xml</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
3、bean.xml配置

<?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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
 <!-- 注解驱动 -->
 <mvc:annotation-driven/>
 <!-- 组件扫描 -->
 <context:component-scan base-package="cn.itcast.springmvc"></context:component-scan>
 
 <!-- 定义数据源 -->
 <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springmvc"></property>
  <property name="user" value="root"></property>
  <property name="password" value="123456"></property>
  <property name="initialPoolSize" value="10"></property>
  <property name="maxPoolSize" value="50"></property>
  <property name="minPoolSize" value="10"></property>
 </bean>
 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="ds"></property>
  
  <!-- hibernate映射文件的位置 -->
  <property name="mappingDirectoryLocations">
   <value>classpath:cn/itcast/springmvc/domain/</value>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.Dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
  </property>
 </bean>
 
 <!-- 配置事物管理器 -->
 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
 
 <!-- 配置事物的传播特性 (事物通知)-->
 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:method name="save*" propagation="REQUIRED"/>
   <tx:method name="delete*" propagation="REQUIRED"/>
   <tx:method name="update*" propagation="REQUIRED"/>
   <tx:method name="find*" read-only="true"/>
   <tx:method name="*" read-only="true"/>
  </tx:attributes>
 </tx:advice>
 
 <aop:config>
  <aop:advisor pointcut="execution(* *..*ServiceImpl.*(..))" advice-ref="txAdvice"/>
  <!--
   <aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*ServiceImpl.*(..))"/>
   -->
 </aop:config>
</beans>

4、SpringMVC.xml配置

<?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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
 
 <!-- 配置内部资源视图解析器 -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>   
</beans>

5、domain和hbm.xml配置

package cn.itcast.springmvc.domain;

public class Person {

private String id;
 private String name;
 private Integer age;
 private String address;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
}

----------------------------

Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="cn.itcast.springmvc.domain.Person" table="persons">
  <id name="id" column="id" type="string">
   <generator class="uuid" />
  </id>
  <property name="name" column="name" type="string" />
  <property name="age" column="age" type="integer" />
  <property name="address" column="address" type="string" />
 </class>
</hibernate-mapping>
6、PersonDao

package cn.itcast.springmvc.dao;

import java.util.List;

import cn.itcast.springmvc.domain.Person;

public interface IPersonDao {
 public Person findPersonById(String id);
 public List<Person> findAllPerson();
 public void savePerson(Person p);
 public void deletePersonById(String id);
 public void updatePerson(Person p);
}

7、PersonDaoImpl

package cn.itcast.springmvc.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import cn.itcast.springmvc.domain.Person;

@Repository(value="personDao")
public class PersonDaoImpl implements IPersonDao {
 
 @Resource(name="sessionFactory")
 private SessionFactory sf;

public Person findPersonById(String id) {
  return (Person) sf.getCurrentSession().createQuery("from Person where id = '" + id + "'").list().get(0);
 }

public void savePerson(Person p) {
  sf.getCurrentSession().save(p);
 }

public void deletePersonById(String id) {
  Person p = new Person();
  p.setId(id);
  //sf.getCurrentSession().delete(id, Person.class);
  sf.getCurrentSession().delete(p);
 }

public List<Person> findAllPerson() {
  return sf.getCurrentSession().createQuery("from Person").list();
 }

public void updatePerson(Person p) {
  sf.getCurrentSession().update(p);
 }

}

8、IPersonService

package cn.itcast.springmvc.service;

import java.util.List;

import cn.itcast.springmvc.domain.Person;

public interface IPersonService {
 public Person findPersonById(String id);
 public List<Person> findAllPerson();
 public void savePerson(Person p);
 public void deletePersonById(String id);
 public void updatePerson(Person p);
}

9、PersonServiceImpl

package cn.itcast.springmvc.service;

import java.util.List;

import javax.annotation.Resource;

import cn.itcast.springmvc.dao.IPersonDao;
import cn.itcast.springmvc.domain.Person;

@org.springframework.stereotype.Service(value="personService")
public class PersonServiceImpl implements IPersonService {
 
 @Resource(name="personDao")
 private IPersonDao personDao;

public Person findPersonById(String id) {
  return personDao.findPersonById(id);
 }

public void savePerson(Person p) {
  personDao.savePerson(p);
 }

public void deletePersonById(String id) {
  personDao.deletePersonById(id);
 }

public List<Person> findAllPerson() {
  return personDao.findAllPerson();
 }

public void updatePerson(Person p) {
  personDao.updatePerson(p);
 }

}

10、PersonController

package cn.itcast.springmvc.controller;

import java.util.List;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.springmvc.domain.Person;
import cn.itcast.springmvc.service.IPersonService;

@Controller
@RequestMapping(value = "/person")
public class PersonController {

@Resource(name = "personService")
 private IPersonService personService;

@RequestMapping(value = "/findPersonById")
 public String findPersonById(String id) {
  Person p = personService.findPersonById(id);
  System.out.println(p);
  return "showPerson";
 }

@RequestMapping(value = "/savePersonUI")
 public String savePersonUI() {
  return "savePerson";
 }

@RequestMapping(value = "/savePerson")
 public String savePerson(Person p) {
  personService.savePerson(p);
  System.out.println(p);
  // 重定向
  return "redirect:/person/findAllPerson";
 }

@RequestMapping(value = "/deletePersonById")
 public String deletePersonById(String id) {
  personService.deletePersonById(id);

// 重定向
  return "redirect:/person/findAllPerson";
 }

// 批量删除
 @RequestMapping(value = "/deletePersonByIds")
 public String deletePersonByIds(String ids) {
  ids = ids.substring(0, ids.length() - 1);
  String[] idss = ids.split(",");
  for (String id : idss) {
   personService.deletePersonById(id);
  }
  // 重定向
  return "redirect:/person/findAllPerson";
 }

@RequestMapping(value = "/findAllPerson")
 public String findAllPerson(HttpServletRequest req) {
  List<Person> persons = personService.findAllPerson();
  req.setAttribute("persons", persons);
  return "personList";
 }

@RequestMapping(value = "/updatePersonUI")
 public String updatePersonUI(HttpServletRequest req, String id) {
  Person p = personService.findPersonById(id);
  req.setAttribute("p", p);
  return "updatePerson";
 }

@RequestMapping(value = "/updatePerson")
 public String updatePerson(Person p) {
  personService.updatePerson(p);
  // 重定向
  return "redirect:/person/findAllPerson";
 }
}

11、personList.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>
<html>
 <head>
  <title>personList.jsp</title>
  <script type="text/javascript">
   function selectOrUnslect(){
    var ids = document.getElementsByName('ids');
    if(document.getElementById('topId').checked == true){
     for(var i=0;i<ids.length;i++){
      ids[i].checked = true;
     }
    }else{
     for(var i=0;i<ids.length;i++){
      ids[i].checked = false;
     }
    }
   }
   
   function deleteSomePerson(){
    var ids = document.getElementsByName('ids');
    var strIds = '';
    for(var i=0;i<ids.length;i++){
     if(ids[i].checked == true){
      strIds += ids[i].value + ',';
     }
    }
    window.location = '<%=path%>/person/deletePersonByIds?ids=' + strIds;
   }
  </script>
 </head>
 <body>
  <h3>
   用户列表页面
  </h3>
  <a href="<%=path %>/person/savePersonUI">添加用户</a><br>
  
  <input type="button" value="批量删除" οnclick="deleteSomePerson();">
  <table width="70%" border="1">
   <tr>
    <td>
     <input type="checkbox" id="topId" οnclick="selectOrUnslect();">
    </td>
    <td>
     name
    </td>
    <td>
     age
    </td>
    <td>
     address
    </td>
    <td>
     删除
    </td>
    <td>
     更新
    </td>
   </tr>
   <c:forEach items="${persons}" var="p">
    <tr>
     <td>
      <input type="checkbox" name="ids" value="${p.id }">
     </td>
     <td>
      ${p.name }
     </td>
     <td>
      ${p.age }
     </td>
     <td>
      ${p.address }
     </td>
     <td>
      <a href="<%=path %>/person/deletePersonById?id=${p.id }">删除</a>
     </td>
     <td>
      <a href="<%=path %>/person/updatePersonUI?id=${p.id }">更新</a>
     </td>
    </tr>
   </c:forEach>
  </table>
 </body>
</html>
12、savePerson.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <title>savePerson.jsp</title>
  </head>
  <body>
  <h3>添加用户页面</h3>
    <form action="<%=path %>/person/savePerson" method="post">
     <table>
      <tr>
       <td>name:</td>
       <td><input type="text" name="name"></td>
      </tr>
      <tr>
       <td>age</td>
       <td><input type="text" name="age"></td>
      </tr>
      <tr>
       <td>address</td>
       <td><input type="text" name="address"></td>
      </tr>
      <tr>
       <td><input type="submit" value="提交"></td>
       <td><input type="reset" value="重置"></td>
      </tr>
     </table>
    </form>
  </body>
</html>
13、showPerson.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'showPerson.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

</head>
 
  <body>
    This is showPerson.jsp
  </body>
</html>

14、updatePerson.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
    <title>savePerson.jsp</title>
  </head>
  <body>
    <h3>更新用户页面</h3>
    <form action="<%=path %>/person/updatePerson" method="post">
     <input type="hidden" name="id" value="${p.id}">
     <table>
      <tr>
       <td>name:</td>
       <td><input type="text" name="name" value="${p.name }"></td>
      </tr>
      <tr>
       <td>age</td>
       <td><input type="text" name="age" value="${p.age }"></td>
      </tr>
      <tr>
       <td>address</td>
       <td><input type="text" name="address" value="${p.address }"></td>
      </tr>
      <tr>
       <td><input type="submit" value="提交"></td>
       <td><input type="reset" value="重置"></td>
      </tr>
     </table>
    </form>
  </body>
</html>

基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)相关推荐

  1. ajax注解解决中文乱码,基于注解的简单MVC框架的实现,以及jquery,prototype,ajax传输乱码问题的一点解决方法...

    1:基于注解的简单MVC框架的实现 效果:1:用户只需要定义一些普通的java类来做为M层,也就是STRUTS的action类,该类里包含1到 N个控制方法,每个方法需要的form数据,由注解@Act ...

  2. Spring+Hibernate+Struts2整合所需要的Jar包

    struts2.1.6 支持jar包 xwork-2.1.2.jar struts2-core-2.1.6.jar commons-logging-1.0.4.jar freemarker-2.3.1 ...

  3. [Spring mvc 深度解析(三)] 创建Spring MVC之器

    第9章 创建Spring MVC之器 ​ 本章将分析Spring MVC自身的创建过程.首先分析Spring MVC的整体结构,然后具体分析每一层的创建过程. 1 整体结构介绍 Spring MVC中 ...

  4. Spring MVC 使用问题与解决--HTTP Status 500 - Servlet.init() for servlet springmvc threw exception

    Spring MVC 使用问题与解决--HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 参考文章: (1) ...

  5. Struts2、Hibernate、Spring整合所需要的jar包

    Struts2.Hibernate.Spring整合所需要的包: Struts2: struts2-core-2.0.14.jar  -- Struts2的核心包. commons-logging-1 ...

  6. springboot templates读取不到_整合spring mvc + mybatis,其实很简单,spring boot实践(5)

    01 spring boot读取配置信息 02 多环境配置 03 处理全局异常 04 spring boot admin 主要通过spring boot整合spring mvc 以及mybatis实现 ...

  7. 基于XML配置的Spring MVC(所需jar包,web.xml配置,处理器配置,视图解析器配置)

    1.添加jar 2.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app ver ...

  8. spring mvc学习(4):第一个spring mvc项目

    一个Spring MVC的项目如何创建?请看这里. 代码编辑器:Intellij IDEA 请提前在电脑上配置好自己的tomcat! 该文属于小白教程,适合初学者. 1 创建Spring MVC项目 ...

  9. MAC OS X El CAPITAN 搭建SPRING MVC (1)- 目录、包名、创建web.xml

    一. 下载STS(Spring Tool Suite) 官方地址:http://spring.io/tools/sts 下载spring tool suite for mac 最新版本.这个IDE是很 ...

最新文章

  1. linux cp 强制覆盖_Linux基本操作教程
  2. python需要电脑配置-python3批量统计用户电脑配置
  3. 【多线程】Synchronized及实现原理
  4. .NET 基金会项目介绍 - ReactiveUI
  5. 第一章、第一节 Angular基础
  6. 中国水溶性PVA薄膜行业市场供需与战略研究报告
  7. Oracle 写存储过程的一个模板还有一些基本的知识点
  8. RDD和DataFrame和DataSet三者间的区别
  9. 抖音极速版—–青龙面板
  10. Linux系统各发行版镜像下载(借阅)
  11. 小米开发版安装magisk_小米9SE不刷recovery直接安装Magisk面具的详细教程
  12. App主界面交互框架(其中包括标签式、跳板式、列表式、旋转木马、抽屉式、点聚式、陈列馆式、瀑布流)
  13. 【Android Jetpack】彻底弄清Navigation的BackStack如何变化
  14. 关注虚拟财富“.ME”域名的投资价值
  15. artDialog对话框组件使用方法
  16. 11.3 作业 Problem L: 数字统计
  17. 【已解决】win10离线安装.net framework 3.5(错误:0x8024402c)
  18. mysql小知识:根据指定日期,获取是当年第几周
  19. 可解释知识追踪(整理更新)
  20. NG-ALAIN 边学边记1

热门文章

  1. JavaScript实现squareRoot平方根算法(附完整源码)
  2. OpenCASCADE:OCCT应用框架OCAF之形状属性
  3. boost::test::string_token_iterator相关的测试程序
  4. boost::geometry模块多边形DP算法简化示例
  5. boost::hash_combine模块实现json哈希值的测试程序
  6. Boost:GPU上的2D图像中绘制最终的随机“walk”,并使用OpenCV进行显示
  7. Boost:bind绑定查找问题的测试程序
  8. ITK:并排平铺图像
  9. VTK:图片之ImageGaussianSmooth
  10. VTK:几何对象之Tetrahedron