SpringMVC+MyBatis整合(3)特别篇SpringMVC配置

  1. 云栖社区>
  2. 博客>
  3. 正文

SpringMVC+MyBatis整合(3)特别篇SpringMVC配置

ycy蓝码 2015-08-16 22:26:23 浏览832
展开阅读全文
SpringMVC配置
导言
这篇文章应该放在最前面的,但是由于本人之前没用过mybatis所以,把mybatis整合提前。但是至少步骤变化而已,对于mvc的配置一样不影响。名字叫做特别篇````
   一、jar包的引入
在jia上面有注释,除开标注mybatis的,其他包均为mvc包。你会发现这个神奇的mvc包,已经加载了所有的你需要的spring包。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ycy.springmvc</groupId>
    <artifactId>springmvc</artifactId>
    <packaging>war</packaging>
    <version>1.0-springmvc</version>
    <url>http://maven.apache.org</url>


    <build>
        <plugins>
            <!--=================generator-mybatis插件===========================-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--此为generator配置文件路径-->
                    <configurationFile>src/test/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!--maven-resources-plugin配置resources文件位置(不建议)-->
        <!-- <dependency>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-resources-plugin</artifactId>
             <version>2.4.3</version>
         </dependency>-->
        <!--junit测试jar-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--==========================mysql数据库驱动配置==============================-->
        <dependency>
            <groupId>Mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.31</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.15</version>
        </dependency>
        <!--==========================spring配置======================================-->
        <!--注解、注入实体-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <!--事务管理-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <!--junit测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <!--事务管理-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>
        <!--==========================mybatis配置=========================================-->
        <!--mybatis驱动包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!--mybatis-spring结核驱动包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!--==========================httpservlet=========================================-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <!--================json转换3人组文件上传(MappingJackson2HttpMessageConverter)==========-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.6.1</version>
        </dependency>
        <!--==========================文件上传=========================================-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>
</project>
二、项目结构


三、配置spring-mvc文件
注意点:扫描包为主要的,我也看别人经验,实在扫描不到就加一个inclu-filter,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-4.0.xsd
		http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器(经验:加上include-filter) -->
    <context:component-scan base-package="com.ycy.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <!-- 视图解析器也可以用标签:p:prefix="/" p:suffix=".jsp" -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 对controller的请求实体转换为JSon(MappingJackson2HttpMessageConverter为spring4里面包) -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="prettyPrint" value="true"/>
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json</value>
                        <value>application/xml</value>
                        <value>text/json</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>
    <!-- 配置拦截器(拦截所有请求) -->
    <!--
           <mvc:interceptors>
                  <mvc:interceptor>
                         <mvc:mapping path="/**" />
                         <bean class="com.ycy.suport.controller.OperateInterceptor"></bean>
                  </mvc:interceptor>
           </mvc:interceptors>
           -->
</beans>

 

 

四、spring.xml的配置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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--配置文件properties-->
    <context:property-placeholder location="classpath:config/development.properties" />
    <!-- 支持标注 -->
	<context:annotation-config />
	<context:component-scan base-package="com.ycy">
      <context:include-filter type="aspectj" expression="com.ycy.*" />
   </context:component-scan>

</beans>




五、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"
         metadata-complete="false">
    <display-name>springmvc</display-name>
    <!--===============加载spring容器配置文件1================ -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext.xml,classpath*:spring/spring-mybatis.xml</param-value>
    </context-param>
    <!--===============字符集过滤器2========================== -->
    <filter>
        <description>字符集过滤器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <description>字符集编码</description>
            <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>
    <!-- ===========spring容器监听==================== -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- ===========防止spring内存溢出监听器3==================== -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!--==========spring mvc servlet============================ -->
    <servlet>
        <description>spring mvc DispatcherServlet</description>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <description>spring mvc 配置文件</description>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/*</url-pattern>
        <!-- <url-pattern>*.do</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>

六、controller的编写

UserController.java

package com.ycy.controller;

import com.ycy.model.UserAccount;
import com.ycy.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2015/8/15 0015.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private Userservice userService;
    @ResponseBody
    @RequestMapping("/getuser/{id}")
    public UserAccount getUserByUserId(@PathVariable String id, HttpServletRequest request ){
        UserAccount u = userService.getUserAccountById(new Long(id));
        request.setAttribute("user", u);
        return u;
    }
}<span style="color:#3366ff;font-weight: bold;">
</span>
七、测试结果


后续:

项目文件百度分享:http://pan.baidu.com/s/1eQEqYem

此文为本人初次编写博客,请多多包涵。很多拙劣之处多多指正。我也是看了网络一些的配置,还有根据项目中实际运用为前提编写此博客。本次编写时间2015-8-16 23:04:47,我在博客上和视频上都没有最新的mvc整合,为他人种树吧。如果有疑问请垂询我的qq:48599355 或者加一个群吧,反正只要我一个人,qq群号:78275755

如果你看到这篇文章在2年以后,请移步吧, 估计新的东西早就替代我啦····嘎嘎

网友评论

登录后评论
0/500
评论
ycy蓝码
+ 关注