ssm分为web层(控制层)、业务层(service)、dao(数据库交互层)
大体架构如下
这是配置文件
web.xml配置如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 7 <!--监听器--> 8 <listener> 9 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10 </listener> 11 <context-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:applicationContext*.xml</param-value> 14 </context-param> 15 <!--springmvc前端控制器--> 16 <servlet> 17 <servlet-name>dispatcherServlet</servlet-name> 18 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 19 <init-param> 20 <param-name>contextConfigLocation</param-name> 21 <param-value>classpath:springmvc.xml</param-value> 22 </init-param> 23 </servlet> 24 <servlet-mapping> 25 <servlet-name>dispatcherServlet</servlet-name> 26 <url-pattern>/</url-pattern> 27 </servlet-mapping> 28 <!--post乱码解决--> 29 <filter> 30 <filter-name>encodingFilter</filter-name> 31 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 32 <init-param> 33 <param-name>encoding</param-name> 34 <param-value>utf-8</param-value> 35 </init-param> 36 </filter> 37 <filter-mapping> 38 <filter-name>encodingFilter</filter-name> 39 <url-pattern>/*</url-pattern> 40 </filter-mapping> 41 42 </web-app>
springmvc.xml配置如下
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:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 6 <!--注解驱动--> 7 <context:component-scan base-package="com.ujy.web"></context:component-scan> 8 <!--注解驱动--> 9 <mvc:annotation-driven conversion-service="conversionService2"></mvc:annotation-driven> 10 <!--配置内部资源视图解析器--> 11 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 12 <property name="suffix" value=".jsp"/> 13 <property name="prefix" value="/"/> 14 </bean> 15 <!--静态资源映射器--> 16 <mvc:default-servlet-handler/> 17 <!--文件上传解析器--> 18 <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 19 <property name="maxUploadSize" value="100000000"></property> 20 </bean> 21 <!--配置类型转换器 然后在mvc:annotation-driven 中配置conversion-service --> 22 <!-- 点进去源码 converters 是set类型 然后复杂属性赋值--> 23 <!--格式转换器--> 24 <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean " id="conversionService2"> 25 <property name="converters"> 26 <set> 27 <bean class="com.ujy.converter.DateTimeConverter"></bean> 28 </set> 29 </property> 30 </bean> 31 <!--异常处理器--> 32 <!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">--> 33 <!-- <property name="exceptionAttribute" value="ex"></property>--> 34 <!-- <property name="exceptionMappings">--> 35 <!-- <props>--> 36 <!-- <!–key 指定异常类的全限定名,指出了异常去哪个页面–>--> 37 <!-- <prop key="com.ujy.customexception.PayException">error</prop>--> 38 <!-- </props>--> 39 <!-- </property>--> 40 <!-- </bean>--> 41 <!--拦截器--> 42 <mvc:interceptors> 43 <mvc:interceptor> 44 <mvc:mapping path="/**"/> 45 <bean class="com.ujy.Interceptor.Myinterceptor"/> 46 </mvc:interceptor> 47 </mvc:interceptors> 48 </beans>
log4j.properties 配置文件如下
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
db.properties配置如下
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=ych521mm
applicationContext-tx.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
applicationContext-service.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.ujy.service"/> </beans>
applicationContext-dao.xml配置如下
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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> 6 <!--引入外部资源文件文件--> 7 <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> 8 <!--包扫描--> 9 <context:component-scan base-package="com.ujy.service"></context:component-scan> 10 <!--database--> 11 <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"> 12 <property name="driverClassName" value="${jdbc.driverClass}"/> 13 <property name="url" value="${jdbc.url}"/> 14 <property name="username" value="${jdbc.username}"/> 15 <property name="password" value="${jdbc.password}"/> 16 <property name="initialSize" value="10"/> 17 <property name="maxActive" value="3000"/> 18 <property name="minIdle" value="10"/> 19 </bean> 20 <!--mybatis--> 21 <bean class="org.mybatis.spring.SqlSessionFactoryBean"> 22 <property name="dataSource" ref="dataSource"/> 23 <property name="typeAliasesPackage" value="com.ujy.pojo"/> 24 <property name="plugins"> 25 <array> 26 <bean class="com.github.pagehelper.PageInterceptor"> 27 <property name="properties"> 28 <value> 29 helperDialect=mysql 30 reasonable=true 31 supportMethodsArguments=true 32 autoRuntimeDialect=true 33 </value> 34 </property> 35 </bean> 36 </array> 37 </property> 38 </bean> 39 <!--Mybatis映射扫描--> 40 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 41 <property name="basePackage" value="com.ujy.dao"/> 42 </bean> 43 44 45 </beans>
pom.xml包如下
1 <properties> 2 <spring.version>5.2.1.RELEASE</spring.version> 3 </properties> 4 6 <dependency> 7 <groupId>junit</groupId> 8 <artifactId>junit</artifactId> 9 <version>4.11</version> 10 <scope>test</scope> 11 </dependency> 12 <!--servlet--> 13 <dependency> 14 <groupId>javax.servlet</groupId> 15 <artifactId>servlet-api</artifactId> 16 <version>2.5</version> 17 <scope>provided</scope> 18 </dependency> 19 <dependency> 20 <groupId>javax.servlet.jsp</groupId> 21 <artifactId>jsp-api</artifactId> 22 <version>2.2</version> 23 <scope>provided</scope> 24 </dependency> 25 <dependency> 26 <groupId>jstl</groupId> 27 <artifactId>jstl</artifactId> 28 <version>1.2</version> 29 </dependency> 30 <!--spring-ioc的包--> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-beans</artifactId> 34 <version>${spring.version}</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-core</artifactId> 39 <version>${spring.version}</version> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-context</artifactId> 44 <version>${spring.version}</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-expression</artifactId> 49 <version>${spring.version}</version> 50 </dependency> 51 <!--spring-aop的包--> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-aop</artifactId> 55 <version>${spring.version}</version> 56 </dependency> 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-aspects</artifactId> 60 <version>${spring.version}</version> 61 </dependency> 62 <dependency> 63 <groupId>org.aspectj</groupId> 64 <artifactId>aspectjweaver</artifactId> 65 <version>1.9.4</version> 66 </dependency> 67 <!--spring-tx的包--> 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-tx</artifactId> 71 <version>${spring.version}</version> 72 </dependency> 73 <!--spring-jdbc的包--> 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-jdbc</artifactId> 77 <version>${spring.version}</version> 78 </dependency> 79 <!--springmvc--> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-web</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 <dependency> 86 <groupId>org.springframework</groupId> 87 <artifactId>spring-webmvc</artifactId> 88 <version>${spring.version}</version> 89 </dependency> 90 <dependency> 91 <groupId>commons-fileupload</groupId> 92 <artifactId>commons-fileupload</artifactId> 93 <version>1.4</version> 94 </dependency> 95 <dependency> 96 <groupId>com.fasterxml.jackson.core</groupId> 97 <artifactId>jackson-databind</artifactId> 98 <version>2.9.8</version> 99 </dependency> 100 <!--mybatis包--> 101 <dependency> 102 <groupId>org.mybatis</groupId> 103 <artifactId>mybatis</artifactId> 104 <version>3.5.1</version> 105 </dependency> 106 <dependency> 107 <groupId>org.mybatis</groupId> 108 <artifactId>mybatis-spring</artifactId> 109 <version>2.0.2</version> 110 </dependency> 111 <dependency> 112 <groupId>com.github.pagehelper</groupId> 113 <artifactId>pagehelper</artifactId> 114 <version>5.1.10</version> 115 </dependency> 116 <!--数据库驱动--> 117 <dependency> 118 <groupId>mysql</groupId> 119 <artifactId>mysql-connector-java</artifactId> 120 <version>5.1.32</version> 121 </dependency> 122 123 <!--连接池--> 124 <dependency> 125 <groupId>com.alibaba</groupId> 126 <artifactId>druid</artifactId> 127 <version>1.1.16</version> 128 </dependency> 129 130 <!--日志信息--> 131 <dependency> 132 <groupId>log4j</groupId> 133 <artifactId>log4j</artifactId> 134 <version>1.2.17</version> 135 </dependency> 136 <dependency> 137 <groupId>org.slf4j</groupId> 138 <artifactId>slf4j-api</artifactId> 139 <version>1.7.26</version> 140 </dependency> 141 <dependency> 142 <groupId>org.slf4j</groupId> 143 <artifactId>slf4j-log4j12</artifactId> 144 <version>1.7.26</version> 145 </dependency>
build插件
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <target>1.8</target> <source>1.8</source> </configuration> </plugin><plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8888</port> <path>/</path> <uriEncoding>utf-8</uriEncoding> </configuration> </plugin> </plugins> </build>
原文:https://www.cnblogs.com/ych961107/p/11889241.html