spring/springmvc/spring+hibernate(mybatis)配置文件

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>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

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>


 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值