您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何使用eclipse+maven一步步搭建SSM框架,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
SSM (SSM 框架集)
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三個(gè)開源框架整合而成,常作為數(shù)據(jù)源較簡(jiǎn)單的web項(xiàng)目的框架。
其中spring是一個(gè)輕量級(jí)的控制反轉(zhuǎn)(IoC)和面向切面(AOP)的容器框架。
SpringMVC分離了控制器、模型對(duì)象、分派器以及處理程序?qū)ο蟮慕巧?,這種分離讓它們更容易進(jìn)行定制。
MyBatis是一個(gè)支持普通SQL查詢,存儲(chǔ)過程和高級(jí)映射的優(yōu)秀持久層框架。
0、系統(tǒng)環(huán)境
1)Windows 10 企業(yè)版
2)JDK 1.8.0_131
3)Eclipse Java EE IDE for Web Developers Version: Neon.3 Release (4.6.3)
4)Tomcat 8.5
1、maven下載及配置
maven的下載地址:http://maven.apache.org/download.cgi
下載后解壓放在d:\Java路徑下
在環(huán)境變量中,新增系統(tǒng)變量名:MAVEN_HOME,變量值:D:\Java\maven
在Path中追加:%MAVEN_HOME%\bin;
在命令行窗口中輸入:mvn –v,如果看見下圖則說明maven安裝配置完畢
2、
2.maven倉(cāng)庫(kù)的設(shè)置
maven倉(cāng)庫(kù)默認(rèn)位置在系統(tǒng)用戶目錄下的.m2目錄中,類似下圖
因?yàn)楸娝苤脑?,直接訪問maven公共倉(cāng)庫(kù)的速度比較慢,所以推薦使用阿里的maven倉(cāng)庫(kù)鏡像。編輯setting.xml文件,在mirrors節(jié)點(diǎn)下,新增如下內(nèi)容,這樣從倉(cāng)庫(kù)中下載jar包速度上會(huì)快很多。
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
3、eclipse整合maven
設(shè)置eclipse自帶maven整合工具,在Preferences中找到Maven節(jié)點(diǎn),觀察User Settings項(xiàng)的設(shè)置是否正確
點(diǎn)擊Installations節(jié)點(diǎn),添加maven runtime
4、創(chuàng)建maven項(xiàng)目時(shí)設(shè)置JDK
問題描述:eclipse創(chuàng)建maven項(xiàng)目時(shí),顯示的JDK默認(rèn)版本為1.5,實(shí)際使用的JDK為1.8,如何修改?
解決方案:找到本機(jī)maven倉(cāng)庫(kù)存放位置,比如:${user.home}/.m2/路徑,編輯settings.xml文件,在profiles節(jié)點(diǎn)下配置
<profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
5、使用maven創(chuàng)建SSM項(xiàng)目
1)選擇Maven Project
2)點(diǎn)擊Next,選擇默認(rèn)工作空間位置
3)選擇web類型
4)填寫GroupID、ArtifactID
Group ID:相當(dāng)于一個(gè)組織
Artifact ID:相當(dāng)于這個(gè)組織下的一個(gè)具體項(xiàng)目
Packege:根據(jù)Group ID和Artifact ID生成一個(gè)默認(rèn)的名稱
5)創(chuàng)建出的maven項(xiàng)目如下圖
6)問題描述:提示錯(cuò)誤:
在eclipse中設(shè)置Server為Tomcat,注意JRE設(shè)置為安裝的JDK的jre
在工程上右鍵,查看工程屬性,找到Java Build Path,添加Server Runtime為Tomcat
點(diǎn)擊Finish后,項(xiàng)目工程變?yōu)橄聢D
7)在項(xiàng)目上右鍵,查看項(xiàng)目信息
默認(rèn)的Dynamic Web Module為2.3,使用Tomcat 8.5,需要修改為3.1
修改方法:
① maven工程所在目錄下org.eclipse.wst.common.project.facet.core.xml
編輯內(nèi)容,如下所示
<?xml version="1.0" encoding="UTF-8"?> <faceted-project> <fixed facet="wst.jsdt.web"/> <installed facet="java" version="1.8"/> <installed facet="jst.web" version="2.3"/> <installed facet="wst.jsdt.web" version="1.0"/> </faceted-project>
改為
<?xml version="1.0" encoding="UTF-8"?><faceted-project> <fixed facet="wst.jsdt.web"/> <installed facet="java" version="1.8"/> <installed facet="jst.web" version="3.1"/> <installed facet="wst.jsdt.web" version="1.0"/> </faceted-project>
② maven工程下的web.xml文件修改為
<?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" metadata-complete="true"> </web-app>
③ pom.xml文件中修改build節(jié)點(diǎn),添加如下內(nèi)容
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins>
④ 修改后,在項(xiàng)目上右鍵,找到Maven屬性下的Update Project,點(diǎn)擊之
⑤ 選擇該項(xiàng)目進(jìn)行更新,如果怕不能強(qiáng)制更新,可以勾選Force Update of Snapshots/Releases
⑥ 點(diǎn)擊OK后更新maven項(xiàng)目,再觀察項(xiàng)目屬性,Module已經(jīng)變?yōu)?.1
6、下載ssm框架所需jar包
修改pom.xml內(nèi)容為:
<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>cn.temptation</groupId> <artifactId>ssm</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssm Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <!-- Spring版本號(hào) --> <spring.version>4.3.8.RELEASE</spring.version> </properties> <dependencies> <!-- Spring相關(guān)包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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-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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- AOP相關(guān)包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.0</version> </dependency> <!-- MyBatis相關(guān)包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!-- MySQL相關(guān)包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <!-- 數(shù)據(jù)庫(kù)連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.20</version> </dependency> <!-- Spring集成MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <!-- JSP標(biāo)準(zhǔn)標(biāo)簽庫(kù) --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 日志相關(guān)包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <!-- 單元測(cè)試相關(guān)包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>ssm</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
保存后,可以看到相關(guān)jar包被下載至本地倉(cāng)庫(kù)
7、完善項(xiàng)目結(jié)構(gòu)
因?yàn)榉?wù)端maven項(xiàng)目的標(biāo)準(zhǔn)結(jié)構(gòu)有四個(gè)子包:src/main/java、src/main/resources、src/test/java、src/test/resources,這里缺少了src/test/resources,所以手動(dòng)補(bǔ)上。
在項(xiàng)目中新建Source Folder
創(chuàng)建src/test/resources目錄后,工程如下圖所示
右鍵查看項(xiàng)目屬性,點(diǎn)擊Deployment Assembly,移除test和target
移除后如下圖
8、創(chuàng)建項(xiàng)目用的配置文件
創(chuàng)建log4j.properties文件,內(nèi)容如下:
#USE THIS SETTING FOR OUTPUT MYBATIS`s SQL ON THE CONSOLE log4j.rootLogger=DEBUG, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
創(chuàng)建mybatis-config.xml文件,內(nèi)容如下:
<?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> <package name="cn.temptation.domain" /> </typeAliases> </configuration>
創(chuàng)建spring-mvc.xml文件,內(nèi)容如下:
<?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/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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 啟動(dòng)自動(dòng)掃描 --> <context:component-scan base-package="cn.temptation.*" /> <!-- 注冊(cè)MVC注解驅(qū)動(dòng) --> <mvc:annotation-driven /> <!-- 靜態(tài)資源可訪問的設(shè)置方式 --> <mvc:default-servlet-handler /> <!-- 配置視圖解析器,可以顯式設(shè)置,也可以不設(shè)置,不設(shè)置會(huì)依據(jù)SpringMVC的默認(rèn)設(shè)置 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
創(chuàng)建spring-mybatis.xml文件,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <!-- 定義數(shù)據(jù)源Bean --> <!-- Druid --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="sa" /> </bean> <!-- 注冊(cè)SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動(dòng)掃描mappers.xml文件 --> <property name="mapperLocations" value="classpath:cn/temptation/dao/*.xml" /> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.temptation.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> </beans>
創(chuàng)建spring-tx.xml文件,內(nèi)容如下:
<?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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 開啟AOP注解掃描 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 事務(wù)管理器,依賴于數(shù)據(jù)源 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 編寫通知:對(duì)事務(wù)進(jìn)行增強(qiáng)(通知),需要編寫對(duì)切入點(diǎn)和具體執(zhí)行事務(wù)細(xì)節(jié) --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 為切入點(diǎn)方法添加事務(wù)詳情 name:方法名,*表示任意方法名稱 propagation:設(shè)置傳播行為 isolation:設(shè)置隔離級(jí)別 read-only:是否只讀 --> <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="Exception" /> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="Exception" /> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="Exception" /> </tx:attributes> </tx:advice> <!-- 設(shè)置AOP,讓Spring自動(dòng)對(duì)目標(biāo)生成代理,需要使用AspectJ表達(dá)式 --> <aop:config proxy-target-class="true"> <!-- 切面:整合切入點(diǎn)和通知 --> <aop:advisor advice-ref="txAdvice" pointcut="within(cn.temptation.web..*)" /> </aop:config> </beans>
9、編寫服務(wù)端代碼
編寫User實(shí)體類,代碼如下:
package cn.temptation.domain; /** * 用戶信息 */ public class User { // 成員變量 private Integer userid; private String username; private String password; // 構(gòu)造函數(shù) public User() { super(); } public User(Integer userid, String username, String password) { super(); this.userid = userid; this.username = username; this.password = password; } // 成員方法 public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
編寫UserController控制器,代碼如下:
package cn.temptation.web; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import cn.temptation.dao.UserDao; import cn.temptation.domain.User; /** * 用戶控制器 */ @Controller @RequestMapping(value = "/user") public class UserController { @Resource private UserDao userDao; @RequestMapping("/view") public String view() { return "main/login"; } @RequestMapping("/indexview") public String index() { return "main/index"; } @RequestMapping(value = "/login", method = RequestMethod.POST) public ModelAndView login(User model, HttpSession session) { User user = userDao.findByUsername(model.getUsername()); if (user == null || !user.getPassword().equals(model.getPassword())) { return new ModelAndView("redirect:/login.jsp"); } else { session.setAttribute("user", user); ModelAndView mav = new ModelAndView(); mav.setViewName("index"); return mav; } } }
編寫UserDao數(shù)據(jù)訪問層接口,代碼如下:
package cn.temptation.dao; import cn.temptation.domain.User; public interface UserDao { public abstract User findByUsername(String username); }
編寫UserMapper.xml數(shù)據(jù)訪問層映射文件,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.temptation.dao.UserDao"> <select id="findByUsername" parameterType="string" resultType="User"> SELECT * FROM userinfo WHERE username=#{username} </select> </mapper>
10、編寫客戶端代碼
編寫login.jsp登錄頁(yè),代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登錄</title> </head> <body> <form action="user/login" method="post"> <label>賬號(hào):</label> <input type="text" id="txtUsername" name="username" placeholder="請(qǐng)輸入賬號(hào)" /><br/> <label>密碼:</label> <input type="password" id="txtPassword" name="password" placeholder="請(qǐng)輸入密碼" /><br/> <input type="submit" value="提交" /> <input type="reset" value="重置" /> </form> </body> </html>
編寫index.jsp主頁(yè),代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>主頁(yè)</title> </head> <body> <h4>歡迎,${user.username }</h4> </body> </html>
11、使用maven構(gòu)建項(xiàng)目
在項(xiàng)目上右鍵,找到Maven屬性的Update Project,也可以Alt+F5操作
修改項(xiàng)目編譯路徑為JRE系統(tǒng)類庫(kù),否則后續(xù)操作出錯(cuò)
項(xiàng)目上右鍵,找到Run As屬性,找到Maven install
點(diǎn)擊執(zhí)行,結(jié)果如下說明構(gòu)建成功
項(xiàng)目運(yùn)行還是使用web工程的部署運(yùn)行方式
運(yùn)行項(xiàng)目,觀察是否報(bào)錯(cuò)
登錄頁(yè)面
登錄成功,跳轉(zhuǎn)到主頁(yè)
關(guān)于“如何使用eclipse+maven一步步搭建SSM框架”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。