搭建SSM一个工程的过程
1、创建Maven项目、配置Maven环境(File-setting-Maven);
2、配置pom.xml文件,添加依赖包;
3、Spring和Mybatis整合:
主要有
applicationContent.xml :Spring的相关配置!
Spring-mhbatis.xml : Spring和Mybatis集成配置!
jdbc.properties : 数据库信息配置!
log4j.xml : 日志输出信息配置!(不做介绍,详细信息查看源码)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:resource/*.properties"/> <!-- 数据库连接池 maxActive 最大的存活时间 minIdle 最小连接数 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10"></property> <property name="minIdle" value="5"></property> </bean> <!-- 配置sqlSessionFactory ,关联数据源 value只能配置简单数据类型,ref关联到复杂类型(对象) --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置自动扫描,扫描mapper对象,交给spring代理 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="net.wanho.mapper"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 实际上用的是数据源的事务 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="create*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* net.wanho.service.*.*(..))"/> </aop:config> </beans>
4、整合SpringMVC,添加配置,创建jsp
SpringMVC需要的依赖在pom.xml中已经加上了,现在需在Web项目中的web.xml中添加启动SpringMVC启动配置和Spring整合SpringMVC的配置了。
<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:dubbo="http://code.alibabatech.com/schema/dubbo" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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:property-placeholder location="classpath:resource/*.properties"/> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven/> <!--<context:property-placeholder location="classpath:resource/*.properties"/>--> <context:component-scan base-package="net.wanho.sellercenter.controller" /> <!-- 静态资源映射,tomcat是映射到static目录下--> <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> <mvc:resources location="/ueditor/" mapping="/ueditor/**"/> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> <mvc:resources location="/WEB-INF/img/" mapping="/img/**"/> <!-- 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"></property> </bean> <!-- 多媒体解析器 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">--> <!-- 定义一下编码格式 <property name="defaultEncoding" value="UTF-8"></property>--> <!-- 定义最大上传最大的文件大小,5*1024*1024 <property name="maxUploadSize" value="5242880"></property> </bean>--> <dubbo:application name="wl-sellercenter-web" /> <!-- 提供依赖信息 --> <dubbo:registry protocol="zookeeper" address="192.168.121.133:2181" /> <dubbo:reference interface="net.wanho.service.SellerService" id="sellerServiceImpl"/> </beans>
5、配置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="buyer-center" version="2.5"> <display-name>Archetype Created Web Application</display-name> <!-- 配置一个解决post中文乱码的过滤器 --> <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> <!--配置springmvc--> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring/springmvc.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>