美文网首页
用IDEA创建一个SSM Web项目时常见的项目配置

用IDEA创建一个SSM Web项目时常见的项目配置

作者: 醛烷基八氮 | 来源:发表于2020-10-21 15:27 被阅读0次

  在IDEA中一个项目创建后,还需要许多配置的过程,下面描述一下我自己的配置过程。

1. 完善目录结构

  刚创建好的项目需要根据项目完善目录结构,我的目录结构如下


image.png

2.导入需要依赖的坐标

  项目创建完毕后还需要导入许多依赖的包,这个项目中我导入的包如下,都是一些常用的包。

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

    <lombok.version>1.18.12</lombok.version>
    <javaee.version>8.0</javaee.version>
    <mysql.version>8.0.20</mysql.version>
    <druid.version>1.1.23</druid.version>
    <mybatis.version>3.5.5</mybatis.version>
    <pagehelper.version>5.2.0</pagehelper.version>
    <spring.version>5.2.7.RELEASE</spring.version>
    <mybatis.spring.version>2.0.5</mybatis.spring.version>
    <jackson.version>2.11.0</jackson.version>
    <fileupload.version>1.4</fileupload.version>
    <log4j.version>2.13.3</log4j.version>
    <slf4j.version>1.7.30</slf4j.version>
    <aspectjweaver.version>1.9.6</aspectjweaver.version>
    <jstl.version>1.2</jstl.version>
    <hibernate-validator.version>6.1.5.Final</hibernate-validator.version>
    <junit.version>4.12</junit.version>

</properties>

<dependencies>

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

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>${javaee.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>${jstl.version}</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <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>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>

    <!--spring整合mybatis需要的依赖坐标-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis.spring.version}</version>
    </dependency>
    <!--分页插件-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>${pagehelper.version}</version>
    </dependency>

    <!--spring-test的依赖坐标-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</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>

    <!--AOP的依赖坐标-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectjweaver.version}</version>
    </dependency>


    <!--JSON数据依赖的坐标-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>

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

    <!--JPA验证-->
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate-validator.version}</version>
    </dependency>

    <!--日志需要的依赖坐标-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <!--日志桥接的依赖坐标-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <!--桥接Log4j-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <!--桥接spring的jcl日志-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jcl</artifactId>
        <version>${slf4j.version}</version>
    </dependency>

</dependencies>

3.添加配置文件

  在导入相关依赖的包之后需要添加配置文件,我的项目配置文件如下

image.png

其中mybatis-config.xml是Mybatis的配置文件,applicationContext.xml是Web项目中Root WebApplicationContext容器的配置,webApplicationContext是DispatcherServlet中的相关配置,generator-config是相关的实体类生成插件的配置文件,log4j2是日志工具,datasource.properties是数据库配置文件。

3.1 datasource.properties数据库配置文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/shop_erp?serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root

3.2 mybatis-config.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--
        别名
        日志
        分页插件
     -->
    <settings>
        <setting name="logImpl" value="LOG4J2"/>
    </settings>

    <typeAliases>
        <package name="com.xuetang9.framework.ssmtkmapper.entity"/>
    </typeAliases>

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--使用对应数据库的分页-->
            <property name="helperDialect" value="mysql"/>
            <!--分页的页码为0时的,默认查询全部-->
            <property name="pageSizeZero" value="true"/>
            <!--分页的参数合理化,页码数字<0 默认查询第一页 页码数字>总页数 默认查询最后一页-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
</configuration>  

3.3 log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
        </Root>
        <!-- <logger name="org.springframework.web" level="error" ></logger> -->
    </Loggers>
</Configuration>

3.4 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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--
        1. 导入数据源配置
        2. 配置数据源
        3. Mybatis会话工厂对象
        4. 事务管理器
        5. 事务自动代理配置
        6. 扫描数据操作层,服务层
     -->
    <context:property-placeholder location="classpath:datasource.properties"/>
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    <!-- Mapper扫描器 -->
<!--    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!--        <property name="basePackage" value="com.xuetang9.framework.ssmtkmapper.mapper"/>-->
<!--        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
<!--    </bean>-->
    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xuetang9.framework.ssmtkmapper.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!-- 服务层扫描 -->
    <context:component-scan base-package="com.xuetang9.framework.ssmtkmapper.service"/>
</beans>

3.5 webApplicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--
        1. 映射处理器
        2. 模板解析器
        3. 模板引擎
        4. 视图解析器
        5. 控制层包扫描
        6. 控制层的注解驱动
        7. 默认的servlet支持
        8. 文件处理支持
     -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver" id="templateResolver">
        <property name="prefix" value="/WEB-INF/template/"/>
        <property name="suffix" value=".html"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <bean class="org.thymeleaf.spring5.SpringTemplateEngine" id="templateEngine">
        <property name="templateResolver" ref="templateResolver"/>
    </bean>
    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <context:component-scan base-package="com.xuetang9.framework.ssmtkmapper.controller"/>
    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>

    <bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver" id="multipartResolver"/>
</beans>

4 用web容器加载Spring配置

  在对相关文件配置完成后,需要在Web容器中加载相关的配置,即编辑WEB-INF目录下的web.xml文件。

image.png
  配置内容如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--配置监听器(在项目启动后加载spring的配置文件创建IOC容器)-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-mybatis.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--配置SpringMVC的前端控制器-->
    <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/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>
    </servlet-mapping>

    <!--配置一个字符编码的过滤器-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

相关文章

网友评论

      本文标题:用IDEA创建一个SSM Web项目时常见的项目配置

      本文链接:https://www.haomeiwen.com/subject/wbxemktx.html