热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Spring+SpringMVC+Hibernate整合(XML方式)

Web.xml

Web.xml


<web-app xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns&#61;"http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation&#61;"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version&#61;"3.1"><display-name>Threedisplay-name><listener><listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>listener><context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:config/applicationContext.xmlparam-value> context-param><servlet> <servlet-name>dispatcherServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:config/springmvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>dispatcherServletservlet-name> <url-pattern>/url-pattern> servlet-mapping> <filter> <filter-name>CharacterEncodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> filter> <filter-mapping> <filter-name>CharacterEncodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> <filter> <filter-name>SessionFilterfilter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilterfilter-class> filter> <filter-mapping> <filter-name>SessionFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping>
web-app>

注&#xff1a;
/&#xff1a; 会匹配到 /springmvc 这样的路径型url&#xff0c;而不会匹配到像 .jsp 这样的后缀型的url。
/*&#xff1a;会匹配到所有的url&#xff1a;路径型url 和后缀型的url &#xff08;包括/springmvc,.jsp,.js,和.html等&#xff09;。

applicationContext.xml


<beans xmlns&#61;"http://www.springframework.org/schema/beans" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns:context&#61;"http://www.springframework.org/schema/context" xmlns:tx&#61;"http://www.springframework.org/schema/tx" xmlns:aop&#61;"http://www.springframework.org/schema/aop"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id&#61;"dataSource" class&#61;"com.mchange.v2.c3p0.ComboPooledDataSource" > <property name&#61;"user" value&#61;"root">property> <property name&#61;"password" value&#61;"123456">property> <property name&#61;"driverClass" value&#61;"com.mysql.jdbc.Driver">property> <property name&#61;"jdbcUrl" value&#61;"jdbc:mysql://localhost:3306/book">property> bean> <bean id&#61;"sessionFactory" class&#61;"org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name&#61;"dataSource" ref&#61;"dataSource">property> <property name&#61;"packagesToScan"> <list> <value>entityvalue> list> property> <property name&#61;"hibernateProperties"> <props> <prop key&#61;"hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect prop> <prop key&#61;"hibernate.hbm2ddl.auto">updateprop> <prop key&#61;"hibernate.temp.use_jdbc_metadata_defaults">falseprop> props> property> bean> <bean id&#61;"transactionManager" class&#61;"org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name&#61;"sessionFactory" ref&#61;"sessionFactory">property> bean> <tx:annotation-driven transaction-manager&#61;"transactionManager" />
beans>

**注&#xff1a;**需要c3p0的jar包。c3p0-0.9.2.1.jar、hibernate-c3p0-5.0.7.Final.jar、mchange-commons-java-0.2.3.4.jar。数据库驱动包就更不用说了。没有的话可以去Maven仓库下。

springmvc.xml


<beans xmlns&#61;"http://www.springframework.org/schema/beans" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns:context&#61;"http://www.springframework.org/schema/context" xmlns:mvc&#61;"http://www.springframework.org/schema/mvc" xmlns:tx&#61;"http://www.springframework.org/schema/tx" xsi:schemaLocation&#61;"http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package&#61;"test" /> <bean class&#61;"org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name&#61;"prefix" value&#61;"/WEB-INF/views/">property> <property name&#61;"suffix" value&#61;".jsp">property> bean> <mvc:default-servlet-handler/> <mvc:annotation-driven/> beans>

配置文件目录&#xff1a;
这里写图片描述

注&#xff1a;
在Spring的配置里&#xff0c;最好不要配置xsd文件的版本号。Spring默认在启动时是要加载XSD文件来验证xml文件的&#xff0c;所以如果有的时候断网了&#xff0c;或者网络不好&#xff0c;就会出现问题。为了防止这种情况&#xff0c;Spring提供了一种机制&#xff0c;默认从本地加载XSD文件。所以当我们没有配置xsd文件的版本号的时候&#xff0c;用的就是当前版本的XSD文件。

至此基本配置都配好了。我们来测试一下&#xff1a;
testDao.java

package test.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import entity.Admin;
&#64;Repository
public class testDao {&#64;Autowiredprivate SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } private Session getSession() {return sessionFactory.getCurrentSession();}public Admin getAdmin(int id) {Admin admin &#61; (Admin)this.getSession().get(Admin.class, id);return admin;}
}

testService.java

package test.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import entity.Admin;
import test.dao.testDao;
&#64;Service
&#64;Transactional
public class testService {&#64;Autowired
private testDao testDao;
public Admin getAdmin(int id) {return testDao.getAdmin(id);
}
}

testController.java

package test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import entity.Admin;
import test.service.testService;
&#64;Controller
public class testController {
&#64;Autowired
private testService testService;
&#64;RequestMapping("/test")
public String test()
{int id &#61; 2;Admin admin &#61; testService.getAdmin(id);System.out.println(admin.getAdminname());return "success";
}
}

success.jsp

<%&#64; page language&#61;"java" contentType&#61;"text/html; charset&#61;UTF-8"
pageEncoding&#61;"UTF-8"%>







查询成功

跳转的成功页面
这里写图片描述
查询出的Adminname
这里写图片描述
最终项目目录结构
这里写图片描述
Spring推荐使用接口编程解耦&#xff0c;不过我这里没有用。因为感觉要创建好多个类啊。。偷懒喽


推荐阅读
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • SpringMVC工作流程概述
    SpringMVC工作流程概述 ... [详细]
  • Struts2+Sring+Hibernate简单配置
    2019独角兽企业重金招聘Python工程师标准Struts2SpringHibernate搭建全解!Struts2SpringHibernate是J2EE的最 ... [详细]
  • 开发笔记:(002)spring容器中bean初始化销毁时执行的方法及其3种实现方式
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了(002)spring容器中bean初始化销毁时执行的方法及其3种实现方式相关的知识,希望对你有一定的参考价值。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • Asp.net Mvc Framework 七 (Filter及其执行顺序) 的应用示例
    本文介绍了在Asp.net Mvc中应用Filter功能进行登录判断、用户权限控制、输出缓存、防盗链、防蜘蛛、本地化设置等操作的示例,并解释了Filter的执行顺序。通过示例代码,详细说明了如何使用Filter来实现这些功能。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 花瓣|目标值_Compose 动画边学边做夏日彩虹
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Compose动画边学边做-夏日彩虹相关的知识,希望对你有一定的参考价值。引言Comp ... [详细]
author-avatar
用户da7lbtmrer
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有