使用IDEA构建Spring-boot多模块项目配置流程

使用IDEA构建Spring-boot多模块项目配置流程

1.创建项目

  • 点击Create New Project

  • 在左侧选中Spring Initializer,保持默认配置,点击下一步。

  • 在Group和Artifact栏中填入项目坐标,点击下一步。

groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
  groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
  比如我创建一个项目,我一般会将groupId设置为cn.pq,cn表示域为中国,pq是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.pq.testProj打头的,如果有个StudentPeng,它的全路径就是cn.pq.testProj.StudentPeng

  • 这一步可以选择模板。如果不想将项目分成多个模块,可以勾选web中的spring web选项,直接使用提供的web模板。
    为了将项目分为多个模块,这里先不勾选任何模板,直接点击下一步。

  • 填写项目名称并选择存放路径,点击下一步。

2.目录结构配置

  • 等待项目创建完毕,勾选右下角的Enable Auto-Import自动导入添加的Maven依赖。初次创建Spring-boot项目可能需要等待一段时间。

  • 创建DAO层:在项目根目录上点右键-New-Module

  • 选择Maven项目,点击下一步,命名为demo_dao,点击Finish。

  • 创建Service层,方法同上。

  • 创建Web层:选择Maven项目后,在右侧勾选Create from archtype,并在下方选中maven-archetype-webapp模板,点击next并命名为demo_web,点击next。

  • 保持默认,点击Finish。等待web模块配置完成。

  • 打开web模块,现在的目录结构如下图所示。

  • 接下来配置web模块中缺少的java目录和resources目录。在demo_web目录上点击右键-New-Directory,双击添加java目录。再同样的操作添加resources目录。


  • 如果这里没有目录提示,需要手动创建,并在Project Structure的Modules中将其分别配置为demo_web的Sources和Resources文件夹。

  • 配置好后的整体目录结构如下(.mvn HELP.md mvnw mvnw.cmd可以删除)

3.文件配置

1) 配置pom.xml中的Maven依赖
  • demo_dao/pom.xml(demo_dao)
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo_dao</artifactId>

    <dependencies>
        <!--mybatis相关-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <!--spring依赖相关-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <!--整合相关-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>
    </dependencies>

</project>
  • demo_service/pom.xml(demo_service)
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo_service</artifactId>

    <dependencies>
        <!--持久层依赖-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>demo_dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--声明式事务-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>
    </dependencies>

</project>
  • demo_web/pom.xml(demo_web)
<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo_web</artifactId>
    <packaging>war</packaging>

    <name>demo_web Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--业务层依赖-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>demo_service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--servlet和jstl-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>demo_web</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
2) 配置文件夹路径
  • 在demo_dao/src/main/java目录下创建包com.example.demo.ectity、com.example.demo.dao、com.example.demo.global分别用于存放实体类对象、持久类操作对象和工具类
  • 在demo_dao/src/main/resources目录下创建文件夹com/example/demo/dao用于存放数据库mapper映射文件。注意创建目录时要用/分隔而不能使用.分隔,否则会产生报错。同时注意该目录路径必须与上一点中的dao包路径相同
  • 在demo_service/src/main/java目录下创建com.example.demo包用于存放service接口类,并在该com.example.demo包下创建Impl包用于存放service接口的实现类
  • 在demo_web/src/main/java目录下创建包com.example.demo.controller、com.example.global分别用于存放控制器类和工具类
3) 配置spring.xml
  • 在demo_dao/src/main/resources目录下创建spring_dao.xml
<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"
       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">

    <!--spring注解自动扫描-->
    <context:component-scan base-package="com.example.demo.dao"/>

    <!--Spring整合Mybatis-->
    <!--数据源-->
    <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/database?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"/>
        <property name="username" value="数据库用户名"/>
        <property name="password" value="数据库密码"/>
    </bean>
    <!--session工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.example.demo.entity"/>
    </bean>
    <!--持久化对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.example.demo.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

</beans>
  • 在demo_service/src/main/resources目录下创建spring_service.xml
<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: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.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">

    <!--引入spring_dao中的配置-->
    <import resource="spring_dao.xml"/>

    <!--spring注解自动扫描-->
    <context:component-scan base-package="com.example.demo"/>
    <!--aspectj切面扫描-->
    <aop:aspectj-autoproxy/>

    <!--声明式事务-->
    <!--事务管理器-->
    <bean name="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--通知-->
    <tx:advice id="txAdvice" transaction-manager="transationManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="search*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--织入-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.example.demo.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
</beans>
  • 在demo_web/src/main/resources目录下创建spring_web.xml
<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"
       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">

    <!--引入spring_service中的配置-->
    <import resource="spring_service.xml"/>

    <!--spring注解自动扫描-->
    <context:component-scan base-package="com.example.demo.controller"/>
    <!--spring-mvc注解驱动-->
    <mvc:annotation-driven/>

    <!--静态资源交由默认servlet处理-->
    <mvc:default-servlet-handler/>

    <!--spring视图转换器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
        <!--<property name="suffix" value=".jsp"/>-->
    </bean>

</beans>
  • 打开Project Structure配置Spring。选择左侧的Modules,点击右侧三个子Module下的Spring选项。点击右上侧加号,将分别将spring-dao.xml、spring-service.xml、spring-web.xml文件选中配置到对应Spring模块下。

4) 项目最终结构

posted @ 2020-02-06 16:25  还没有女朋友的执念  阅读(944)  评论(0编辑  收藏  举报