Spring、springMVC、Mybatis整合

本文介绍了一个整合Spring、SpringMVC和Mybatis的Java Web项目配置过程,包括配置文件详解、工程结构说明及可能遇到的问题解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring、SpringMVC、Mybatis整合

工程结构

工程结构

Spring

配置文件:applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 扫描包下面所有的类,如果检测到带注解@Component、@Service、@Controller等注解的类,那么将其注册为bean-->
    <context:component-scan base-package="com.foradawn"/>

    <!-- 整合mybatis -->
    <!-- 配置数据源,这样就可以不用在mybatis的配置文件里配置 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/student_manage_system?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC"/>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    <!-- 配置工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 加载Mybatis全局配置文件 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    </bean>
    <!-- 配置mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描包路径,如果需要扫描多个包中间用半角逗号隔开 -->
        <property name="basePackage" value="com.foradawn.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
</beans>

SpringMVC

配置文件:spring_mvc.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"
       xsi:schemaLocation="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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 包扫描 -->
    <context:component-scan base-package="com.foradawn"></context:component-scan>

    <!-- 应用默认配置方案
    <mvc:annotation-driven /> 会自动注册RequestMappingHandlerMapping、RequestMappingHandlerAdapter 与ExceptionHandlerExceptionResolver 三个bean。
    还将提供以下支持:
        支持使用 ConversionService 实例对表单参数进行类型转换;
        支持使用 @NumberFormat annotation、@DateTimeFormat;
        注解完成数据类型的格式化;
        支持使用 @Valid 注解对 JavaBean 实例进行 JSR 303 验证;
        支持使用 @RequestBody 和 @ResponseBody 注解;
    mvc:annotation-driven使用:
        当使用mvc:view-controller标签时一定要加入mvc:annotation-driven,不然会使requestMapping失效。
        当为了处理静态资源问题而加入mvc:default-servlet-handler时,也一定要加入mvc:annotation-driven,不然requestMapping同样会失效。
        当使用自定义类型转换器的时候需要加上mvc:annotation-driven标签。
    -->
    <!-- <mvc:annotation-driven/>标签相当于如下配置-->
    <!-- Begin -->
    <!-- HandlerMapping -->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "></bean>-->
    <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>-->
    <!-- HandlerAdapter -->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
    <!--<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter "></bean>-->
    <!--<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>-->
    <!-- HadnlerExceptionResolvers -->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver "></bean>-->
    <!--<bean class="org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver"></bean>-->
    <!--<bean class="org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver"></bean>-->
    <!-- End -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value="test/html;charset=utf-8"></property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 在springMVC-servlet.xml中配置<mvc:default-servlet-handler />后,会在Spring MVC上下文中定义一个
    org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它会像一个检查员,对进入
    DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,
    如果不是静态资源的请求,才由DispatcherServlet继续处理。 -->
    <mvc:default-servlet-handler/>

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
    </bean>
</beans>

Mybatis

配置文件:sqlMapConfig.xml、*mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--<environments default="development">-->
    <!-- 由于数据源已经在applicationContext配置了,所以这里就不用了 -->
        <!--<environment id="development">-->
            <!--<transactionManager type="JDBC"/>-->
            <!--<dataSource type="POOLED">-->
                <!--<property name="url" value="jdbc:mysql://localhost:3306/student_manage_system"/>-->
                <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->
                <!--<property name="username" value="root"/>-->
                <!--<property name="password" value=""/>-->
            <!--</dataSource>-->
        <!--</environment>-->
    <!--</environments>-->
    <mappers>
        <!--<mapper resource="com/foradawn/dao/ClassDao.xml"/>-->
        <!--<mapper resource="com/foradawn/dao/MajorDao.xml"/>-->
        <!--<mapper resource="com/foradawn/dao/ManagerDao.xml"/>-->
        <!--<mapper resource="com/foradawn/dao/StudentDao.xml"/>-->
        <!--<mapper resource="com/foradawn/dao/TeacherDao.xml"/>-->
        <!-- 使用包扫描,接口类和xml文件名称需要保持一致 -->
        <package name="com.foradawn.dao"/>
    </mappers>
</configuration>

Web

配置文件:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>filter</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring_mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>filter</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

mapper的配置

比如ClassDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace一定要与ClassDao所在路径匹配,resultType使用全限定名,接口定义的方法与这里的id要相同 -->
<mapper namespace="com.foradawn.dao.ClassDao">
    <select id="selectClassById" resultType="com.foradawn.pojo.Class">
        select * from Class where cid = #{cid}
    </select>
</mapper>

有可能报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

我是在pom中加上这段代码就行了

<build>
       <resources>
           <resource>
               <directory>src/main/java</directory>
               <includes>
                   <include>**/*.xml</include>
               </includes>
           </resource>
           <resource>
               <directory>src/main/resources</directory>
           </resource>
       </resources>
    </build>
### 回答1: SpringSpring MVC和MyBatis是三个非常流行的Java开发框架。它们可以被整合在一起,形成一个强大的Web应用程序开发框架。整合这三个框架可以使开发人员更加高效地开发Web应用程序,并且可以提高应用程序的性能和可维护性。整合的过程需要配置Spring的上下文、Spring MVC的控制器和MyBatis的数据访问层。整合后,可以使用Spring的依赖注入和AOP功能来管理对象和事务,使用Spring MVC的控制器来处理请求和响应,使用MyBatis的Mapper来访问数据库。 ### 回答2: SpringSpringMVCMyBatis是目前企业级Java开发中常用的三种框架,它们分别有着不同的作用和功能,但是它们可以结合起来使用,相互补充,成为一个完整的应用程序。 首先,Spring框架是一个轻量级的、开源的Java EE框架,以IoC(控制反转)和AOP(面向切面编程)为核心,提供了诸如事务管理、MVC框架、 JDBC框架等服务,是Java企业级应用程序的修建工和纽带。 其次,SpringMVC框架是Spring框架的一部分,是一个基于MVC架构模式的Web框架,它可以处理传入的HTTP请求,将请求映射到相应的处理方法,最终产生HTTP响应,并将响应返回给前端。 最后,MyBatis是一种ORM(对象关系映射)框架,它可以将Java对象映射到数据库中的表,通过提供简单的抽象语言,避免了很多传统的JDBC编程必须处理的样板代码,使得开发人员可以更加关注业务逻辑的实现。 当SpringSpringMVCMyBatis结合起来使用时,可以更好地开发Java Web应用程序。其中,Spring提供了IoC容器,帮助开发人员进行依赖注入,解决各个组件之间的依赖关系,降低系统的耦合度;SpringMVC提供了一个灵活的MVC框架,可以根据不同的需求进行自定义配置,帮助开发人员快速构建Web应用程序;MyBatis可以与Spring MVC集成,提供数据访问层的ORM映射,通过配置文件来实现简便的数据操作。三者相互配合,可以大幅提升开发效率,提高程序运行效率,降低开发的难度和成本。 综上所述,SpringSpringMVCMyBatis整合是Java企业级应用程序中常用的开发手段,既提高了开发的效率,又提高了程序的性能,为企业的IT系统提供了可靠和高效的支持。 ### 回答3: SpringSpring MVC和MyBatis都是非常优秀的Java开发框架,它们都有着各自的优点和特点。Spring是一个轻量级的IoC容器,可以管理Bean的生命周期,并提供了一些其他的功能,例如AOP、事务管理等。Spring MVC是一个基于Servlet的MVC框架,可以进行RESTful风格的Web应用程序开发。MyBatis是一个优秀的ORM框架,可以非常方便地操作数据库。 在实际开发中,SpringSpring MVC和MyBatis通常是一起使用的。可以通过整合这三个框架来提高开发效率,并使代码的复杂度大大降低。整合的过程也非常简单,只需要将它们的配置文件与代码进行交互即可。 具体的整合过程如下: 首先,需要引入SpringSpring MVC和MyBatis的相关依赖包。可以使用Maven或Gradle等依赖管理工具来方便地下载和管理这些依赖。 其次,需要在Spring配置文件中定义相关的Bean,并将它们注入到Spring容器中。MyBatis需要定义SqlSessionFactory和MapperScannerConfigurer这两个Bean,分别用于创建SqlSession和自动扫描Mapper接口。而Spring MVC则需要定义HandlerMapping和HandlerAdapter这两个Bean,用于映射请求和处理请求。 最后,需要在Spring MVC配置文件中配置视图解析器和静态资源访问等相关信息,并添加MyBatis的Mapper映射文件和Mapper接口扫描路径等信息。 经过整合之后,就可以方便地在Spring MVC中使用MyBatis进行数据库操作了。当然,要注意一些细节问题,例如和事务处理的配合、Mapper映射文件和Mapper接口的命名规范等。 整合SpringSpring MVC和MyBatis可以极大地提高项目的开发效率和代码的可维护性,同时也可以减少各个框架之间的冲突和问题。因此,我在实际的开发中也会选择使用这几个框架进行整合,以便更好地完成项目的开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值