• idea创建Maven版的ssm项目


    要使用idea创建一个maven项目,首先电脑安装maven,maven下载地址:http://maven.apache.org/download.cgi

    1、打开idea,选择创建一个新项目,选择maven创建

    选择jdk版本,不需要选择create from archetype,否则你会很可能没有src包或者scr下的main/java等包。点击下一步


     2、输入Nama(项目名称),groupid(项目的包名),artifactid(项目名,与Name一致),Version不用管,然后点击下一步完成创建。


     3、创建完成后,初始的目录结构是这样的,

    此时的项目里面没有web文件夹,我们需要自己去添加。

    点击工具栏的该图标,进入以下页面

    然后选择左侧栏里的Modules,点击添加,选择web,如图:

    完成后在java文件夹里面添加pojo、service等包,结构如下图


    4.在pom.xml中引入ssm需要的相关依赖

    <?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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>om.mysjz</groupId>
        <artifactId>maven-test</artifactId>
        <version>1.0-SNAPSHOT</version>
        <properties>
             <!--设置项目编码编码-->
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
                    <maven.compiler.source>1.7</maven.compiler.source>
                    <maven.compiler.target>1.7</maven.compiler.target>
            <!-- spring版本号 方便在下面的依赖中进行引用-->
            <spring.version>4.3.5.RELEASE</spring.version>
            <!-- mybatis版本号 方便在下面的依赖中进行引用-->
            <mybatis.version>3.4.1</mybatis.version>
        </properties>
        <!-- 配置阿里云仓库,要不然从中央maven库下载很慢-->
            <repositories>
                <repository>
                    <id>nexus-aliyun</id>
                    <name>nexus-aliyun</name>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
            </repositories>
        <!-- 添加项目里面的依赖 -->
        <dependencies>
            <!-- java ee -->
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
            </dependency>
            <!-- log4j日志 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <!-- 单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <!-- 实现slf4j接口并整合 -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.2</version>
            </dependency>
            <!-- JSON -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.7</version>
            </dependency>
            <!-- 数据库 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.15</version>
                <scope>runtime</scope>
            </dependency>
            <!-- 数据库连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.8</version>
            </dependency>
            <!-- MyBatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <!-- mybatis/spring整合包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- aop的配置 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.6.11</version>
            </dependency>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.1_3</version>
            </dependency>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</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-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</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-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</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-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.2.1</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.2</version>
            </dependency>
        </dependencies>
           <build>
               <finalName>springspringmvcmybatis</finalName>
               <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
                   <plugins>
                       <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-compiler-plugin</artifactId>
                           <configuration>
                               <!-- 设置JDK版本 -->
                               <source>13</source>
                               <target>13</target>
                           </configuration>
                       </plugin>
                   </plugins>
               </pluginManagement>
           </build>
    </project>

    5.在resource目录下创建一个mybatisconfig文件,用于配置Mybatis.xml文件,命名为SqlMapConfig.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>
        <!-- 这里面你可以配置一下关于JavaBean中类的一些别名,这样就方便在之后的Mapper文件中进行处理,包括开启下划线转驼峰命名 -->
        <typeAliases>
            <package name="com.mysjz.pojo"/>
        </typeAliases>
    </configuration>

    6.在resource目录下创建一个mapperxmlconfig文件,用于存放mybatis的mapper映射文件

    7.在resource目录下创建一个springconfig的文件,这里存放之后的关于spring的一些配置文件;

    (1)在springconfig文件中,创建applicationContext-dao.xml文件,主要是配置:加载properties文件,数据源,SqlSessionFactory,Mapper扫描。

    <?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"
           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 ">
    
        <!-- 配置 读取properties文件 jdbc.properties -->
        <context:property-placeholder location="classpath:jdbc.properties" />
        <!-- 自动扫描所有的注解 -->
        <context:component-scan base-package="com.mysjz"/>
    
        <!-- 配置阿里的druid数据源 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
    
        <!-- 配置SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 设置数据源 -->
            <property name="dataSource" ref="dataSource"/>
            <!-- 设置MyBatis核心配置文件 -->
            <property name="configLocation" value="classpath:mybatisconfig/SqlMapConfig.xml" />
            <!-- 设置扫描mapper.xml文件 -->
            <property name="mapperLocations" value="classpath:mapperxmlconfig/*Mapper.xml"/>
        </bean>
    
        <!-- 配置Mapper层java类扫描 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 设置Mapper扫描包 -->
            <property name="basePackage" value="com.mysjz.mapper" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
    
    </beans>

    刚开始创建时扫描mapper.xml文件的value可能会报错,是因为mapperxmlconfig.xml中没有以Mapper结尾的xml文件,可以先不用管或者注释掉,当开始写项目的时候再往里面添加。

    (2)在springconfig文件中,创建管理service层的文件,主要是对service包开启扫描。命名为applicationContext-service.xml。(也可以将该扫描步骤添加至dao层的配置文件中)

    <?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: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/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      <!-- 配置Service扫描,不配置也可以,直接在dao层用一个总的扫描类即可 -->
      <context:component-scan base-package="com.mysjz.service"/>
    </beans>

    (3)在springconfig文件中,创建管理transaction层的applicationContext-transaction.xml配置文件,主要用于事务以及切面AOP的管理

    <?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:mvc="http://www.springframework.org/schema/mvc"
           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/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">
      <!--添加spring-dao层配置文件-->
      <import resource="applicationContext-dao.xml"/>
    
      <!-- 事务管理器 -->
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
      </bean>
    
      <!-- 事务模板对象 -->
      <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
        <property name="transactionManager" ref="transactionManager" />
      </bean>
    
      <!-- 通知 -->
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
          <tx:method name="save*"/>
          <tx:method name="add*"/>
          <tx:method name="insert*"/>
          <tx:method name="update*"/>
          <tx:method name="delete*"/>
        </tx:attributes>
      </tx:advice>
      <!-- 切面 -->
      <aop:config>
        <aop:pointcut id="txpc" expression="execution(* com.mysjz.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
      </aop:config>
      <!--
        如果嫌弃上面的配置太麻烦,想在service不同的类进行不同的事务控制,那么就用下面的配置
        但是要注意:需要在service的类中进行手动的添加@Transactional注解,而用上面的话就不需要进行额外的配置了
    
         配置基于注解的声明式事务
        <tx:annotation-driven transaction-manager="transactionManager"/>
        -->
    </beans>

    8.在resource文件目录下,创建数据库的properties文件,这个就是连接数据库对应的内容,因为我使用的mysql是8以上的,如果版本是5左右的,驱动配置应该是

    jdbc.driver=com.mysql.jdbc.Dirver

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
    jdbc.username=用户
    jdbc.password=密码

    9.在resource文件目录下,创建数据库的log4j.properties文件,这个就是为了显示更好的打印日志信息

    # Global logging configuration
    log4j.rootLogger=DEBUG, stdout
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

    10.在resource目录下,创建springmvcconfig文件目录,用于配置springMVC的配置文件,并创建命名为springmvc.xml(主要:Controller扫描、注解驱动、视图解析器)

    <?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: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/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      <!-- 配置Controller扫描 -->
      <context:component-scan base-package="com.mysjz.controller"/>
    
      <!-- 配置注解驱动 -->
      <mvc:annotation-driven />
    
      <!-- 视图解析器 -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!-- 设置属性 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    
      <mvc:default-servlet-handler/>
      <context:annotation-config/>
    <!-- 文件上传配置 -->
      <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600" />
        <property name="maxInMemorySize" value="4096" />
        <property name="defaultEncoding" value="utf-8"/>
    
      </bean>
    </beans>

    11.配置web.xml文件(主要:配置Spring、SpringMVC、解决post乱码问题)

    <?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_3_1.xsd"
             version="3.1">
        <display-name>Archetype Created Web Application</display-name>
    
        <!--配置项目启动的时候加载spring的相关配置文件-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springconfig/applicationContext-*.xml</param-value>
        </context-param>
        <!--配置监听器加载spring-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!--配置过滤器,解决post乱码问题-->
        <filter>
            <filter-name>encoding</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>
        </filter>
        <filter-mapping>
            <filter-name>encoding</filter-name>
            <url-pattern>*</url-pattern>
        </filter-mapping>
        <!--配置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:springmvcconfig/springmvc-servlet.xml</param-value>
            </init-param>
            <!-- 配置springmvc什么时候启动,参数必须为整数 -->
            <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 -->
            <!-- 如果小于0,则在第一次请求进来的时候启动 -->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!-- 默认访问的页面 -->
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

    以上就是使用idea创建一个maven项目的基本配置,可以启动tomcat服务器试试。

    可能会出现以下的某种错误信息

    Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    解决办法,打开一下页面,进行如图操作

    如有其他依赖加载错误有可能是依赖冲突,可以在idea的plugins添加maven helper插件,然后在pom.xml中点击下方Dependency Analyzer查看依赖情况。

    下一篇:maven项目使用逆向工程生成实体类和mapper相关简单配置

  • 相关阅读:
    【RS】RankMBPR:Rank-Aware Mutual Bayesian Personalized Ranking for Item Recommendation
    【RS】Local Collaborative Ranking
    【RS】List-wise learning to rank with matrix factorization for collaborative filtering
    【RS】Using graded implicit feedback for bayesian personalized ranking
    Pycharm 中添加第三方库和插件
    Debug 路漫漫-05
    Debug 路漫漫-04
    推荐系统 BPR 算法求解过程
    【RS】BPR:Bayesian Personalized Ranking from Implicit Feedback
    推荐系统 SVD和SVD++算法
  • 原文地址:https://www.cnblogs.com/lijchah/p/12075715.html
Copyright © 2020-2023  润新知