SSM妗嗘灦鏁村悎(IntelliJ IDEA+Maven+SSM)
Posted Java鍚庣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM妗嗘灦鏁村悎(IntelliJ IDEA+Maven+SSM)相关的知识,希望对你有一定的参考价值。
鍘嗗彶鍒嗕韩锛?/span>
馃憠
馃憠
馃憠
馃憠
馃憠 浜?/span>
馃憠
馃憠
鏈瘒鏂囩珷涓昏鍐呭鏄粙缁嶅浣曚娇鐢↖ntelliJ IDEA鍒涘缓Spring + SpringMVC + MyBatis椤圭洰锛屼笅闈細缁欏嚭椤圭洰鎼缓鐨勮缁嗘楠や互鍙婄浉鍏崇殑閰嶇疆鏂囦欢銆?/span>
1. 鍒涘缓maven椤圭洰
1.1 File -> New Module锛岃繘鍏ュ垱寤洪」鐩獥鍙c€?/span>
1.2 鐐瑰嚮Next锛屽~鍐橤roupId銆丄rtifactId鍜孷ersion
1.3 鎺ョ潃涓嬩竴姝ワ紝杩欓噷闇€瑕佹敞鍦≒roperties涓坊鍔犱竴涓弬鏁?nbsp;archetypeCatalog=internal锛屼笉鍔犺繖涓弬鏁帮紝鍦╩aven鐢熸垚楠ㄦ灦鐨勬椂鍊欏皢浼氶潪甯告參锛屾湁鏃跺€欑洿鎺ュ崱浣忋€傛潵鑷綉涓婄殑瑙i噴锛歛rchetypeCatalog琛ㄧず鎻掍欢浣跨敤鐨刟rchetype鍏冩暟鎹紝涓嶅姞杩欎釜鍙傛暟鏃堕粯璁や负remote锛宭ocal锛屽嵆涓ぎ浠撳簱archetype鍏冩暟鎹紝鐢变簬涓ぎ浠撳簱鐨刟rchetype澶浜嗘墍浠ュ鑷村緢鎱紝鎸囧畾internal鏉ヨ〃绀轰粎浣跨敤鍐呴儴鍏冩暟鎹€?/span>
1.4 濉啓Module name
1.5 鐢熸垚maven鐨勯」鐩鏋朵箣鍚庯紝鎴戜滑杩橀渶瑕佹墜鍔ㄥ湪 src/main 涓嬪垱寤?java鐩綍銆傜幇鍦ㄥ彲浠ョ洿鎺ョ紪鍐欎簡锛屾垜鎶婇」鐩墍闇€瑕佺殑鏂囦欢閮界紪鍐欏畬鎴愪箣鍚庯紝椤圭洰鐨勫伐绋嬬粨鏋勫鍥俱€?/span>
鍒版涓烘锛岄」鐩殑妗嗘灦鍩烘湰鎼缓瀹屾垚锛屼笅闈㈤噸鐐硅涓€涓嬮」鐩殑閰嶇疆鏂囦欢
2. 椤圭洰閰嶇疆鏂囦欢
2.1 杩欓噷浣跨敤maven鏉ュ紩鍏ラ」鐩墍闇€瑕佺殑jar鍖咃紝鎵€浠ヤ篃灏变笉闇€瑕佹墜鍔ㄦ潵绠$悊jar鍖呬簡銆?/span>
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.heitian.web</groupId>
<artifactId>web-ssm</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>web-ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
<!-- spring鐗堟湰鍙?-->
<spring.version>4.2.5.RELEASE</spring.version>
<!-- mybatis鐗堟湰鍙?-->
<mybatis.version>3.2.8</mybatis.version>
<!-- mysql椹卞姩鐗堟湰鍙?-->
<mysql-driver.version>5.1.29</mysql-driver.version>
<!-- log4j鏃ュ織鍖呯増鏈彿 -->
<slf4j.version>1.7.18</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- 娣诲姞jstl渚濊禆 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 娣诲姞junit4渚濊禆 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 鎸囧畾鑼冨洿锛屽湪娴嬭瘯鏃舵墠浼氬姞杞?-->
<scope>test</scope>
</dependency>
<!-- 娣诲姞spring鏍稿績渚濊禆 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-oxm</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-jdbc</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-context</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</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.2.2</version>
</dependency>
<!-- 娣诲姞mysql椹卞姩渚濊禆 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-driver.version}</version>
</dependency>
<!-- 娣诲姞鏁版嵁搴撹繛鎺ユ睜渚濊禆 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 娣诲姞fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<!-- 娣诲姞鏃ュ織鐩稿叧jar鍖?-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 鏄犲叆JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<finalName>web-ssm</finalName>
</build>
</project>
2.2 鏂板缓jdbc.properties鏂囦欢
jdbc.properties
driverClasss=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/db_ssm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=root
password=root
#瀹氫箟鍒濆杩炴帴鏁?/span>
initialSize=0
#瀹氫箟鏈€澶ц繛鎺ユ暟
maxActive=20
#瀹氫箟鏈€澶х┖闂?/span>
maxIdle=20
#瀹氫箟鏈€灏忕┖闂?/span>
minIdle=1
#瀹氫箟鏈€闀跨瓑寰呮椂闂?/span>
maxWait=60000
2.3 鏂板缓log4j 閰嶇疆鏂囦欢
log4j.properties
log4j.rootLogger=INFO,Console,File
#鎺у埗鍙版棩蹇?/span>
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=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
#鏅€氭枃浠舵棩蹇?/span>
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB
#杈撳嚭鏃ュ織锛屽鏋滄崲鎴怐EBUG琛ㄧず杈撳嚭DEBUG浠ヤ笂绾у埆鏃ュ織
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
2.4 鏂板缓springmvc閰嶇疆鏂囦欢
spring-mvc.xml
<!-- 鑷姩鎵弿 @Controller-->
<context:component-scan base-package="com.heitian.ssm.controller"/>
<!--閬垮厤IE鎵цAJAX鏃讹紝杩斿洖JSON鍑虹幇涓嬭浇鏂囦欢 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 鍚姩SpringMVC鐨勬敞瑙e姛鑳斤紝瀹屾垚璇锋眰鍜屾敞瑙OJO鐨勬槧灏?-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON杞崲鍣?-->
</list>
</property>
</bean>
<!-- 瀹氫箟璺宠浆鐨勬枃浠剁殑鍓嶅悗缂€ 锛岃鍥炬ā寮忛厤缃?-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
<!-- 鏂囦欢涓婁紶閰嶇疆 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 榛樿缂栫爜 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 涓婁紶鏂囦欢澶у皬闄愬埗涓?1M锛?1*1024*1024 -->
<property name="maxUploadSize" value="32505856"/>
<!-- 鍐呭瓨涓殑鏈€澶у€?-->
<property name="maxInMemorySize" value="4096"/>
</bean>
2.5 鏂板缓spring鍜宮ybatis鏁村悎鎵€闇€鐨勯厤缃枃浠?/span>
spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 鑷姩鎵弿 -->
<context:component-scan base-package="com.heitian.ssm"/>
<!-- 绗竴绉嶆柟寮忥細鍔犺浇涓€涓猵roperties鏂囦欢 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!-- 绗簩绉嶆柟寮忥細鍔犺浇澶氫釜properties鏂囦欢
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:common.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"/>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"/>
</bean>
-->
<!-- 閰嶇疆鏁版嵁婧?-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClasss}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 鍒濆鍖栬繛鎺ュぇ灏?-->
<property name="initialSize" value="${initialSize}"></property>
<!-- 杩炴帴姹犳渶澶ф暟閲?-->
<property name="maxActive" value="${maxActive}"></property>
<!-- 杩炴帴姹犳渶澶х┖闂?-->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 杩炴帴姹犳渶灏忕┖闂?-->
<property name="minIdle" value="${minIdle}"></property>
<!-- 鑾峰彇杩炴帴鏈€澶х瓑寰呮椂闂?-->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- mybatis鍜宻pring瀹岀編鏁村悎锛屼笉闇€瑕乵ybatis鐨勯厤缃槧灏勬枃浠?-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 鑷姩鎵弿mapping.xml鏂囦欢 -->
<property name="mapperLocations" value="classpath:mapping/*.xml"></property>
</bean>
<!-- DAO鎺ュ彛鎵€鍦ㄥ寘鍚嶏紝Spring浼氳嚜鍔ㄦ煡鎵惧叾涓嬬殑绫?-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.heitian.ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (浜嬪姟绠$悊)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- (浜嬪姟绠$悊)transaction manager, use JtaTransactionManager for global tx -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
2.6 web.xml閰嶇疆鏂囦欢
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>web-ssm</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 缂栫爜杩囨护鍣?-->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring鐩戝惉鍣?-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 闃叉spring鍐呭瓨婧㈠嚭鐩戝惉鍣紝姣斿quartz -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- spring mvc servlet-->
<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-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 姝ゅ涔熷彲浠ラ厤缃垚 *.do 褰㈠紡 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- session閰嶇疆 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
3. 椤圭洰閰嶇疆鍜岄儴缃?/span>
3.1 File -> Project Structure锛岃繘鍏ュ垱寤洪」鐩厤缃獥鍙c€?/span>
3.2 鍒涘缓涓€涓猅omcat瀹瑰櫒瀹炰緥锛屽苟鎶婇」鐩儴缃茶繘鍘?/span>
3.3 椤圭洰鎵€闇€閰嶇疆濂介」鐩闂殑鏍硅矾寰勶紝鐒跺悗鍚姩Tomcat銆?/span>
3.5 椤圭洰鎵€闇€鐪嬪埌鍥句腑鏄剧ず鏁堟灉锛屽垯琛ㄧず椤圭洰鎼缓鎴愬姛銆?/span>
4. 鍊煎緱娉ㄦ剰鐨勫湴鏂?/span>
鎼缓濂介」鐩殑妗嗘灦涔嬪悗锛屽惎鍔═omcat锛屽鏋滆闂?http://localhost:8080/user/showUser 鍑虹幇濡備笅閿欒锛?/span>
鍑虹幇杩欎釜閿?span class="mq-607">璇槸鍥犱负灏戜簡渚濊禆鐨刯ar鍖咃紝鍙鍦╬om鏂囦欢涓坊鍔犲涓嬩緷璧栧嵆鍙€?/span>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
浣?nbsp; 鑰咃細 GallenZhang
鈥斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€?/span>
以上是关于SSM妗嗘灦鏁村悎(IntelliJ IDEA+Maven+SSM)的主要内容,如果未能解决你的问题,请参考以下文章