ssm基础配置
1.导包
1 <dependencies> 2 3 <dependency> 4 <groupId>org.springframework</groupId> 5 <artifactId>spring-webmvc</artifactId> 6 <version>3.2.8.RELEASE</version> 7 </dependency> 8 9 <dependency> 10 <groupId>org.springframework</groupId> 11 <artifactId>spring-tx</artifactId> 12 <version>3.2.8.RELEASE</version> 13 </dependency> 14 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-web</artifactId> 18 <version>3.2.8.RELEASE</version> 19 </dependency> 20 21 <dependency> 22 <groupId>org.codehaus.jackson</groupId> 23 <artifactId>jackson-mapper-asl</artifactId> 24 <version>1.9.13</version> 25 </dependency> 26 27 <dependency> 28 <groupId>com.fasterxml.jackson.core</groupId> 29 <artifactId>jackson-annotations</artifactId> 30 <version>2.9.7</version> 31 </dependency> 32 33 <dependency> 34 <groupId>com.fasterxml.jackson.core</groupId> 35 <artifactId>jackson-core</artifactId> 36 <version>2.9.7</version> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-jdbc</artifactId> 42 <version>3.2.8.RELEASE</version> 43 </dependency> 44 45 <dependency> 46 <groupId>commons-dbcp</groupId> 47 <artifactId>commons-dbcp</artifactId> 48 <version>1.4</version> 49 </dependency> 50 51 <dependency> 52 <groupId>mysql</groupId> 53 <artifactId>mysql-connector-java</artifactId> 54 <version>5.0.8</version> 55 </dependency> 56 57 <dependency> 58 <groupId>org.mybatis</groupId> 59 <artifactId>mybatis</artifactId> 60 <version>3.3.0</version> 61 </dependency> 62 63 <dependency> 64 <groupId>org.mybatis</groupId> 65 <artifactId>mybatis-spring</artifactId> 66 <version>1.2.3</version> 67 </dependency> 68 69 <dependency> 70 <groupId>junit</groupId> 71 <artifactId>junit</artifactId> 72 <version>4.12</version> 73 </dependency> 74 75 </dependencies>
2.spring配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 9 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 10 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 12 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 13 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 14 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 15 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 16 17 <!-- 1.启用注解驱动 --> 18 <mvc:annotation-driven conversion-service="conversionService" /> 19 20 <!-- 2.配置组件扫描 --> 21 <context:component-scan base-package="com.goatherd.ssm" /> 22 23 <!-- 3.视图解析 --> 24 <bean 25 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 26 <property name="prefix" value="/WEB-INF/" /> 27 <property name="suffix" value=".jsp" /> 28 </bean> 29 30 <!-- 4.session拦截器配置(多个拦截器以配置的顺序为主) --> 31 <mvc:interceptors> 32 <mvc:interceptor> 33 <mvc:mapping path="/**"/> 34 <mvc:exclude-mapping path="/toLogin.do"/> 35 <mvc:exclude-mapping path="/login.do"/> 36 <!-- 拦截器的实现类 --> 37 <bean class="com.session.SessionInterceptor"></bean> 38 </mvc:interceptor> 39 </mvc:interceptors> 40 41 <!-- 自定义参数绑定,不知道为啥这里还需要配置 multipartResolver bean --> 42 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean> 43 </beans>
3.mybatis配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 9 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 10 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 12 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 13 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 14 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 15 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 16 17 <!-- 引入数据库配置文件(读取properties文件),这种方式可以引入多个properties文件 --> 18 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 19 <property name="locations"> 20 <list> 21 <value>classpath:config/jdbc.properties</value> 22 <!--<value>classpath:config/redis.properties</value> --> 23 </list> 24 </property> 25 </bean> 26 27 <!-- 数据库配置 --> 28 <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> 29 <!-- 初级配置 --> 30 <property name="driverClassName" value="${mysql.driverClassName}"/> 31 <property name="url" value="${mysql.url}"/> 32 <property name="username" value="${mysql.username}"/> 33 <property name="password" value="${mysql.password}"/> 34 </bean> 35 36 <!-- session工厂(映射器) --> 37 <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> 38 <!-- 注入DataSource --> 39 <property name="dataSource" ref="dbcp"></property> 40 <!-- 注入SQL语句(映射文件的位置) --> 41 <property name="mapperLocations" value="classpath:mapper/*.xml"></property> 42 </bean> 43 44 <!-- 扫描器 --> 45 <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 46 <!-- 指定mapper接口 --> 47 <property name="basePackage" value="com.ssm.dao"></property> 48 <!-- 指定sqlSession资源 --> 49 <property name="sqlSessionFactoryBeanName" value="ssf" /> 50 </bean> 51 </beans>
4.jdbc配置文件
1 #MySQL数据库 2 mysql.driverClassName=com.mysql.jdbc.Driver 3 mysql.url=jdbc:mysql://localhost/erp?useUnicode=true&characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true 4 mysql.username=root 5 mysql.password=123456
5.web.xml文件配置
1 <!-- 初始配置 --> 2 <context-param> 3 <param-name>contextConfigLocation</param-name> 4 <param-value>classpath:config/xml/spring-*.xml</param-value> 5 </context-param> 6 <listener> 7 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 8 </listener> 9 10 <!-- 配置DispatcherServlet --> 11 <servlet> 12 <servlet-name>mvc</servlet-name> 13 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 14 <init-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:config/xml/spring-*.xml</param-value> 17 </init-param> 18 <load-on-startup>1</load-on-startup> 19 </servlet> 20 <servlet-mapping> 21 <servlet-name>mvc</servlet-name> 22 <url-pattern>*.do</url-pattern> 23 </servlet-mapping>
6.项目结构图