Spring、springMVC、Mybatis整合

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值