乐趣区

关于java:猿实战02SpringMVCSpringmybatismaven多模块框架搭建

猿实战是一个原创系列文章,通过实战的形式,采纳前后端拆散的技术联合 SpringMVC Spring Mybatis, 手把手教你撸一个残缺的电商零碎,跟着教程走下来,变身猿人找到工作不是问题。想要一起实战吗?,关注公主号 猿人工厂 获取根底代码 ,手把手带你开发属于本人的电商零碎。 从明天开始,咱们将进入猿实战的阶段,实现一个前后端拆散的电商零碎,没有开发框架怎么行?明天是第一个系列,猿人君将带你搭建后端开发框架。

创立利用父模块

父模块负责管制整个利用的构造, 因为 pzmall-basic 作为父我的项目的存在,无需编写代码, 记得在创立实现后,删除我的项目下的 src 目录。

创立子模块

思考到咱们须要构建一个 B2C(兼容商家端的利用)咱们的利用依照代码职责的划分,咱们的利用临时波及到一下几个模块。

pzmall-basic-common: 负责编写我的项目专用的一些内容,比方工具类。

pzmall-basic-domain: 负责我的项目波及的实体内容编写。

pzmall-basic-dao: 负责数据长久层的内容编写。

pzmall-basic-service:负责业务逻辑层的内容编写。

pzmall-basic-search-util:负责我的项目搜索引擎工具相干的内容编写。

pzmall-basic-web: 负责我的项目网站端业务内容编写。

pzmall-basic-manweb: 负责我的项目经营治理端业务内容编写。

接下来咱们一起来创立子项目 pzmall-basic-common。

接下来,以葫芦画瓢,建设 pzmall-basic-domain、pzmall-basic-dao、pzmall-basic-service、pzmall-basic-search-util 几个子模块,最初失去如下后果。

因为 pzmall-basic-web 和 pzmall-basic-manweb 属于 web 利用,在创立时有所区别,在抉择 maven 我的项目类型时记得抉择正确的 archetype,org.apache.maven.archetypes:maven-archetype-webapp.

实现后,并在模块的 src/main 目录下建设 java 和 resources 两个目录。

最初我的项目的整体后果成果如下,为了测试下成果,编译下我的项目。

调整我的项目依赖

就我的项目模块之间的依赖关系而言,pzmall-basic-common 和 pzmall-basic-serch-util 因为编写的是工具类,绝对独立,不依赖于其它模块。pzmall-basic-domain、pzmall-basic-dao、pzmall-basic-service、三者的依赖是存在关联关系的,pzmall-basic-service 依赖 pzmall-basic-dao,pzmall-basic-dao 依赖 pzmall-basic-domain。而之前提及的 pzmall-basic-common 和 pzmall-basic-serch-util 更多的会在 service 层被应用。所以,pzmall-basic-service 会依赖 pzmall-basic-common 和 pzmall-basic-serch-util。留神哈,pzmall-basic-domain 要尽量放弃洁净,不要和工具类扯上什么关系,否则对今后的架构方面的扩大,是不敌对的。而 pzmall-basic-web 和 pzmall-basic-manweb 作为我的项目最终要部署的 war 包,依赖 pzmall-basic-service 就能够了。

接下来咱们调整引入各个层面所须要的依赖,搭建咱们我的项目开发所须要的框架。

批改父我的项目 pzmall-basic 下 pom.xml 内容为:

<?xmlversion=”1.0″ encoding=”UTF-8″?>
<projectxmlns=”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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.pz</groupId>

<artifactId>pzmall-basic</artifactId>

<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<!-- 集中定义依赖版本号 -->
<properties>
   <junit.version>4.12</junit.version>
   <spring.version>4.2.4.RELEASE</spring.version>
   <servlet-api.version>2.5</servlet-api.version>
   <mybatis.version>3.2.8</mybatis.version>
    <mybatis.spring.version>1.2.2</mybatis.spring.version>
   <mysql.version>5.1.32</mysql.version>
   <druid.version>1.0.9</druid.version>
   <commons-fileupload.version>1.3.1</commons-fileupload.version>
   <freemarker.version>2.3.23</freemarker.version>
   <security.version>3.2.3.RELEASE</security.version>
</properties>
<modules>
   <module>pzmall-basic-common</module>
   <module>pzmall-basic-domain</module>
   <module>pzmall-basic-dao</module>
    <module>pzmall-basic-service</module>
   <module>pzmall-basic-search-util</module>
   <module>pzmall-basic-web</module>
   <module>pzmall-basic-manweb</module>
</modules>
<dependencyManagement>
    <dependencies>
        <!-- Spring -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
           <version>${spring.version}</version>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
           <version>${spring.version}</version>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>${spring.version}</version>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jms</artifactId>
           <version>${spring.version}</version>
        </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context-support</artifactId>
           <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>${spring.version}</version>
        </dependency>
        <dependency>
           <groupId>junit</groupId>
            <artifactId>junit</artifactId>
           <version>4.9</version>
        </dependency>
        <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
            <version>1.2.29</version>
        </dependency>
        <dependency>
           <groupId>javassist</groupId>
           <artifactId>javassist</artifactId>
           <version>3.11.0.GA</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
           <artifactId>commons-codec</artifactId>
           <version>1.10</version>
        </dependency>

        <dependency>
           <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
           <version>${servlet-api.version}</version>
           <scope>provided</scope>
        </dependency>
        <!-- Mybatis -->
        <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>${mybatis.version}</version>
        </dependency>
        <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis-spring</artifactId>
           <version>${mybatis.spring.version}</version>
        </dependency>

        <!-- MySql -->
        <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- 连接池 -->
        <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>


        <!-- 文件上传组件 -->
        <dependency>
           <groupId>commons-fileupload</groupId>
           <artifactId>commons-fileupload</artifactId>
           <version>${commons-fileupload.version}</version>
        </dependency>

        <dependency>
           <groupId>org.freemarker</groupId>
           <artifactId>freemarker</artifactId>
           <version>${freemarker.version}</version>
        </dependency>

        <!-- 身份验证 -->
        <dependency>
           <groupId>org.springframework.security</groupId>
           <artifactId>spring-security-web</artifactId>
           <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
           <groupId>org.springframework.security</groupId>
           <artifactId>spring-security-config</artifactId>
           <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.github.penggle</groupId>
           <artifactId>kaptcha</artifactId>
           <version>2.3.2</version>
            <exclusions>
                <exclusion>
                   <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
           <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
           <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
           <groupId>org.jasig.cas.client</groupId>
           <artifactId>cas-client-core</artifactId>
           <version>3.3.3</version>
            <!-- 排除 log4j 包抵触 -->
            <exclusions>
                <exclusion>
                   <groupId>org.slf4j</groupId>
                   <artifactId>log4j-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
           <groupId>org.apache.httpcomponents</groupId>
           <artifactId>httpcore</artifactId>
            <version>4.4.4</version>
        </dependency>
        <dependency>
           <groupId>org.apache.httpcomponents</groupId>
           <artifactId>httpclient</artifactId>
           <version>4.5.3</version>
        </dependency>
        <dependency>
           <groupId>dom4j</groupId>
           <artifactId>dom4j</artifactId>
           <version>1.6.1</version>
        </dependency>
        <dependency>
           <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
           <version>1.4.01</version>
        </dependency>
    </dependencies>

</dependencyManagement>

</project>

批改 pzmall-basic-common 下 pom.xml 内容为:

<?xmlversion=”1.0″ encoding=”UTF-8″?>

<projectxmlns=”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">
<parent>
   <artifactId>pzmall-basic</artifactId>
    <groupId>com.pz</groupId>
   <version>1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>pzmall-basic-common</artifactId>

<name>pzmall-basic-common</name>

<dependencies>
    <dependency>
       <groupId>com.pz</groupId>
       <artifactId>pzmall-basic-domain</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
       <groupId>commons-fileupload</groupId>
       <artifactId>commons-fileupload</artifactId>
    </dependency>

    <!-- 缓存 -->
    <dependency>
       <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>
    <dependency>
       <groupId>org.springframework.data</groupId>
       <artifactId>spring-data-redis</artifactId>
    </dependency>
    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
    </dependency>
</dependencies>

</project>

  批改 pzmall-basic-domain 下 pom.xml 内容为:

<?xmlversion="1.0" encoding="UTF-8"?>
 
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
       <artifactId>pzmall-basic</artifactId>
        <groupId>com.pz</groupId>
       <version>1.0-SNAPSHOT</version>
    </parent>
   <modelVersion>4.0.0</modelVersion>
 
   <artifactId>pzmall-basic-domain</artifactId>
 
   <name>pzmall-basic-domain</name>
    <!-- FIXME change it to the project'swebsite -->
   <url>http://www.example.com</url>
    <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
           <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>
</project>

批改 pzmall-basic-dao 下 pom.xml 内容为:

<?xmlversion="1.0" encoding="UTF-8"?>
 
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
       <artifactId>pzmall-basic</artifactId>
        <groupId>com.pz</groupId>
       <version>1.0-SNAPSHOT</version>
    </parent>
   <modelVersion>4.0.0</modelVersion>
 
   <artifactId>pzmall-basic-dao</artifactId>
 
    <name>pzmall-basic-dao</name>
    <!-- FIXME change it to the project'swebsite -->
   <url>http://www.example.com</url>
    <dependencies>
 
 
        <!-- Mybatis -->
        <dependency>
           <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis-spring</artifactId>
        </dependency>
      
        <!-- MySql -->
        <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 连接池 -->
        <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
        </dependency>
 
        <dependency>
           <groupId>com.pz</groupId>
           <artifactId>pzmall-basic-domain</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
 
 
    <build>
        <resources>
            <resource>
               <directory>src/main/resources</directory>
               <filtering>true</filtering>
            </resource>
        </resources>
 
    </build>
</project>

批改 pzmall-basic-service 下 pom.xml 内容为:

<?xmlversion="1.0" encoding="UTF-8"?>
 
<projectxmlns="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">
    <parent>
       <artifactId>pzmall-basic</artifactId>
        <groupId>com.pz</groupId>
       <version>1.0-SNAPSHOT</version>
    </parent>
   <modelVersion>4.0.0</modelVersion>
 
    <artifactId>pzmall-basic-service</artifactId>
    <version>1.0-SNAPSHOT</version>
 
   <name>pzmall-basic-service</name>
    <!-- FIXME change it to the project'swebsite -->
   <url>http://www.example.com</url>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
           <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
        </dependency>
 
        <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
           <groupId>javassist</groupId>
           <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
           <groupId>commons-codec</groupId>
           <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>servlet-api</artifactId>
           <scope>provided</scope>
        </dependency>
 
        <dependency>
           <groupId>org.freemarker</groupId>
           <artifactId>freemarker</artifactId>
        </dependency>
 
        <dependency>
           <groupId>com.pz</groupId>
           <artifactId>pzmall-basic-dao</artifactId>
           <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
           <groupId>com.pz</groupId>
           <artifactId>pzmall-basic-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

批改 pzmall-basic-search-util 下 pom.xml 内容为:

<?xmlversion="1.0" encoding="UTF-8"?>
 
<projectxmlns="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">
    <parent>
       <artifactId>pzmall-basic</artifactId>
        <groupId>com.pz</groupId>
       <version>1.0-SNAPSHOT</version>
    </parent>
   <modelVersion>4.0.0</modelVersion>
 
    <artifactId>pzmall-basic-search-util</artifactId>
    <version>1.0-SNAPSHOT</version>
 
   <name>pzmall-basic-search-util</name>
    <!-- FIXME change it to the project'swebsite -->
   <url>http://www.example.com</url>
 
    <dependencies>
        <dependency>
            <groupId>com.pz</groupId>
           <artifactId>pzmall-basic-domain</artifactId>
           <version>1.0-SNAPSHOT</version>
        </dependency>
       
        <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
        </dependency>
    </dependencies>
</project>

批改 pzmall-basic -web 下 pom.xml 内容为:

<?xmlversion="1.0" encoding="UTF-8"?>
 
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
       <artifactId>pzmall-basic</artifactId>
        <groupId>com.pz</groupId>
       <version>1.0-SNAPSHOT</version>
    </parent>
   <modelVersion>4.0.0</modelVersion>
 
   <artifactId>pzmall-basic-web</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
 
    <name>pzmall-basic-web MavenWebapp</name>
    <!-- FIXME change it to the project'swebsite -->
   <url>http://www.example.com</url>
 
    <dependencies>
        <!-- Spring -->
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
           <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
        </dependency>
 
 
        <dependency>
           <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
           <groupId>commons-codec</groupId>
           <artifactId>commons-codec</artifactId>
        </dependency>
 
 
        <dependency>
           <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
 
        <!-- 身份认证的依赖 -->
        <dependency>
           <groupId>org.springframework.security</groupId>
           <artifactId>spring-security-web</artifactId>
        </dependency>
 
        <dependency>
           <groupId>org.springframework.security</groupId>
           <artifactId>spring-security-config</artifactId>
        </dependency>
 
 
 
        <dependency>
           <groupId>com.pz</groupId>
            <artifactId>pzmall-basic-service</artifactId>
           <version>1.0-SNAPSHOT</version>
        </dependency>
 
 
    </dependencies>
 
 
    <build>
        <plugins>
            <plugin>
               <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
               <version>2.2</version>
                <configuration>
                    <!-- 指定端口 -->
                   <port>9203</port>
                    <!-- 申请门路 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

批改 pzmall-basic -manweb 下 pom.xml 内容为:

<?xmlversion="1.0" encoding="UTF-8"?>
 
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
       <artifactId>pzmall-basic</artifactId>
        <groupId>com.pz</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
   <modelVersion>4.0.0</modelVersion>
 
   <artifactId>pzmall-basic-manweb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
 
    <name>pzmall-basic-manweb MavenWebapp</name>
    <!-- FIXME change it to the project'swebsite -->
   <url>http://www.example.com</url>
 
 
    <dependencies>
        <!-- Spring -->
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
           <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
        </dependency>
 
 
        <dependency>
           <groupId>javassist</groupId>
           <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
           <groupId>commons-codec</groupId>
           <artifactId>commons-codec</artifactId>
        </dependency>
 
 
        <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
 
        <!-- 身份认证的依赖 -->
        <dependency>
            <groupId>org.springframework.security</groupId>
           <artifactId>spring-security-web</artifactId>
        </dependency>
 
        <dependency>
           <groupId>org.springframework.security</groupId>
           <artifactId>spring-security-config</artifactId>
        </dependency>
 
 
 
        <dependency>
           <groupId>com.pz</groupId>
           <artifactId>pzmall-basic-service</artifactId>
           <version>1.0-SNAPSHOT</version>
        </dependency>
 
 
    </dependencies>
 
 
    <build>
        <plugins>
            <plugin>
               <groupId>org.apache.tomcat.maven</groupId>
               <artifactId>tomcat7-maven-plugin</artifactId>
               <version>2.2</version>
                <configuration>
                    <!-- 指定端口 -->
                   <port>9201</port>
                    <!-- 申请门路 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

搭建开发框架

1. 在 pzmall-basic-dao 模块的 src/main 目录下建设 resources/mybatis 目录, 并在 mybaits 目录下建设 SqlMapConfig.xml 文件以及 mapper 目录。

SqlMapConfig.xml 下用于配置 mybatis 全局相干的一些属性,以及加载哪些 mapper 配置文件,mapper 目录下将来将按模块寄存相干的 mapper 文件。

2. 在 pzmall-basic-manweb 目录的 resources 目录下别离建设 spring 和 config 两个目录。并在 resources 目录下建设 applicationContext.xml 文件。如下图所示。

3. 整合 mybatis,在 spring 目录中建设文件,spring-dao.xml 并输出如下内容。

<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsd"default-autowire="byName">
 
       <!-- 数据库连接池 -->
       <bean id="dataSource"class="com.alibaba.druid.pool.DruidDataSource"
              destroy-method="close">
              <property name="url"value="${jdbc.url}" />
              <propertyname="username" value="${jdbc.username}" />
              <propertyname="password" value="${jdbc.password}" />
              <propertyname="driverClassName" value="${jdbc.driver}" />
              <propertyname="maxActive" value="10" />
              <propertyname="minIdle" value="5" />
       </bean>
      
       <!-- 让 spring 治理 sqlsessionfactory 应用 mybatis 和 spring 整合包中的 -->
       <beanid="mybatisSqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
              <!-- 数据库连接池 -->
              <propertyname="dataSource" ref="dataSource" />
              <!-- 加载 mybatis 的全局配置文件 -->
              <propertyname="configLocation"value="classpath:mybatis/SqlMapConfig.xml" />
       </bean>
       <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="sqlSessionFactoryBeanName"value="mybatisSqlSessionFactory"/>
              <propertyname="basePackage" value="com.pz.basic.mall.dao" />
       </bean>
</beans>

4. 配置事务相干的内容,在 spring 目录中建设文件,spring-tx.xml 并输出如下内容。​​​​​​​

<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd
              http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
 
 
    <!-- 事务管理器  --> 
    <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <propertyname="dataSource" ref="dataSource" /> 
    </bean> 
     
    <!-- 开启事务管制的注解反对 --> 
    <tx:annotation-driventransaction-manager="transactionManager"/>
  
</beans>

5. 配置 SpringMVC 相干内容,先在 spring 目录中建设文件 spring-service.xml 备用,而后再建设一个 springmvc.xml 文件,并在文件中输出如下内容。​​​​​​​

<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"default-autowire="byName">
      
       <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
          <beanclass="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                     <propertyname="supportedMediaTypes">
                            <list>
                                   <value>text/html;charset=UTF-8</value>
                                   <value>application/json</value>
                                   <value>application/xml;charset=UTF-8</value>
                            </list>
                     </property>
                     <propertyname="features">
                            <list>
                                   <!-- 默认的意思就是不配置这个属性,配置了就不是默认了 -->
                                   <!-- 是否输入值为 null 的字段,默认是 false -->
 
                                   <value>WriteMapNullValue</value>
 
                                   <value>WriteNullNumberAsZero</value>
                                   <value>WriteNullListAsEmpty</value>
                                   <value>WriteNullStringAsEmpty</value>
                                   <value>WriteNullBooleanAsFalse</value>
                                   <value>WriteDateUseDateFormat</value>
 
                            </list>
                     </property>
          </bean>
        </mvc:message-converters> 
       </mvc:annotation-driven>
 
       <!-- 配置多媒体解析器 -->
       <bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
              <propertyname="defaultEncoding" value="UTF-8"></property>
              <!-- 设定文件上传的最大值 5MB,5*1024*1024 -->
              <propertyname="maxUploadSize" value="5242880"></property>
       </bean>
 
       <!-- 跨域问题解决 -->
       <mvc:interceptors>
              <mvc:interceptor>
                     <mvc:mappingpath="/**" />
                     <beanclass="com.pz.basic.mall.interceptor.CommonInterceptor">
                            <propertyname="excludedUrls">
                                   <list>
                                          <value>/</value>
                                   </list>
                            </property>
                     </bean>
              </mvc:interceptor>
 
       </mvc:interceptors>
 
 
 
</beans>

6. 因为咱们打算应用前后端拆散的形式来开发我的项目,须要解决跨域问题,咱们应用了拦截器的形式来解决这个问题,也在 springMVC 中做了相应的配置。在 pzmall-basic-manweb 模块中编写代码。​​​​​​​

package com.pz.basic.mall.interceptor;
 
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
 
/**
 * 解决跨域申请
 */
public classCommonInterceptor implements HandlerInterceptor {
 
    private List<String> excludedUrls;
 
    public List<String> getExcludedUrls(){return excludedUrls;}
 
    public voidsetExcludedUrls(List<String> excludedUrls) {this.excludedUrls = excludedUrls;}
 
    /**
     *
     * 在业务处理器解决申请之前被调用如果返回 false
     * 从以后的拦截器往回执行所有拦截器的 afterCompletion(),
     * 再退出拦截器链, 如果返回 true 执行下一个拦截器,
     * 直到所有的拦截器都执行结束再执行被拦挡的 Controller
     * 而后进入拦截器链,
     * 从最初一个拦截器往回执行所有的 postHandle()
     * 接着再从最初一个拦截器往回执行所有的 afterCompletion()
     *
     * @param request
     *
     * @param response
     */
    public boolean preHandle(HttpServletRequestrequest, HttpServletResponse response,
                             Object handler)throws Exception {response.setHeader("Access-Control-Allow-Origin","*");
       response.setHeader("Access-Control-Allow-Methods", "POST,GET, PUT, OPTIONS, DELETE, PATCH");
        response.setHeader("Access-Control-Max-Age","3600");
       response.setHeader("Access-Control-Allow-Headers","X-Token, Origin, X-Requested-With, Content-Type, Accept");
       response.setHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("XDomainRequestAllowed","1");
        return true;
    }
 
    // 在业务处理器解决申请执行实现后, 生成视图之前执行的动作
    public void postHandle(HttpServletRequestrequest, HttpServletResponse response, Object handler,
                           ModelAndViewmodelAndView) throws Exception { }
 
    /**
     *
     * 在 DispatcherServlet 齐全解决完申请后被调用
     * 当有拦截器抛出异样时,
     * 会从以后拦截器往回执行所有的拦截器的 afterCompletion()
     *
     * @param request
     *
     * @param response
     *
     * @param handler
     *
     */
    public voidafterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler,Exception ex) throws Exception {}}

7. 批改 pzmall-basic-manweb 模块中的 resources/config 目录下减少 db.properties 文件,退出数据库相干的配置。​​​​​​​

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/pz_mall_basic?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

8. 在 resources 目录下新增 log4j.properties 文件,减少日志相干配置。​​​​​​​

log4j.rootLogger=trace,console
logging.level.com.pz.mall.dao=DEBUG
log4j.appender.console= org.apache.log4j.ConsoleAppender
log4j.appender.console.Target= System.out
log4j.appender.console.layout= org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH:mm:ss}]%m%n
 
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.Java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

9. 批改 pzmall-basic-manweb 模块中的 web.xml 文件,减少利用启动相干配置:​​​​​​​

<?xmlversion="1.0" encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       version="2.5">
       <!-- 解决 post 乱码 -->
       <filter>
              <filter-name>CharacterEncodingFilter</filter-name>
              <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
              <init-param>
                     <param-name>encoding</param-name>
                     <param-value>utf-8</param-value>
              </init-param>
              <init-param>
                     <param-name>forceEncoding</param-name>
                     <param-value>true</param-value>
              </init-param>
       </filter>
       <filter-mapping>
              <filter-name>CharacterEncodingFilter</filter-name>
              <url-pattern>/*</url-pattern>
       </filter-mapping>
 
       <servlet>
              <servlet-name>springmvc</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <!-- 指定加载的配置文件,通过参数 contextConfigLocation 加载 -->
              <init-param>
                     <param-name>contextConfigLocation</param-name>
                     <param-value>classpath:applicationContext.xml</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
       </servlet>
 
       <servlet-mapping>
              <servlet-name>springmvc</servlet-name>
              <url-pattern>/*</url-pattern>
       </servlet-mapping>
 
     <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
 
    <context-param>
       <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
 
</web-app>

到此为止,咱们我的项目的开发框架搭建结束。
  如学习过程中遇到疑难杂症,本人切实搞不定,能够加我的微信,帮你解决。

退出移动版