zoukankan      html  css  js  c++  java
  • SpringMVC-Spring-Mybatis

    1.配置Spring和Mybatis

    1.beans.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!-- 自动扫描 -->
    <context:component-scan base-package="com.hyd" />
    <context:annotation-config/>

    <!-- 配置DataSource数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/goods" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
    </bean>

    <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/mybatis/mybatis.xml" />
    </bean>

    <!--MapperScannerConfigurer配置-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--basePackage指定要扫描的包,在此包之下的映射器都会被
    搜索到。可指定多个包,包与包之间用逗号或分号分隔-->
    <property name="basePackage" value="com.hyd.mapper"/>
    </bean>

    </beans>

    1.Spring的annotation和自动扫描包

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.hyd" />
    <context:annotation-config/>

    2.配置数据源

    <!-- 配置DataSource数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/goods" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
    </bean>

    3.配置SqlsessionFactoryBean

    <!-- 配置SqlSessionFactoryBean --> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/mybatis/mybatis.xml" />
    </bean>

    4.扫描Mybatis的接口映射的包

    <!--MapperScannerConfigurer配置-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--basePackage指定要扫描的包,在此包之下的映射器都会被
    搜索到。可指定多个包,包与包之间用逗号或分号分隔-->
    <property name="basePackage" value="com.hyd.mapper"/>
    </bean>

    </beans>

    2.SpringMVC的配置

    <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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-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/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 可以扫描controller、service、
    这里让扫描controller, 制定controller的包
    -->
    <context:component-scan base-package="com.hyd.controller"></context:component-scan>

    <!-- 静态资源解析 -->
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/img/" mapping="/img/**"/>

    <mvc:annotation-driven />

    <!-- 试图解析器 , 解析jsp, 默认使用jstl解析, classpath下的得有jstl的包
    -->
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 拦截器 -->
    <mvc:interceptors>
    <!-- 多个拦截器, 顺序执行 -->
    <!-- 登录认证拦截七 -->
    <mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean class="com.hyd.interceptor.LoginInterceptor"></bean>
    </mvc:interceptor>
    </mvc:interceptors>
    </beans>

    1.扫描Controller包

    <!-- 可以扫描controller、service、
    这里让扫描controller, 制定controller的包
    -->
    <context:component-scan base-package="com.hyd.controller"></context:component-scan>

    2.mvc的annotation配置

    <mvc:annotation-driven />

    3.试图解析器

    <!-- 试图解析器 , 解析jsp, 默认使用jstl解析, classpath下的得有jstl的包
    -->
    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

    4.拦截器

    <!-- 拦截器 -->
    <mvc:interceptors>
    <!-- 多个拦截器, 顺序执行 -->
    <!-- 登录认证拦截七 -->
    <mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean class="com.hyd.interceptor.LoginInterceptor"></bean>
    </mvc:interceptor>
    </mvc:interceptors>

    3.在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/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <!-- Spring和mybatis的配置文件 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/beans.xml</param-value>
    </context-param>


    <!-- Spring监听器 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Spring MVC servlet -->
    <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring/springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
    <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置SESSION超时,单位是分钟 -->
    <session-config>
    <session-timeout>15</session-timeout>
    </session-config>


    </web-app>

    1.设置spring的配置文件

    <!-- Spring和mybatis的配置文件 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/beans.xml</param-value>
    </context-param>

    2.配置spring的监听器,加载spring配置文件

    <!-- Spring监听器 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    3.配置Springmvc前端控制器

    <!-- Spring MVC servlet -->
    <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring/springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
    <url-pattern>*.action</url-pattern>
    </servlet-mapping>

  • 相关阅读:
    关于微信最新推出的应用号的看法
    HTML常见标签
    重读《从菜鸟到测试架构师》-- 模拟客户的访问行为(上)
    重读《从菜鸟到测试架构师》-- 大促带来的灾难
    重读《从菜鸟到测试架构师》-- 功能测试之百种变身
    重读《从菜鸟到测试架构师》-- 对黑盒子的全方位照明
    重读《从菜鸟到测试架构师》-- 如何把黑盒子分块
    重读《从菜鸟到测试架构师》-- 黑色的盒子里有什么(下)
    重读《从菜鸟到测试架构师》--黑色的盒子里有什么(中)
    重读《从菜鸟到测试架构师》-- 黑色的盒子里面有什么(上)
  • 原文地址:https://www.cnblogs.com/handsome1013/p/5209246.html
Copyright © 2011-2022 走看看