生活随笔
收集整理的這篇文章主要介紹了
IDEA整合 ssm的详细demo(使用maven)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
把我idea的模板拿出來,希望能夠幫到大家初學者,從myeclipse遷移到idea是以把心酸淚,我把以前的demo拿出來。
創建maven項目,切記別選錯
到這項的時候,如果創建maven項目不全的話,一定要加上最后的archetypeCatalog=internal.
這樣項目就創建好,下面添加tomcat,配置的話就不多說了。記住將項目添加進去,還有路徑配置(這個配置資料比較多)
添加tomcat
緊接著先創建對應文件夾,使其目錄大致與相似一致(大文件夾一致)創建完文件夾要聲明作用。
創建完文件夾聲明各個文件夾的屬性以及用途(和myeclipse有點不一樣,因為myeclipse幫你自動配置好了,idea需要自己配置一下)。
結果這樣就行:
接著開始配置maven環境。里面包含幾乎初學者的所有jar包。ssm的初始配置應該是夠的,我剛開始找maven完整版也找了很久,自己也湊了一點。我的maven配置為:(項目名改成自己的)
<?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>food
</groupId><artifactId>food
</artifactId><version>1.0-SNAPSHOT
</version><packaging>war
</packaging><name>food Maven Webapp
</name><url>http://www.example.com
</url><properties><javaee.version>7.0
</javaee.version><jstl.version>1.2
</jstl.version><mysql.version>5.1.46
</mysql.version><dbcp.version>1.4
</dbcp.version><mybatis-spring.version>1.3.0
</mybatis-spring.version><spring.version>5.1.5.RELEASE
</spring.version><mybatis.version>3.4.0
</mybatis.version><jackson.verson>2.9.8
</jackson.verson><junit-version>4.12
</junit-version><cglib-version>3.2.2
</cglib-version><slf4j.version>1.7.21
</slf4j.version><log4j.version>1.2.17
</log4j.version></properties><dependencies><dependency><groupId>javax
</groupId><artifactId>javaee-api
</artifactId><version>${javaee.version}
</version></dependency><dependency><groupId>jstl
</groupId><artifactId>jstl
</artifactId><version>${jstl.version}
</version></dependency><dependency><groupId>mysql
</groupId><artifactId>mysql-connector-java
</artifactId><version>${mysql.version}
</version></dependency><dependency><groupId>commons-dbcp
</groupId><artifactId>commons-dbcp
</artifactId><version>${dbcp.version}
</version></dependency><dependency><groupId>org.mybatis
</groupId><artifactId>mybatis-spring
</artifactId><version>${mybatis-spring.version}
</version></dependency><dependency><groupId>org.mybatis.generator
</groupId><artifactId>mybatis-generator-core
</artifactId><version>1.3.7
</version></dependency><dependency><groupId>org.mybatis
</groupId><artifactId>mybatis
</artifactId><version>${mybatis.version}
</version></dependency><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-aop
</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-test
</artifactId><version>${spring.version}
</version></dependency><dependency><groupId>com.fasterxml.jackson.core
</groupId><artifactId>jackson-databind
</artifactId><version>${jackson.verson}
</version></dependency><dependency><groupId>com.fasterxml.jackson.core
</groupId><artifactId>jackson-core
</artifactId><version>${jackson.verson}
</version></dependency><dependency><groupId>com.fasterxml.jackson.core
</groupId><artifactId>jackson-annotations
</artifactId><version>${jackson.verson}
</version></dependency><dependency><groupId>junit
</groupId><artifactId>junit
</artifactId><version>${junit-version}
</version><scope>test
</scope></dependency><dependency><groupId>cglib
</groupId><artifactId>cglib
</artifactId><version>${cglib-version}
</version></dependency><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.aspectj
</groupId><artifactId>aspectjweaver
</artifactId><version>1.8.10
</version></dependency><dependency><groupId>aspectj
</groupId><artifactId>aspectjrt
</artifactId><version>1.5.3
</version></dependency></dependencies><build><finalName>food
</finalName><pluginManagement><plugins><plugin><artifactId>maven-clean-plugin
</artifactId><version>3.0.0
</version></plugin><plugin><artifactId>maven-resources-plugin
</artifactId><version>3.0.2
</version></plugin><plugin><artifactId>maven-compiler-plugin
</artifactId><version>3.7.0
</version></plugin><plugin><artifactId>maven-surefire-plugin
</artifactId><version>2.20.1
</version></plugin><plugin><artifactId>maven-war-plugin
</artifactId><version>3.2.0
</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>
緊接著web.xml配置,我第一個過濾器可以刪除掉,里面一些名稱根據自己項目名修改,但是對應關系要一致
<filter><filter-name>cors2
</filter-name>
<filter-class> com.date.fitter.filter
</filter-class></filter><filter-mapping><filter-name> cors2
</filter-name><url-pattern>/*
</url-pattern></filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class></listener><context-param><param-name>contextConfigLocation
</param-name><param-value>classpath*:applicationContext.xml
</param-value></context-param><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*:food-servlet.xml
</param-value></init-param><load-on-startup>1
</load-on-startup><multipart-config><max-file-size>20971520
</max-file-size> <max-request-size>20971520
</max-request-size> <file-size-threshold>0
</file-size-threshold> </multipart-config></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>
接著在resource目錄下的xml配置如下:
- application.xml spring核心配置
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost/food?useSSL=false"/><property name="username" value="root"/><property name="password" value="123456"/></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:mybatis-conf.xml"/><property name="mapperLocations" value="classpath:mapping/*.xml"></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.food.dao"></property><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><tx:annotation-driven transaction-manager="txManager"/></beans>
- food-servlet.xml配置 springmvc的配置,名稱具體參考自己的,包括映射jsp等等
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><mvc:annotation-driven/><mvc:default-servlet-handler/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean><context:component-scan base-package="com.food.controller"/>
</beans>
- mybatis-conf.xml配置: mybatis一些額外配置,但是我用的不太多
<?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></configuration>
- 還剩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
上面就是基本的配置,下面我把我簡單的結合的demo展示出來控制層controller,控制器是springmvc的主要部分,里面主要關于url請求地址的配置,以及處理。和servlet很像。它是網站和服務器通信的樞紐。
controller:
dao層叫數據訪問層。其實就是聲明一個接口,這個接口有很多方法集合,關于數據的增刪該查,返回的類型可以為自定義map,list,或者bean對象等等。
dao層為:
mapper,mapper就是dao的方法具體實現,在xml中寫sql,這是mybatis部分。在myeclipse中我的dao和mapper是放到一塊的,但是idea的maven編譯方式和myeclipse不同,如果寫在一塊需要配置,可以直接在resources中新建mapping文件夾放到里面。這樣就能夠被編譯
bean層,至于bean層就不多說了,就是對象,要和數據庫對象的字段一一對應。如果不一致則需要自定義返回類型在其中配置。
另外,還有一些遇到過的小問題,比如沒有找打監聽器報錯,
還有就是一些jar包版本可能會沖突,比如我之前用的mysql就出錯了。后來換了版本。注意要在dependece中先將jar包移除,然后maven clean jar包,不然可能會出錯。還有就是剛開始配置可能有的xml或者class沒找到,可以在target文件下查看classes那些東西已經編譯
剩下的如果以后遇到會補充,上面有些文件名可能寫blog時候會打錯,各位具體名稱還請對照自己的名稱。另外,如果有錯誤還請大神指出。
項目完整github地址(有些變化)
ssm
如果對后端、爬蟲、數據結構算法等感性趣歡迎關注我的個人公眾號交流:bigsai
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的IDEA整合 ssm的详细demo(使用maven)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。