这篇文章用来总结一下spring,springmvc,spring+mybatis,spring+hibernate的配置文件

1.web.xml

要使用spring,必须在web.xml中定义分发器等信息,基本的配置信息如下:

<?xml version="1.0" encoding= "UTF-8"?>
<web-app version= "3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" > <!-- 配置spring分发器,spring表示对应的 servlet【名可以改】配置文件为spring-servlet.xml -->
<servlet >
<servlet-name >spring </servlet-name >
<servlet-class >org.springframework.web.servlet.DispatcherServlet </servlet-class >
</servlet >
<servlet-mapping >
<!-- 会拦截.do请求-->
<servlet-name >spring </servlet-name >
<url-pattern >*.do </url-pattern >
</servlet-mapping >
< display-name></display-name >
< welcome-file-list>
<welcome-file >index.jsp </welcome-file >
</ welcome-file-list>
</web-app>

2.spring配置文件

<!-- 该配置文件为spring的基本配置文件, springmvc,aop ,transaction等的配置均在此基础上进行 -->
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:context= "http://www.springframework.org/schema/context"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
" > <!-- 对com包下进行扫描,以完成对bean的创建和自动依赖注入 -->
<context:component-scan base-package ="com"/> </beans>

3.springmvc配置文件

<!-- 该配置文件为 springmvc的基本配置文件 -->
<!-- 相比较spring,增加了 mvc的命名空间与注解驱动 -->
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:context= "http://www.springframework.org/schema/context"
xmlns:mvc= "http://www.springframework.org/schema/mvc"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
" > <!-- 对com包下进行扫描,以完成对bean的创建和自动依赖注入 -->
<context:component-scan base-package ="com"/>
<!-- mvc层提供的注解驱动[注册用于解析@ResponseBody注解的类]
当controller中的方法需要返回 json数据时,需要用到@ResponseBody注解,此时需呀添加此驱动 -->
<mvc:annotation-driven /> </beans>

4.springmvc整合hibernate

以下为springmvc+hibernate的配置文件,去掉mvc命名空间等配置即为spring+hibernate的配置文件

<!-- 该配置文件为 springmvc+hibernate 的基本配置文件 -->
<!-- 相比较springmvc,增加了hibernate 的配置 -->
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:context= "http://www.springframework.org/schema/context"
xmlns:mvc= "http://www.springframework.org/schema/mvc"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
" > <!-- 对com包下进行扫描,以完成对bean的创建和自动依赖注入 -->
<context:component-scan base-package ="com"/> <!-- mvc层提供的注解驱动[注册用于解析@ResponseBody注解的类]
当controller中的方法需要返回 json数据时,需要用到@ResponseBody注解,此时需呀添加此驱动 -->
<mvc:annotation-driven /> <!-- 配置hibernate 开始 -->
<bean id ="ht" class= "org.springframework.orm.hibernate3.HibernateTemplate" >
<!-- 指向session工厂 -->
<property name ="SessionFactory" ref= "sf"></property >
</bean >
<!-- 配置session工厂
a setAnnotatedClasses(Class[] claes)
指向映射实体bean列表
每在工程中添加一个映射实体bean,就需要在list元素下添加一个value子元素指向该实体bean
b setPackagesToScan(String package)
扫描实体bean所在的包结构,在包下查找所有的映射实体
-->
<bean name ="sf" class= "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name ="dataSource" ref="ds"></ property>
<!-- 映射实体bean 配置bean所在的包-->
<property name ="packagesToScan" value= "com.po,com.ngsh.bean"></property ><!-- 如果有多个包有映射实体,都在value中写,用逗号隔开 -->
<property name ="hibernateProperties">
<props >
<prop key= "hibernate.show_sql">true</prop >
</props >
</property >
</bean >
<!-- hibernate的数据源 -->
<bean id ="ds" class= "org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name ="driverClassName" value= "com.mysql.jdbc.Driver"></property >
<property name ="url" value= "jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8" ></property >
<property name ="username" value="root"></ property>
<property name ="password" value="root"></ property>
</bean > </beans>

5.springmvc整合mybatis配置文件

去掉mvc的相关配置即为spring+mybatis的配置文件

<!-- 该配置文件为 springmvc+mybatis 的基本配置文件 -->
<!-- 相比较springmvc,增加了mybatis 的配置 -->
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:context= "http://www.springframework.org/schema/context"
xmlns:mvc= "http://www.springframework.org/schema/mvc"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
" > <!-- 对com包下进行扫描,以完成对bean的创建和自动依赖注入 -->
<context:component-scan base-package ="com"/> <!-- mvc层提供的注解驱动[注册用于解析@ResponseBody注解的类]
当controller中的方法需要返回 json数据时,需要用到@ResponseBody注解,此时需呀添加此驱动 -->
<mvc:annotation-driven /> <!-- 配置mybatis 开始 -->
<!-- 在ioc容器中配置sqlSessionFactory -->
<bean id ="ssf" class= "org.mybatis.spring.SqlSessionFactoryBean" >
<!-- 配置数据源 指向 ds -->
<property name ="dataSource" ref="ds"></property>
<!-- 配置映射文件 当有多个时 在list中添加-->
<property name ="mapperLocations">
<list >
<!-- classpath +映射文件的路径 -->
<value> classpath:com.dao.UserDao-mapper.xml</value >
</list >
</property >
</bean >
<!-- mybatis的数据源 -->
<bean id ="ds" class= "org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name ="driverClassName" value= "com.mysql.jdbc.Driver"></property >
<property name ="url" value= "jdbc:mysql://localhost:3306/demo" ></property >
<property name ="username" value="root"></property>
<property name ="password" value="root"></property>
</bean >
<!-- 配置mapper.xml所映射的接口,-->
<!-- 方法一 每增加一个接口类就得新增一个对应的bean进行注册 -->
<!-- <bean id ="userDao" class= "org.mybatis.spring.mapper.MapperFactoryBean" >
指向sessionFactory
<property name ="sqlSessionFactory" ref= "ssf"></property >
<property name ="mapperInterface" value= "com.dao.UserDaoIf"></property >
</bean > -->
<!-- 方法二 直接扫描dao包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao" />
<property name="sqlSessionFactoryBeanName" value="ssf"></property>
</bean>
</beans>

6.mybatis的mapper文件的模板

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 声明命名空间即其所映射的接口 -->
<mapper namespace= "com.dao.UserDaoIf">
<!-- parameterType指定参数类型,多个参数使用map resultMap指定结果集 -->
<select id ="selectById" parameterType="java.util.map"
resultMap= "user">
select * from user where name=#{name} and pw=#{pw };
</select >
<!-- resultType表示返回int 型 -->
<select id ="selectUserCount" resultType= "java.lang.Integer">
select count(*) from user;
</select >
<!-- 修改 -->
<update id ="uppw" parameterType="java.util.Map" >
update user set pw=#{pw} where id=#{id};
</update >
<delete id ="removeById" parameterType="java.lang.Integer">
delete from user where id=#{id};
</delete > <!-- 定义返回的结果集 使用select查询时可以使用resultType[返回类型如java.lang.String],也可以使用resultMap,
但两者不可以同时使用,可定义多个,通过id区分
-->
<resultMap type ="com.bean.User" id="user">
<result property ="id" column="id"/>
<result property ="name" column="name"/>
<result property ="pw" column="pw"/>
</resultMap >
</mapper>

版权声明:本文为博主原创文章,未经博主允许不得转载。

spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件的更多相关文章

  1. SpringMVC, Spring和Mybatis整合案例一

    一  准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二  整合思路 Dao层: 1.SqlMapConfig. ...

  2. SpringMVC Spring MyBatis整合配置文件

    1.spring管理SqlSessionFactory.mapper 1)在classpath下创建mybatis/sqlMapConfig.xml <?xml version="1. ...

  3. SSH(Struts,Spring,Hibernate)和SSM(SpringMVC,Spring,MyBatis)的区别

    SSH 通常指的是 Struts2 做前端控制器,Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做前端控制器,Spring 管理各层的组件,M ...

  4. 整合最优雅SSM框架:SpringMVC + Spring + MyBatis

    我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...

  5. 最优雅SSM框架:SpringMVC + Spring + MyBatis

    在写代码之前我们先了解一下这三个框架分别是干什么的? 相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码! SpringMVC:它用于web层,相当于con ...

  6. SpringMVC+Spring+Mybatis框架集成

    一.基本概念 1.Spring      Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-O ...

  7. 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis

    在写代码之前我们先了解一下这三个框架分别是干什么的? 相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码! SpringMVC:它用于web层,相当于con ...

  8. SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现

    上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1.    外部架包依赖引入 外部依赖包引入 ...

  9. springMVC+spring+mybatis搭建最近

    一:概述SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛. Spring作为一个轻量级的框架,有很多的拓展功能,最主要的我们一般项目使用的就是IOC和AOP. Sp ...

随机推荐

  1. Linxu命令与文件的搜索 - which, whereis, locate, find

    which (寻找『运行档』) [root@www ~]# which [-a] command 选项或参数: -a :将所有由 PATH 目录中可以找到的命令均列出,而不止第一个被找到的命令名称 范 ...

  2. hadoop的节点间的通信

    一个DataNode上的Block是唯一的,多个DataNode可能有相同的Block. 2)通信场景: (1)NameNode的映射表上不永久保存每个DataNode所对应的block信息,而是通过 ...

  3. Linux下进程通信方式(简要概述)

    http://blog.sina.com.cn/s/blog_65c209580100u0ee.html (1)管道(Pipe):管道可用于具有亲缘关系进程间的通信,允许一个进程和另一个与它有共同祖先 ...

  4. C语言笔试经典-查找多位数重复数字以及次数

    从键盘输入一个多位的整数 用程序判断 这个数里面有没有 重复的数字  有重复的数字就打印  哪个数字重复了  重复了几次 例如:输入:1122431 打印结果: 1重复 出现3次 2重复 出现2次, ...

  5. 最大的k个数问题

    代码来源: http://blog.csdn.net/v_JULY_v 调整堆为小顶堆的代码片:基本思想就是把孩子节点中大的一个跟父节点交换 void HeapAdjust(int array[], ...

  6. ruby rails_autolink不能加载的原因

    从rails 3.1.0开始,默认在ActionView::Helper::TextHelper中的auto_link方法已经被移除,放到了第三方的gem里:rails_autolink.遂想试一下其 ...

  7. SQL语言逻辑执行顺序

    SQL语言逻辑执行顺序 2012-12-18 16:18:13 分类: 数据库开发技术 查询的逻辑执行顺序 FROM < left_table> ON < join_conditio ...

  8. jvm栈-运行控制,jvm-堆运行存储共享单元

     JVM-栈 2012-09-17 15:43:53 分类: Java 原文转自:http://www.blogjava.net/nkjava/archive/2012/03/15/371971.ht ...

  9. ScrollView的顶部下拉和底部上拉回弹效果

    要实现ScrollView的回弹效果,需要对其进行触摸事件处理.先来看一下简单的效果: 根据Android的View事件分发处理机制,下面对dispatchTouchEvent进行详细分析: 在加载布 ...

  10. Python用pip安装IPython/Jupyter最佳交互环境

    一.Python模块及安装包简介 如果说编程语言是武器,那么Python就是一把双管枪(Python2/Python3),而各种为Python编写的模块和包就是子弹.使用pip来填满我们的武器吧! I ...