Spring+SpringMVC+Mybatis的搭建教程

Spring+SpringMVC+Mybatis的搭建教程

最近准备开发给予微信服务号的智能设备接入和控制逻辑,参照以前同事建议的SpringMVC框架来搭建后台系统,第一次摸索尝试,把其中的坑记录下来,供其他人参考。
PS:我是web后台开发初学者,其中如有什么不对的地方,请各位不吝指出,共同学习进步,拜谢!
下面进入正题:


准备工作

需要Intellij idea, JDK1.8,和maven,本例使用windows 10配置,具体的配置过程,网上很多,请自行搜索。本例默认上述环境都配置OK。

开始

1. 新建一个maven工程

本例使用module的方式,所以新建的工程是不使用webapp架构,如图
Spring+SpringMVC+Mybatis的搭建教程_第1张图片
Spring+SpringMVC+Mybatis的搭建教程_第2张图片

2. 添加依赖的module,本例中添加api, dao, model和service。

Spring+SpringMVC+Mybatis的搭建教程_第3张图片
api作为我们的controller和web的视图容器,我们使用webapp框架,其他module直接使用maven工程即可。

3. 修改每一个module的pom文件,添加相应的dependancies。如project的pom文件如下:

<properties>
    
    <spring.version>4.3.1.RELEASEspring.version>
    
    <mybatis.version>3.4.0mybatis.version>
    
    <slf4j.version>1.7.21slf4j.version>
properties>
<dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>javaxgroupId>
            <artifactId>javaee-apiartifactId>
            <version>7.0version>
        dependency>

        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.17version>
        dependency>


        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.41version>
            <scope>runtimescope>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.23version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>${mybatis.version}version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.1version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
        dependency>
        

        
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>${slf4j.version}version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>jcl-over-slf4jartifactId>
            <version>${slf4j.version}version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>${slf4j.version}version>
        dependency>

        

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>

    dependencies>
dependencyManagement>

api这个module下的pom也需要添加spring的依赖,如下:

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-coreartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-oxmartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-txartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aopartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>${spring.version}version>
        <exclusions>
            <exclusion>
                <groupId>commons-logginggroupId>
                <artifactId>commons-loggingartifactId>
            exclusion>
        exclusions>
    dependency>
    <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjrtartifactId>
        <version>1.8.9version>
    dependency>
    <dependency>
        <groupId>org.aspectjgroupId>
        <artifactId>aspectjweaverartifactId>
        <version>1.8.9version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aopartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>${spring.version}version>
    dependency>
    
    
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>${mybatis.version}version>
    dependency>
    
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>1.3.1version>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.41version>
        <scope>runtimescope>
    dependency>
    
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>druidartifactId>
        <version>1.0.23version>
    dependency>
    
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>fastjsonartifactId>
        <version>1.2.17version>
    dependency>

    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>javax.servlet-apiartifactId>
        <version>3.1.0version>
    dependency>

    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>jstlartifactId>
        <version>1.2version>
    dependency>

其他module的需要的依赖的关系,详细见附件中的代码。

4. 在api中新建Java目录,并作为主source root,类似service和model,dao中都需要类似操作。

Spring+SpringMVC+Mybatis的搭建教程_第4张图片

5. 在api的resources中新建conf文件夹,添加jdbc.properties(数据库属性文件),spring-mvc.xml和spring-mybatis.xml两个文件,具体内容见工程。在dao中添加conf/mybatis-config.xml和mapper/UserMapper.xml

6. 修改api下的web.xml配置,增加spring的依赖关系

<web-app 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"
         version="3.0">

    
    <context-param>
        <param-name>webAppRootKeyparam-name>
        <param-value>myservice.api.rootparam-value>
    context-param>

    <listener>
        <listener-class>org.springframework.web.util.WebAppRootListenerlistener-class>
    listener>


    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:conf/spring-mybatis.xmlparam-value>
    context-param>

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


    
    <filter>
        <filter-name>SpringEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>SpringEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <servlet>
        <servlet-name>springservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:conf/spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

7. 根据实际的内容分别在api下新建controller类,dao下新建dao文件,model下新建model结构,service下新建service接口和实现,配置好的目录结构如下图:

Spring+SpringMVC+Mybatis的搭建教程_第5张图片
Spring+SpringMVC+Mybatis的搭建教程_第6张图片

8. 增加spring framework支持,勾选Spring和springMVC

Spring+SpringMVC+Mybatis的搭建教程_第7张图片
Spring+SpringMVC+Mybatis的搭建教程_第8张图片
Spring+SpringMVC+Mybatis的搭建教程_第9张图片

9. 配置tomcat

Spring+SpringMVC+Mybatis的搭建教程_第10张图片
Spring+SpringMVC+Mybatis的搭建教程_第11张图片
Spring+SpringMVC+Mybatis的搭建教程_第12张图片

10. 配置成功后,可以运行tomcat。

运行的log会保存在target/api/WEB-INF/logs下,如果遇到什么错误可以查看。log的配置可以查看log4j.properties.。

源码下载

你可能感兴趣的:(Spring+SpringMVC+Mybatis的搭建教程)