1)Windows 10 企業版html
2)JDK 1.8.0_131java
3)Eclipse Java EE IDE for Web Developers Version: Neon.3 Release (4.6.3)mysql
4)Tomcat 8.5web
maven的下載地址:http://maven.apache.org/download.cgispring
下載後解壓放在d:\Java路徑下 sql
在環境變量中,新增系統變量名:MAVEN_HOME,變量值:D:\Java\maven數據庫
在Path中追加:%MAVEN_HOME%\bin;apache
在命令行窗口中輸入:mvn –v,若是看見下圖則說明maven安裝配置完畢api
maven倉庫默認位置在系統用戶目錄下的.m2目錄中,相似下圖spring-mvc
由於衆所周知的緣由,直接訪問maven公共倉庫的速度比較慢,因此推薦使用阿里的maven倉庫鏡像。編輯setting.xml文件,在mirrors節點下,新增以下內容,這樣從倉庫中下載jar包速度上會快不少。
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
設置eclipse自帶maven整合工具,在Preferences中找到Maven節點,觀察User Settings項的設置是否正確
點擊Installations節點,添加maven runtime
問題描述:eclipse建立maven項目時,顯示的JDK默認版本爲1.5,實際使用的JDK爲1.8,如何修改?
解決方案:找到本機maven倉庫存放位置,好比:${user.home}/.m2/路徑,編輯settings.xml文件,在profiles節點下配置
<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>
1)選擇Maven Project
2)點擊Next,選擇默認工做空間位置
3)選擇web類型
4)填寫GroupID、ArtifactID
Group ID:至關於一個組織
Artifact ID:至關於這個組織下的一個具體項目
Packege:根據Group ID和Artifact ID生成一個默認的名稱
5)建立出的maven項目以下圖
6)問題描述:提示錯誤:
在eclipse中設置Server爲Tomcat,注意JRE設置爲安裝的JDK的jre
在工程上右鍵,查看工程屬性,找到Java Build Path,添加Server Runtime爲Tomcat
點擊Finish後,項目工程變爲下圖
7)在項目上右鍵,查看項目信息
默認的Dynamic Web Module爲2.3,使用Tomcat 8.5,須要修改成3.1
修改方法:
① maven工程所在目錄下org.eclipse.wst.common.project.facet.core.xml
編輯內容,以下所示
<?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節點,添加以下內容
<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>
④ 修改後,在項目上右鍵,找到Maven屬性下的Update Project,點擊之
⑤ 選擇該項目進行更新,若是怕不能強制更新,能夠勾選Force Update of Snapshots/Releases
⑥ 點擊OK後更新maven項目,再觀察項目屬性,Module已經變爲3.1
修改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>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版本號 --> <spring.version>4.3.8.RELEASE</spring.version> </properties> <dependencies> <!-- Spring相關包 --> <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相關包 --> <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相關包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!-- MySQL相關包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <!-- 數據庫鏈接池 --> <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標準標籤庫 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 日誌相關包 --> <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> <!-- 單元測試相關包 --> <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>
保存後,能夠看到相關jar包被下載至本地倉庫
由於服務端maven項目的標準結構有四個子包:src/main/java、src/main/resources、src/test/java、src/test/resources,這裏缺乏了src/test/resources,因此手動補上。
在項目中新建Source Folder
建立src/test/resources目錄後,工程以下圖所示
右鍵查看項目屬性,點擊Deployment Assembly,移除test和target
移除後以下圖
建立log4j.properties文件,內容以下:
#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
建立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="cn.temptation.domain" /> </typeAliases> </configuration>
建立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: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"> <!-- 啓動自動掃描 --> <context:component-scan base-package="cn.temptation.*" /> <!-- 註冊MVC註解驅動 --> <mvc:annotation-driven /> <!-- 靜態資源可訪問的設置方式 --> <mvc:default-servlet-handler /> <!-- 配置視圖解析器,能夠顯式設置,也能夠不設置,不設置會依據SpringMVC的默認設置 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
建立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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 定義數據源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> <!-- 註冊SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mappers.xml文件 --> <property name="mapperLocations" value="classpath:cn/temptation/dao/*.xml" /> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.temptation.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> </beans>
建立spring-tx.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: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" /> <!-- 事務管理器,依賴於數據源 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 編寫通知:對事務進行加強(通知),須要編寫對切入點和具體執行事務細節 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 爲切入點方法添加事務詳情 name:方法名,*表示任意方法名稱 propagation:設置傳播行爲 isolation:設置隔離級別 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> <!-- 設置AOP,讓Spring自動對目標生成代理,須要使用AspectJ表達式 --> <aop:config proxy-target-class="true"> <!-- 切面:整合切入點和通知 --> <aop:advisor advice-ref="txAdvice" pointcut="within(cn.temptation.web..*)" /> </aop:config> </beans>
配置web.xml,個人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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/spring-mybatis.xml,
classpath*:/spring-tx.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener </listener-class>
</listener>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
</web-app>
編寫User實體類,代碼以下:
package cn.temptation.domain; /** * 用戶信息 */ public class User { // 成員變量 private Integer userid; private String username; private String password; // 構造函數 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數據訪問層接口,代碼以下:
package cn.temptation.dao; import cn.temptation.domain.User; public interface UserDao { public abstract User findByUsername(String username); }
編寫UserMapper.xml數據訪問層映射文件,代碼以下:
<?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>
編寫login.jsp登陸頁,代碼以下:
<%@ 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>帳號:</label> <input type="text" id="txtUsername" name="username" placeholder="請輸入帳號" /><br/> <label>密碼:</label> <input type="password" id="txtPassword" name="password" placeholder="請輸入密碼" /><br/> <input type="submit" value="提交" /> <input type="reset" value="重置" /> </form> </body> </html>
編寫index.jsp主頁,代碼以下:
<%@ 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> <h3>歡迎,${user.username }</h3> </body> </html>
在項目上右鍵,找到Maven屬性的Update Project,也能夠Alt+F5操做
修改項目編譯路徑爲JRE系統類庫,不然後續操做出錯
項目上右鍵,找到Run As屬性,找到Maven install
點擊執行,結果以下說明構建成功
項目運行仍是使用web工程的部署運行方式
運行項目,觀察是否報錯
登陸頁面
登陸成功,跳轉到主頁