要使用idea建立一個maven項目,首先電腦安裝maven,maven下載地址:http://maven.apache.org/download.cgihtml
一、打開idea,選擇建立一個新項目,選擇maven建立java
選擇jdk版本,不須要選擇create from archetype,不然你會極可能沒有src包或者scr下的main/java等包。點擊下一步mysql
二、輸入Nama(項目名稱),groupid(項目的包名),artifactid(項目名,與Name一致),Version不用管,而後點擊下一步完成建立。web
三、建立完成後,初始的目錄結構是這樣的,spring
此時的項目裏面沒有web文件夾,咱們須要本身去添加。sql
點擊工具欄的該圖標,進入如下頁面數據庫
而後選擇左側欄裏的Modules,點擊添加,選擇web,如圖:express
完成後在java文件夾裏面添加pojo、service等包,結構以下圖apache
4.在pom.xml中引入ssm須要的相關依賴api
<?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查看依賴狀況。