1、导入jar

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">

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="批量删除" onclick="deleteSomePerson();">

  <table width="70%" border="1">

   <tr>

    <td>

     <input type="checkbox" id="topId" onclick="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. Spring 框架介绍 [Spring 优点][Spring 应用领域][体系结构][目录结构][基础 jar 包]

    您的"关注"和"点赞",是信任,是认可,是支持,是动力...... 如意见相佐,可留言. 本人必将竭尽全力试图做到准确和全面,终其一生进行修改补充更新. 目录 ...

  2. SSH框架整合配置所需JAR包(SSH整合)

    转载于:http://www.cnblogs.com/kaige123/p/5719662.html Hibernate Jar: 1.hibernate3.jar,这个是hibernate3.0的核 ...

  3. springmvc 项目完整示例07 设置配置整合springmvc springmvc所需jar包springmvc web.xml文件配置

    前面主要是后台代码,spring以及mybatis的整合 下面主要是springmvc用来处理请求转发,展现层的处理 之前所有做到的,完成了后台,业务层和持久层的开发完成了 接下来就是展现层了 有很多 ...

  4. hibernate中各个jar包的含义和关系

    最基本的Hibernate3.3.2之 JAR包(必要): 包名 位置 用途 hibernate3.jar /hibernate 核心JAR包 antlr.jar /hibernate/lib/req ...

  5. hibernate的各个jar包的作用

    hibernate的各个jar包的作用 最基本的Hibernate3.3.2之 JAR包(必要): 包名 位置 用途 hibernate3.jar /hibernate 核心JAR包 antlr.ja ...

  6. Spring环境搭建之:导入jar包、配置文件名称及放置位置

    Spring环境搭建之:导入jar包.配置文件名称及放置位置 现在项目开发中spring框架应用的还是比较多的,自己用的还不太熟练,每次用的时候总配置半天,总有些配置弄错,就找个时间总结以下,方便以后 ...

  7. Spring Batch远程分区的本地Jar包模式

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Spring相关文章:Springboot-Cloud Spring Batch远程分区对于大量数据的处理非常擅长,它的实现 ...

  8. 使用maven-shade-plugin打包spring项目为可执行的jar包

    使用maven-shade-plugin打包spring项目为可执行的jar包,打包后的jar包里面包含依赖的jar包. POM文件: <plugin> <groupId>or ...

  9. 基于注解Spring MVC综合Hibernate(需要jar包,spring和Hibernate整合配置,springMVC组态,重定向,)批量删除

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

随机推荐

  1. 宏ut_2pow_remainder

    求余数 12%8=4 n%m也能计算出余数,但效率可能比位操作要低一些 /*************************************************************// ...

  2. poj3252

    好了,我的数论渣爆了………… 首先[n,m]内的round number显然就是f[m]-f[n-1] 即问0~x内有多少round number: 设x的二进制位数为t: 首先很好分析出在这个范围 ...

  3. Python开发环境

    --> Python开发环境简备 Python的优点除了其自身的各种特点外,还因为有众多第三方模块,为其扩展了各方面无数的功能. 虽然大多数时候在百度上随便搜都能搜到一大把,pypi (http ...

  4. I.MX6 U-boot Kernel backlight setting

    /********************************************************************* * I.MX6 U-boot Kernel backlig ...

  5. mysql qps tps

    (1)QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Question% ...

  6. GP(Geoprocessing)服务的发布与调用

    转自:http://www.cnblogs.com/gisangela/archive/2011/01/06/1927702.html 1.什么是GP服务 GP服务是Geoprocessing服务的简 ...

  7. 6、四大组件之二-Service初步

    一.什么是Service 有些用时比较长得操作我们希望他在后台运行 ,不耽误我们当前的操作 . 这就引入了Service概念 . 常见的比如:访问网络 , 文件IO操作 , 大数据的数据库任务,播放音 ...

  8. SqlServer将日期格式DateTime转换成varchar类型

    Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE( ...

  9. codeforce 606B Testing Robots

    题意:给定一个x*y的矩形,和一个机器人的初始位置(x0,y0).以向下为x轴正方向,向右为y轴正方向.现在要对这个机器人进行多次测试.每次测 试,会在矩形的某个位置有一个矿井.所以一共要进行x*y次 ...

  10. [学姿势]使用AngularJS+CodeIgniter框架经验谈

    这篇文章作为总结总结7月份参与项目时遇到的新东西和问题,包括这两个框架(CodeIgniter.AngularJS)以及两个功能实现.有前端调试显示动态认证,也有用php实现功能模块. 1.添加设备 ...