1、创建maven项目
1)New Maven project 页面
勾选 Create a simple project (不使用骨架)
注意选择 maven-archetype-webapp
2)取好Group Id,ArtifactID
GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。
ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。
一般GroupID就是填com.leafive.test这样子。
GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。
ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。
一般GroupID就是填com.leafive.test这样子。
(引用 百度知道答案 https://zhidao.baidu.com/question/511003382.html)
)
<strong>2、修改默认版本</strong>
1)进入项目下的.settings文件夹
打开org.eclipse.wst.common.project.facet.core.xml 文件
.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)修改项目 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 Bloghttp://blog.csdn.net/chuyuqing/article/details/28879477)
4、添加Maven Dependencies
右击项目名 → properties → Deployment Assembly → add → Java Build Path Entries → Maven Dependencies
Apply OK
5、配置文件
1)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>
<!-- 会话超时配置(单位为分钟)-->
<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>/index.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>
2)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>
3)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>
4)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
5)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 Bloghttp://blog.csdn.net/yijiemamin/article/details/51156189 )