您好,登錄后才能下訂單哦!
SSM項目怎么利用IEAD+Maven進行搭建?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
一、新建項目:
1.打開IDEA,新建一個Maven項目
2.點擊next,新建一個為ssmDemo的Maven項目(自己定義項目GroupId和ArtifactId)
3.配置本地Maven路徑
4.點擊Next,確認項目信息,配置項目名稱和項目路徑,點擊Finish按鈕即可創建項目
5.項目創建完成,目錄結構如下:
注:如項目目錄中未包含src,則項目還在初始化,注意網絡連接,稍等片刻即可
二、補充項目結構:
1.在main文件夾下面創建java和resources文件夾
2.使java文件夾為項目資源文件夾(Sources root)
3.使resources文件夾為Resources root
4.在src/java目錄下面創建com包,在com包創建controller、dao、po、service、service.impl包
<?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>com.demo</groupId> <artifactId>ssmDemo02</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ssmDemo02 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版本管理--> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!-- spring版本管理 當對spring進行版本控制后, maven導入的jar包就不需要關心版本的問題, 因為會自動匹配符合當前版本控制版本的jar包 ,導入方式如下:${spring.version}--> <spring.version>5.0.2.RELEASE</spring.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <shiro.version>1.2.3</shiro.version> <!-- mysql連接驅動版本號 --> <mysql.version>5.1.6</mysql.version> <!-- mybatis版本號 --> <mybatis.version>3.4.5</mybatis.version> <spring.security.version>5.0.1.RELEASE</spring.security.version> </properties> <dependencies> <!-- Mybatis 和 mybatis 與 spring 的整合 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</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-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-webmvc</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-test</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-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--junit測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!--jstl--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- log --> <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> <!--alibaba連接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> </dependencies> <build> <finalName>ssmDemo02</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.1.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.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</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>
效果圖:
注1:代碼復制完成后,選擇右下角的Import Changes
注2:如Import Changes后External還未出現jar包,則選中項目,右鍵,選擇maven,Generate Sources...
1.選擇項目右鍵 -- open Module Setting
2.選擇Artifact
3.選擇項目名,選擇outPut LayOut,選擇Available Elements -- 選擇項目名稱 -- 右鍵 -- put into...
效果圖(WEB-INF中添加了lib目錄):
1.選擇右側側欄的maven
2.選擇Lifecycle -- 選擇clean(雙擊)
3.選擇Lifecycle -- 選擇install(雙擊)
4.效果圖
1.添加tomcat(run --> editConfigurations --> 點擊+ --> 選擇tomcat-- local)
2.配置Deployment(點擊Deployment -- 選擇右下角+ -- 選擇Artifact --選擇Artifa)
3.配置訪問路徑:/ssmDemotest
4.啟動tomcat,訪問:http://localhost:8080/ssmDemotest
1.在src/resources文件夾下面添加mybatis包、spring包
2.在src/resources文件夾下面添加log4j.properties、db.properties
3.在mybatis包下添加:mybatis-config.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> <!-- 別名 --> <typeAliases> <package name="com.po" /> </typeAliases> </configuration>
4.在spring包下添加:spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 1.配置component-scan 掃描service,mapper包 --> <!-- 如不寫,則在控制層的@Resource注解則指定不到對應的service類 --> <context:component-scan base-package="com.service" /> <context:component-scan base-package="com.dao" /> <!-- 2.配置 讀取properties文件 jdbc.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 3.配置 數據源 --> <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> <!-- 4.配置SqlSessionFactory對象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 4.1.注入數據庫連接池 --> <property name="dataSource" ref="dataSource" /> <!-- 4.2.配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" /> <!-- 4.3.掃描entity包 使用別名 --> <property name="typeAliasesPackage" value="com.po" /> <!-- 4.4.掃描sql配置文件:mapper需要的xml文件 <property name="mapperLocations" value="classpath:com/dao/*.xml" /> --> </bean> <!-- 5.配置Mapper掃描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 5.1.注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- 5.2.配置Mapper掃描 --> <property name="basePackage" value="com.dao" /> </bean> </beans>
5.在spring包下添加:spring-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置Service掃描 --> <context:component-scan base-package="com.service" /> </beans>
6.在spring包下添加:spring-mvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 1.配置Controller掃描 --> <context:component-scan base-package="com.controller" /> <!-- 2.配置注解驅動 --> <mvc:annotation-driven /> <!-- 3.配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 4.1.前綴 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 4.2.后綴 --> <property name="suffix" value=".jsp" /> </bean> </beans>
7. 完善web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Archetype Created Web Application</display-name> <!-- 1.加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-dao.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 2.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:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 3.設置匹配所有的url --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> </web-app>
1.如啟動日志沒有錯誤信息,則xml添加無誤
2.如網址:http://localhost:8080/ssmDemotest 正常展示,則xml添加無誤
注:由于大部分代碼均為復制,可以重啟IDEA
1.在controller包新建類:TestController
2.在類頂部(導包語句下面)添加注解:@Controller,@RequestMapping(value = "test")
3.編寫方法:
@RequestMapping("dologin") public String dologin() { System.out.println("====test===="); return "index"; }
1.在web-inf下創建目錄:jsp
2.在jsp文件下創建:index.jsp
3.在index.jsp下添加:hello,ssm
http://localhost:8080/ssmDemotest/test/dologin
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。