QUESTION:SSM:Maven整合ssm时的步骤
目录
QUESTION:SSM:Maven整合ssm时的步骤
ANSWER:
ANSWER:
工程结构:
1.在新建的web工程中,pom.xml文件中导入以下坐标:
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><spring.version>5.1.6.RELEASE</spring.version><slf4j.version>1.6.6</slf4j.version><log4j.version>1.2.12</log4j.version><mysql.version>5.1.6</mysql.version><oracle.version>11.2.0</oracle.version><mybatis.version>3.4.5</mybatis.version><spring.security.version>5.0.1.RELEASE</spring.security.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>compile</scope></dependency><!-- spring --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId> <version>1.6.8</version></dependency><dependency><groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId><version>${spring.version}</version> </dependency><dependency><groupId>org.springframework</groupId> <artifactId>spring-context</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-test</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>mysql</groupId><artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- log start --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.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><!-- log end --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.1</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.1.2</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId><version>${spring.security.version}</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId><version>${spring.security.version}</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-core</artifactId><version>${spring.security.version}</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-taglibs</artifactId><version>${spring.security.version}</version></dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>${oracle.version}</version></dependency></dependencies>
2.建立spring.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 开启注解扫描,管理service和dao --><context:component-scan base-package="com.xy.service"></context:component-scan><context:component-scan base-package="com.xy.dao"></context:component-scan><context:property-placeholder location="classpath:db.properties"/><!--spring整合mybatis--><!-- 配置连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- 把交给IOC管理 SqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /></bean><!-- 扫描dao接口 --><bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.xy.dao"/></bean><!-- 配置Spring的声明式事务管理 --><!-- 配置事务管理器 --><bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><tx:advice transaction-manager="dataSourceTransactionManager" id="txAdvice"><tx:attributes><tx:method name="find*" read-only="true"/><tx:method name="*" isolation="DEFAULT"></tx:method></tx:attributes></tx:advice><!--配置AOP增强--><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xy.service.impl.*.*(..))"></aop:advisor></aop:config>
</beans>
3.建立spring-mvc.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 扫描controller的注解,别的不扫描 --><context:component-scan base-package="com.xy.controller"></context:component-scan><!-- 配置视图解析器 --><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- JSP文件所在的目录 --><property name="prefix" value="/pages/" /><!-- 文件的后缀名 --><property name="suffix" value=".jsp" /></bean><!-- 设置不过滤静态资源--><mvc:resources location="/css/" mapping="/css/**" /><mvc:resources location="/img/" mapping="/img/**" /><mvc:resources location="/js/" mapping="/js/**" /><mvc:resources location="/plugins/" mapping="/plugins/**" /><!-- 开启对SpringMVC注解的支持 --><mvc:annotation-driven /><aop:aspectj-autoproxy proxy-target-class="true"/></beans>
4.建立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"version="3.1"><!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) --><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置初始化参数,创建完DispatcherServlet对象,加载spring-mvc.xml配置文件 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><!-- 服务器启动的时候,让DispatcherServlet对象创建 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><!-- 配置加载类路径的配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:spring.xml,classpath*:spring-security.xml</param-value></context-param><!-- 配置监听器,默认只加载WEB-INF下的spring配置文件--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置监听器,监听request域对象的创建和销毁的 --><listener><listener-class>org.springframework.web.context.request.RequestContextListener</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></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>