Spring+SpringMVC+MyBatis整合配置

前端控制器 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    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_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 前端控制器 -->
  <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring.xml</param-value>
      </init-param>

      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- 中文乱码过滤器 -->
  <filter>
      <filter-name>encodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

spring核心配置文件

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" >

    <!-- 加载属性配置文件 -->
    <util:properties id="db"
        location="classpath:db.properties"/>

    <!-- 定义数据源 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="#{db.driver}"/>
        <property name="url" value="#{db.url}"/>
        <property name="username" value="#{db.user}"/>
        <property name="password" value="#{db.pwd}"/>
    </bean>    

    <!-- 定义SQLSessionFactoryBean组件 MyBatis连接数据库1-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <!-- 没有了MyBatis的主配置文件SqlMapConfig.xml -->
        <!-- 需要指定连接资源 -->
        <property name="dataSource" ref="ds"></property>
        <!-- 需要指定映射文件 -->
        <property name="mapperLocations" value="classpath:com/xms/entity/mapper/*.xml"></property>
    </bean>

    <!-- 定义MapperScannerrConfigurer扫描组件MyBatis扫描dao包2 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <!-- 指定Mapper接口扫描包 -->
        <property name="basePackage" value="com.xms.dao" ></property>
        <!-- 手动指定SqlSessionFactory对象 -->  <!-- sqlSessionFactory属性可以不用指定,它会以Autowired方式自动注入 -->
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" ></property>

        <!-- 推荐使用注解方法 -->
      <property name="annotationClass" value="com.xms.common.MyAnnontation" ></property> 

    <!-- 接口方法 -->
    <!--    <property name="markerInterface" value="com.xms.common.Myinterface" />   -->
    </bean>

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.xms"/>

    <!-- 开启RequestMapping注解扫描 -->
    <mvc:annotation-driven/>

    <!-- 定义视图解析器ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 全局异常处理 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.Exception">error</prop>
            </props>
        </property>
    </bean>

</beans>

原文地址:https://www.cnblogs.com/yingyigongzi/p/9295324.html

时间: 2024-08-12 04:16:32

Spring+SpringMVC+MyBatis整合配置的相关文章

idea spring+springmvc+mybatis环境配置整合详解

idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2.查看idea中是否安装Maven插件: 2.1:File --> Settings --> Plugins 2.2:如下图所示的步骤进行操作(注:安装完插件,idea会重新启动) 3.idea创建Maven项目的步骤 4.搭建目录结构 下图就是我搭建Maven项目之后,添加对应的目录和文件 5.p

Spring+SpringMVC +MyBatis整合配置文件案例66666

Spring+SpringMVC +MyBatis整合配置文件案例 标签: springspringmvcmybatismvcjava 2017-04-13 19:12 228人阅读 评论(1) 收藏 举报 分类: java_javaSE(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Spring+SpringMVC +MyBatis整合配置文件案例 针对spring/SpringMVC/MyBatis三个框架的整合有很多的方式,经过最近的学习我来总结一下其配置文

ssm之spring+springmvc+mybatis整合初探

1.基本目录如下  2.首先是向lib中加入相应的jar包  3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/

Spring+SpringMVC+Mybatis环境配置

文件目录展示 resouce目录展示 src目录展示 WebRoot目录展示 jdbc.propertise driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/hrm_db3?characterEncoding=UTF-8 username=root password=root #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 mi

spring和mybatis整合配置

<?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:p="http://www.springframework.org/schema/p&

spring+springMVC+mybatis整合并使用redis做缓存

1.用idea 构建一个maven 的web项目 点击下一步 继续下一步 选着maven home directory 自己的maven 安装目录,全选下一步,等一分钟就OK了 2.配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h

Spring+SpringMvc+Mybatis整合注意事项

web.xml代码如下 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/n

2016.5.19 SSM 框架(一) Spring SpringMVC Mybatis 整合思路

前言 : 内容 依然是根据 传智播客 燕青老师 视频 及自己理解所写 1. 总体框架图 2.理解 可能从后往前更好理解一点 底层: 数据库    mysql  sqlserver oracle 等等 持久层(DAO):                   ---调用数据库   提供mapper接口 持久层 生成 Mapper接口(DAO接口), 提供访问数据库的方法和数据. 业务层(Service):                --- 调用 mapper 接口     提供Service接口

Spring+SpringMVC+MyBatis整合应用

1)搭建Spring,SpringMVC和MyBatis环境 创建一个web工程 添加MyBatis相关环境 引入数据库驱动包和DBCP连接池开发包 引入MyBatis开发包 添加Spring,SpringMVC相关技术环境 引入Spring ioc,jdbc-tx,aop开发包 引入Spring web,webmvc开发包 在src下添加spring.xml配置文件 在web.xml中配置DispatcherServlet前端控制器和中文乱码 处理过滤器CharacterEncodingFil