日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IDEA整合 ssm的详细demo(使用maven)

發(fā)布時間:2025/3/20 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IDEA整合 ssm的详细demo(使用maven) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

把我idea的模板拿出來,希望能夠幫到大家初學者,從myeclipse遷移到idea是以把心酸淚,我把以前的demo拿出來。

  • 創(chuàng)建maven項目,切記別選錯
  • 到這項的時候,如果創(chuàng)建maven項目不全的話,一定要加上最后的archetypeCatalog=internal.
  • 這樣項目就創(chuàng)建好,下面添加tomcat,配置的話就不多說了。記住將項目添加進去,還有路徑配置(這個配置資料比較多)

    添加tomcat
  • 緊接著先創(chuàng)建對應(yīng)文件夾,使其目錄大致與相似一致(大文件夾一致)創(chuàng)建完文件夾要聲明作用。
  • 創(chuàng)建完文件夾聲明各個文件夾的屬性以及用途(和myeclipse有點不一樣,因為myeclipse幫你自動配置好了,idea需要自己配置一下)。

    結(jié)果這樣就行:
  • 接著開始配置maven環(huán)境。里面包含幾乎初學者的所有jar包。ssm的初始配置應(yīng)該是夠的,我剛開始找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><!-- FIXME change it to the project's website --><url>http://www.example.com</url><!--<properties>--><!--<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>--><!--<maven.compiler.source>1.7</maven.compiler.source>--><!--<maven.compiler.target>1.7</maven.compiler.target>--><!--</properties>--><!--<dependencies>--><!--<dependency>--><!--<groupId>junit</groupId>--><!--<artifactId>junit</artifactId>--><!--<version>4.11</version>--><!--<scope>test</scope>--><!--</dependency>--><!--</dependencies>--><properties><!-- java ee jar包 --><javaee.version>7.0</javaee.version><!-- jstl jar包 --><jstl.version>1.2</jstl.version><!-- mysql-connector包 --><mysql.version>5.1.46</mysql.version><!-- commons-dbcp --><dbcp.version>1.4</dbcp.version><!-- mybatis-spring整合包 --><mybatis-spring.version>1.3.0</mybatis-spring.version><!-- spring版本號 --><spring.version>5.1.5.RELEASE</spring.version><!-- mybatis版本號 --><mybatis.version>3.4.0</mybatis.version><!-- jackson版本號 --><jackson.verson>2.9.8</jackson.verson><!-- junit --><junit-version>4.12</junit-version><!-- cglib --><cglib-version>3.2.2</cglib-version><!-- log4j日志文件管理包版本 --><slf4j.version>1.7.21</slf4j.version><log4j.version>1.2.17</log4j.version></properties><dependencies><!-- 導入java ee jar 包 --><dependency><groupId>javax</groupId><artifactId>javaee-api</artifactId><version>${javaee.version}</version></dependency><!-- jstl包 --><!-- JSTL標簽類 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><!-- 導入Mysql數(shù)據(jù)庫鏈接jar包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- dbcp數(shù)據(jù)庫連接池包 --><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>${dbcp.version}</version></dependency><!--mybatis-spring整合包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis-spring.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.7</version></dependency><!-- mybatis-核心包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></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-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-context-supportmvn </artifactId>--><!--<version>${spring.version}</version>--><!--</dependency>--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><!-- jackson包 --><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><!-- cglib包 --><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>${cglib-version}</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><!-- aspectjweaver包 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.10</version></dependency><!-- aspectjrt 包--><dependency><groupId>aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.5.3</version></dependency><!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --><!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --><!--<dependency>--><!--<groupId>org.mybatis.generator</groupId>--><!--<artifactId>mybatis-generator-core</artifactId>--><!--<version>1.3.6</version>--><!--</dependency>--></dependencies><build><finalName>food</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.0.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><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配置,我第一個過濾器可以刪除掉,里面一些名稱根據(jù)自己項目名修改,但是對應(yīng)關(guān)系要一致
  • <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><!-- <location>/</location> --><max-file-size>20971520</max-file-size> <!--單個文件最大大小:5MB--><max-request-size>20971520</max-request-size> <!--所有文件最大大小:20MB--><file-size-threshold>0</file-size-threshold> <!-- 超過這個大小直接存硬盤,而不是內(nèi)存 --></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"><!-- 1. 數(shù)據(jù)源 : DriverManagerDataSource --><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><!--2. mybatis的SqlSession的工廠: SqlSessionFactoryBean dataSource:引用數(shù)據(jù)源MyBatis定義數(shù)據(jù)源,同意加載配置--><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><!--3. mybatis自動掃描加載Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactorybasePackage:指定sql映射文件/接口所在的包(自動掃描)--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.food.dao"></property><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean><!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> --><!-- 注入sqlSessionFactory --><!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --><!-- 給出需要掃描Dao接口包 --><!-- <property name="basePackage" value="com.ssm.dao" /> --><!-- </bean> --><!--4. 事務(wù)管理 : DataSourceTransactionManager dataSource:引用上面定義的數(shù)據(jù)源--><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 5. 使用聲明式事務(wù)transaction-manager:引用上面定義的事務(wù)管理器--><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"><!-- 配置SpringMVC --><!-- 1.開啟SpringMVC注解模式 --><!-- 簡化配置: (1)自動注冊DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter (2)提供一些列:數(shù)據(jù)綁定,數(shù)字和日期的format @NumberFormat, @DateTimeFormat, xml,json默認讀寫支持 --><mvc:annotation-driven/><!-- 2.靜態(tài)資源默認servlet配置(1)加入對靜態(tài)資源的處理:js,gif,png(2)允許使用"/"做整體映射--><mvc:default-servlet-handler/><!-- 3.配置jsp 顯示ViewResolver --><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><!-- 4.掃描web相關(guān)的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><!-- 實體類,簡稱 -設(shè)置別名 --><!--<typeAliases>--><!--<typeAlias alias="sheng" type="com.date.bean.sheng" />--><!--</typeAliases>--><!-- 實體接口映射資源 --><!--說明:如果xxMapper.xml配置文件放在和xxMapper.java統(tǒng)一目錄下,mappers也可以省略,因為org.mybatis.spring.mapper.MapperFactoryBean默認會去查找與xxMapper.java相同目錄和名稱的xxMapper.xml--><!-- <mappers> --><!-- <mapper resource="com/ssm/dao/citylistMapper.xml" /> --><!-- <mapper resource="com/ssm/dao/foodMapper.xml" /> --><!-- </mappers> --></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

    上面就是基本的配置,下面我把我簡單的結(jié)合的demo展示出來控制層controller,控制器是springmvc的主要部分,里面主要關(guān)于url請求地址的配置,以及處理。和servlet很像。它是網(wǎng)站和服務(wù)器通信的樞紐。

  • controller:
  • dao層叫數(shù)據(jù)訪問層。其實就是聲明一個接口,這個接口有很多方法集合,關(guān)于數(shù)據(jù)的增刪該查,返回的類型可以為自定義map,list,或者bean對象等等。
    dao層為:
  • mapper,mapper就是dao的方法具體實現(xiàn),在xml中寫sql,這是mybatis部分。在myeclipse中我的dao和mapper是放到一塊的,但是idea的maven編譯方式和myeclipse不同,如果寫在一塊需要配置,可以直接在resources中新建mapping文件夾放到里面。這樣就能夠被編譯
  • bean層,至于bean層就不多說了,就是對象,要和數(shù)據(jù)庫對象的字段一一對應(yīng)。如果不一致則需要自定義返回類型在其中配置。
    另外,還有一些遇到過的小問題,比如沒有找打監(jiān)聽器報錯,

    還有就是一些jar包版本可能會沖突,比如我之前用的mysql就出錯了。后來換了版本。注意要在dependece中先將jar包移除,然后maven clean jar包,不然可能會出錯。還有就是剛開始配置可能有的xml或者class沒找到,可以在target文件下查看classes那些東西已經(jīng)編譯

    剩下的如果以后遇到會補充,上面有些文件名可能寫blog時候會打錯,各位具體名稱還請對照自己的名稱。另外,如果有錯誤還請大神指出。
  • 項目完整github地址(有些變化)
    ssm

    如果對后端、爬蟲、數(shù)據(jù)結(jié)構(gòu)算法等感性趣歡迎關(guān)注我的個人公眾號交流:bigsai

    《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

    總結(jié)

    以上是生活随笔為你收集整理的IDEA整合 ssm的详细demo(使用maven)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。