1、創建maven項目 版本:Java 1.8 Mysql 6.0 勾選 Create a simple project (不使用骨架) 註意選擇 maven-archetype-webapp 2)New Maven project 頁面 GroupID 是項目組織唯一的標識符,實際對應java的包 ...
1、創建maven項目
版本:Java 1.8 Mysql 6.0
1)New Maven project 頁面
勾選 Create a simple project (不使用骨架)
註意選擇 maven-archetype-webapp
2)New Maven project 頁面
GroupID 是項目組織唯一的標識符,實際對應java的包的結構,是main目錄里的java的目錄結構。
ArtifactID 是項目的唯一標識符,實際對應項目的名稱,是項目根目錄的名稱。
一般GroupID 就是填com.leafive.test這樣子。
(引用 百度知道答案 https://zhidao.baidu.com/question/511003382.html)
2、修改預設版本
1)修改.settings文件
進入項目文件夾,進入 .settings 文件夾
打開 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="3.1"/> <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>
2)右擊項目名,properties → Java Compiler → 調整Java版本1.8
3)修改項目 web.xml 的文件頭
內容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Archetype Created Web Application</display-name> </web-app>
(引用 CSDN Blog http://blog.csdn.NET/qq_23212697/article/details/52444262 )
3、更改項目結構
在 webapp 目錄下創建 META-INF 和 WEB-INF 目錄,在 WEB-INF下創建 web.xml 文件
(引用 CSDN Blog http://blog.csdn.net/chuyuqing/article/details/28879477)
4、添加Maven Dependencies
右擊項目名 → properties → Deployment Assembly → add → Java Build Path Entries → Maven Dependencies
Apply OK
5、配置文件
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ly</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>demo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.12</junit.version> <spring.version>4.3.10.RELEASE</spring.version> <mybatis.version>3.4.4</mybatis.version> <mybatis.spring.version>1.3.1</mybatis.spring.version> <commons-dbcp.version>1.4</commons-dbcp.version> <jstl.version>1.2</jstl.version> <log4j.version>1.2.17</log4j.version> <fastjson.version>1.2.35</fastjson.version> <slf4j.version>1.7.25</slf4j.version> <jackson.version>1.9.13</jackson.version> <commons-fileupload.version>1.3.3</commons-fileupload.version> <commons-io.version>2.5</commons-io.version> <commons-codec.version>1.10</commons-codec.version> <aspectjweaver.version>1.8.10</aspectjweaver.version> </properties> <dependencies> <!-- Junit測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- spring包 core、web、oxm、tx、jdbc、webmvc、aop、context、test --> <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-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- 導入Mysql資料庫鏈接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> <!-- 導入dbcp的jar包,用來在applicationContext.xml中配置資料庫 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${commons-dbcp.version}</version> </dependency> <!-- JSTL標簽類 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- 日誌文件管理包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- 格式化對象,方便輸出日誌 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.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> <!-- json和bean之間相互轉換 --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>${jackson.version}</version> </dependency> <!-- 上傳組件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons-codec.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0-b07</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
2)web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <context-param> <!--全局範圍內環境參數初始化--> <param-name>contextConfigLocation</param-name> <!--參數名稱--> <param-value>classpath:spring-mybatis.xml</param-value> <!--參數取值--> </context-param> <!--以下配置的載入順序:先 ServletContext >> context-param >> listener >> filter >> servlet >> spring--> <!--過濾器配置--> <!--例:編碼過濾器--> <filter> <!-- 用來聲明filter的相關設定,過濾器可以截取和修改一個Servlet或JSP頁面的請求或從一個Servlet或JSP頁面發出的響應--> <filter-name>encodingFilter</filter-name> <!--指定filter的名字--> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--定義filter的類的名稱--> <async-supported>true</async-supported> <!--設置是否啟用非同步支持--> <init-param><!--用來定義參數,若在Servlet可以使用下列方法來獲得:String param_name=getServletContext().getInitParamter("param-name裡面的值");--> <param-name>encoding</param-name> <!--參數名稱--> <param-value>UTF-8</param-value> <!--參數值--> </init-param> </filter> <filter-mapping><!--用來定義filter所對應的URL--> <filter-name>encodingFilter</filter-name> <!--指定對應filter的名字--> <url-pattern>/*</url-pattern> <!--指定filter所對應的URL--> </filter-mapping> <!--監聽器配置--> <!--例:spring監聽器--> <listener> <!--用來設定Listener介面--> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!--定義Listener的類名稱--> </listener> <!-- 防止Spring記憶體溢出監聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!--servlet配置--> <servlet> <!--用來聲明一個servlet的數據 --> <servlet-name>SpringMVC</servlet-name> <!--指定servlet的名稱--> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--指定servlet的類名稱,這裡配置了前端控制器--> <init-param><!--用來定義參數,可有多個init-param。在servlet類中通過getInitParamenter(String name)方法訪問初始化參數 --> <param-name>contextConfigLocation</param-name> <!--參數名稱--> <param-value>classpath:spring-mvc.xml</param-value> <!--參數值--> </init-param> <load-on-startup>1</load-on-startup><!--當值為正數或零時:Servlet容器先載入數值小的servlet,再依次載入其他數值大的servlet.--> <async-supported>true</async-supported><!--設置是否啟用非同步支持--> </servlet> <servlet-mapping><!--用來定義servlet所對應的URL--> <servlet-name>SpringMVC</servlet-name> <!--指定servlet的名稱--> <url-pattern>/</url-pattern> <!--指定servlet所對應的URL--> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!--會話超時配置(單位為分鐘)--> <session-config><!--如果某個會話在一定時間未被訪問,則伺服器可以扔掉以節約記憶體--> <session-timeout>120</session-timeout> </session-config> <!--MIME類型配置 --> <mime-mapping><!--設定某種擴展名的文件用一種應用程式來打開的方式類型,當該擴展名文件被訪問的時候,瀏覽器會自動使用指定應用程式來打開--> <extension>*.ppt</extension> <!--擴展名名稱--> <mime-type>application/mspowerpoint</mime-type> <!--MIME格式--> </mime-mapping> <!--歡迎頁面配置 --> <welcome-file-list><!--定義首頁列單.--> <welcome-file>/pages/login/login.jsp</welcome-file> <!--用來指定首頁文件名稱.我們可以用<welcome-file>指定幾個首頁,而伺服器會依照設定的順序來找首頁.--> <welcome-file>/index.html</welcome-file> </welcome-file-list> <!--配置錯誤頁面--> <!-- <error-page> 將錯誤代碼(Error Code)或異常(Exception)的種類對應到web應用資源路徑. --> <!-- <error-code>404</error-code> HTTP Error code,例如: 404、403 --> <!-- <location>/error.html</location> 用來設置發生錯誤或異常時要顯示的頁面 --> <!-- </error-page> --> <!-- <error-page> --> <!-- <exception-type>java.lang.Exception</exception-type>設置可能會發生的java異常類型,例如:java.lang.Exception --> <!-- <location>ExceptionError.html</location> 用來設置發生錯誤或異常時要顯示的頁面 --> <!-- </error-page> --> </web-app>
3)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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 1、配置映射器與適配器 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 2、視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 3、自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 --> <context:component-scan base-package="com.ly.controller"/> </beans>
4)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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!--1 自動掃描 將標註Spring註解的類自動轉化Bean--> <context:component-scan base-package="com.ly" /> <!--2 載入數據資源屬性文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!-- <span style="white-space:pre">3 配置數據源</span> --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 初始化連接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連接池最大數量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連接池最大空閑 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連接池最小空閑 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- 4 配置sessionfactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapper.xml文件 --> <property name="mapperLocations" value="classpath:com/ly/mappers/*.xml"></property> </bean> <!-- 5 裝配dao介面 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ly.dao" /> <!-- DAO介面所在包名,Spring會自動查找其下的類 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 6、聲明式事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 7、註解事務切麵 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
5)jdbc.properties
# database configure #jdbc.driver=com.mysql.jdbc.Driver jdbc.driver=com.mysql.cj.jdbc.Driver #jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&autoReconnect=true jdbc.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC jdbc.username=root jdbc.password=123 #定義初始連接數 initialSize=0 #定義最大連接數 maxActive=20 #定義最大空閑 maxIdle=20 #定義最小空閑 minIdle=1 #定義最長等待時間 maxWait=60000
6)log4j.properties
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!--1 自動掃描 將標註Spring註解的類自動轉化Bean--> <context:component-scan base-package="com.ly" /> <!--2 載入數據資源屬性文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!-- <span style="white-space:pre">3 配置數據源</span> --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 初始化連接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連接池最大數量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連接池最大空閑 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連接池最小空閑 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- 4 配置sessionfactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapper.xml文件 --> <property name="mapperLocations" value="classpath:com/ly/mappers/*.xml"></property> </bean> <!-- 5 裝配dao介面 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ly.dao" /> <!-- DAO介面所在包名,Spring會自動查找其下的類 --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 6、聲明式事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 7、註解事務切麵 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
(引用 CSDN Blog http://blog.csdn.net/yijiemamin/article/details/51156189 )