maven的介绍
-
什么是maven
maven是基于项目对象模型(pom),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。 -
创建maven工程,该工程属于web类型的
-
在src下导入web.xml文件,需要创建一个WEB-INF文件夹,放入web.xml
-
-
在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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
-
在pom.xml中的Dependencies导入相应的包:
-
在Dependecy Hierarchy则会显示这样:
-
解决jre报错的,在pom.xml中加入以下的代码:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
在resource下创建一个spring.xml的文件:
在文件插入以下的代码:`
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 启用自动扫描注解 -->
<context:component-scan
base-package="com.offcn.ssh"></context:component-scan>
<!-- 配置连接池 -->
<bean class="org.apache.commons.dbcp.BasicDataSource"
id="dataSource">
<property name="url" value="jdbc:mysql:///newsytemhib"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- 注入SessionFactory,实现着spring和hibernate的整合 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 配置hibernate属性,方言,show_sql,format_sql -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 关联*.hbm.xml -->
<property name="mappingLocations"
value="classpath:com/offcn/ssh/pojo/*.hbm.xml"></property>
</bean>
</beans>
之后分别创建action,dao,dao的实现层,pojo类,service,service的实现层