ssm框架的整合搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm框架的整合搭建相关的知识,希望对你有一定的参考价值。

maven简单项目的创建前面已经完成了,下面开始依赖包和配置文件的编写

好好努力每一天!!!!!!

项目结构看这里

1.首先,依赖包pom.xml

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.test</groupId>
  5     <artifactId>ssm-crud</artifactId>
  6     <version>0.0.1-SNAPSHOT</version>
  7     <packaging>war</packaging>
  8     <dependencies>
  9         <!-- 引入pageHelper分页插件 -->
 10         <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
 11         <dependency>
 12             <groupId>com.github.pagehelper</groupId>
 13             <artifactId>pagehelper</artifactId>
 14             <version>5.1.2</version>
 15         </dependency>
 16 
 17         <!-- 可手动生成实体类,xml文件,mapper接口类 -->
 18         <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
 19         <dependency>
 20             <groupId>org.mybatis.generator</groupId>
 21             <artifactId>mybatis-generator-core</artifactId>
 22             <version>1.3.6</version>
 23         </dependency>
 24 
 25         <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
 26         <dependency>
 27             <groupId>org.springframework</groupId>
 28             <artifactId>spring-webmvc</artifactId>
 29             <version>5.0.2.RELEASE</version>
 30         </dependency>
 31 
 32         <!-- 返回json字符串的支持 -->
 33         <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
 34         <dependency>
 35             <groupId>com.fasterxml.jackson.core</groupId>
 36             <artifactId>jackson-databind</artifactId>
 37             <version>2.9.3</version>
 38         </dependency>
 39 
 40         <!--JSR303数据校验支持;tomcat7及以上的服务器, tomcat7以下的服务器:el表达式。额外给服务器的lib包中替换新的标准的el -->
 41         <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
 42         <dependency>
 43             <groupId>org.hibernate</groupId>
 44             <artifactId>hibernate-validator</artifactId>
 45             <version>5.4.1.Final</version>
 46         </dependency>
 47 
 48 
 49         <!-- Spring-Jdbc -->
 50         <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-jdbc</artifactId>
 54             <version>4.3.7.RELEASE</version>
 55         </dependency>
 56 
 57         <!--Spring-test -->
 58         <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
 59         <dependency>
 60             <groupId>org.springframework</groupId>
 61             <artifactId>spring-test</artifactId>
 62             <version>4.3.7.RELEASE</version>
 63         </dependency>
 64 
 65 
 66         <!-- Spring面向切面编程 -->
 67         <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
 68         <dependency>
 69             <groupId>org.springframework</groupId>
 70             <artifactId>spring-aspects</artifactId>
 71             <version>4.3.7.RELEASE</version>
 72         </dependency>
 73 
 74         <!--MyBatis -->
 75         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
 76         <dependency>
 77             <groupId>org.mybatis</groupId>
 78             <artifactId>mybatis</artifactId>
 79             <version>3.4.2</version>
 80         </dependency>
 81         <!-- MyBatis整合Spring的适配包 -->
 82         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
 83         <dependency>
 84             <groupId>org.mybatis</groupId>
 85             <artifactId>mybatis-spring</artifactId>
 86             <version>1.3.1</version>
 87         </dependency>
 88 
 89         <!-- 数据库连接池、驱动 -->
 90         <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
 91         <dependency>
 92             <groupId>c3p0</groupId>
 93             <artifactId>c3p0</artifactId>
 94             <version>0.9.1</version>
 95         </dependency>
 96         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
 97         <dependency>
 98             <groupId>mysql</groupId>
 99             <artifactId>mysql-connector-java</artifactId>
100             <version>5.1.41</version>
101         </dependency>
102         <!-- (jstl,servlet-api,junit) -->
103         <!-- https://mvnrepository.com/artifact/jstl/jstl -->
104         <dependency>
105             <groupId>jstl</groupId>
106             <artifactId>jstl</artifactId>
107             <version>1.2</version>
108         </dependency>
109 
110         <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
111         <dependency>
112             <groupId>javax.servlet</groupId>
113             <artifactId>javax.servlet-api</artifactId>
114             <version>3.0.1</version>
115             <scope>provided</scope>
116         </dependency>
117 
118 
119         <!-- junit -->
120         <!-- https://mvnrepository.com/artifact/junit/junit -->
121         <dependency>
122             <groupId>junit</groupId>
123             <artifactId>junit</artifactId>
124             <version>4.12</version>
125         </dependency>
126 
127         <!-- mybatis的一个曾强工具 -->
128         <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
129         <dependency>
130             <groupId>com.baomidou</groupId>
131             <artifactId>mybatis-plus</artifactId>
132             <version>2.1.8</version>
133         </dependency>
134 
135         <!-- MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中 -->
136         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
137         <dependency>
138             <groupId>org.mybatis</groupId>
139             <artifactId>mybatis-spring</artifactId>
140             <version>1.3.1</version>
141         </dependency>
142 
143         <!-- 导入spring data redis 缓存包 -->
144         <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
145         <dependency>
146             <groupId>org.springframework.data</groupId>
147             <artifactId>spring-data-redis</artifactId>
148             <version>2.0.2.RELEASE</version>
149         </dependency>
150 
151         <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
152         <dependency>
153             <groupId>redis.clients</groupId>
154             <artifactId>jedis</artifactId>
155             <version>2.9.0</version>
156         </dependency>
157     </dependencies>
158 </project>

2.web.xml配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     <!-- sping -->
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>classpath:applicationContext.xml</param-value>
10     </context-param>
11     <listener>
12         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
13     </listener>
14     <listener>
15         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
16     </listener>
17     <!-- 日志 -->
18     <context-param>
19         <param-name>log4jConfigLocation</param-name>
20         <param-value>classpath:log4j.properties</param-value>
21     </context-param>
22     <context-param>
23         <param-name>log4jRefreshInterval</param-name>
24         <param-value>60000</param-value>
25     </context-param>
26     <!-- springMVC -->
27     <servlet>
28         <servlet-name>dispatcherServlet</servlet-name>
29         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
30         <init-param>
31             <param-name>contextConfigLocation</param-name>
32             <param-value>classpath:dispatcherServlet.xml</param-value>
33         </init-param>
34         <load-on-startup>1</load-on-startup>
35     </servlet>
36     <servlet-mapping>
37         <servlet-name>dispatcherServlet</servlet-name>
38         <url-pattern>/</url-pattern>
39     </servlet-mapping>
40     <!-- 字符编码 -->
41     <filter>
42         <filter-name>CharacterEncodingFilter</filter-name>
43         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
44         <init-param>
45             <param-name>encoding</param-name>
46             <param-value>utf-8</param-value>
47         </init-param>
48         <init-param>
49             <param-name>forceRequestEncoding</param-name>
50             <param-value>true</param-value>
51         </init-param>
52         <init-param>
53             <param-name>forceResponseEncoding</param-name>
54             <param-value>true</param-value>
55         </init-param>
56     </filter>
57     <filter-mapping>
58         <filter-name>CharacterEncodingFilter</filter-name>
59         <url-pattern>/*</url-pattern>
60     </filter-mapping>
61     <filter>
62         <filter-name>HiddenHttpMethodFilter</filter-name>
63         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
64     </filter>
65     <filter-mapping>
66         <filter-name>HiddenHttpMethodFilter</filter-name>
67         <url-pattern>/*</url-pattern>
68     </filter-mapping>
69     <filter>
70         <filter-name>HttpPutFormContentFilter</filter-name>
71         <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
72     </filter>
73     <filter-mapping>
74         <filter-name>HttpPutFormContentFilter</filter-name>
75         <url-pattern>/*</url-pattern>
76     </filter-mapping>
77     <display-name>ssm-crud</display-name>
78     <welcome-file-list>
79         <welcome-file>index.html</welcome-file>
80         <welcome-file>index.htm</welcome-file>
81         <welcome-file>index.jsp</welcome-file>
82         <welcome-file>default.html</welcome-file>
83         <welcome-file>default.htm</welcome-file>
84         <welcome-file>default.jsp</welcome-file>
85     </welcome-file-list>
86 </web-app>

3.applicationContext.xml  及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"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 8         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 9         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
11     
12     <!-- 配置自动扫描的包,规定声明注解为@controller进行扫描 -->
13     <context:component-scan base-package="com.shao.demo">
14         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
15     </context:component-scan>
16 
17     <!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->
18     <!-- 数据源,事务控制,xxx -->
19     <context:property-placeholder location="classpath:dbconfig.properties" /><!-- 导入properties配置文件 -->
20     <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
21         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
22         <property name="driverClass" value="${jdbc.driverClass}"></property>
23         <property name="user" value="${jdbc.user}"></property>
24         <property name="password" value="${jdbc.password}"></property>
25     </bean>
26 
27     <!-- 配置和MyBatis的整合 -->
28     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
29         <!-- 指定mybatis全局配置文件的位置 -->
30         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
31         <property name="dataSource" ref="pooledDataSource"></property>
32         <!-- 指定mybatis,mapper文件的位置 -->
33         <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
34     </bean>
35 
36     <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
37     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
38         <!--扫描所有dao接口的实现,加入到ioc容器中 -->
39         <property name="basePackage" value="com.shao.demo.dao"></property>
40     </bean>
41     
42     <!-- 配置一个可以执行批量的sqlSession -->
43     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
44         <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
45         <constructor-arg name="executorType" value="BATCH"></constructor-arg>
46     </bean>
47 
48     <!-- 事务控制的配置 -->
49     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
50         <!--控制住数据源  -->
51         <property name="dataSource" ref="pooledDataSource"></property>
52     </bean>
53     <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式)  -->
54     <aop:config>
55         <!-- 切入点表达式 -->
56         <aop:pointcut expression="execution(* com.shao.demo.service..*(..))" id="txPoint"/>
57         <!-- 配置事务增强 -->
58         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
59     </aop:config>
60     
61     <!--配置事务增强,事务如何切入  -->
62     <tx:advice id="txAdvice" transaction-manager="transactionManager">
63         <tx:attributes>
64             <!-- 所有方法都是事务方法 -->
65             <tx:method name="*"/>
66             <!--以get开始的所有方法  -->
67             <tx:method name="get*" read-only="true"/>
68         </tx:attributes>
69     </tx:advice>
70     
71     <!-- Spring配置文件的核心点(数据源、与mybatis的整合,事务控制) -->
72     
73     
74     <!-- 增加redis的properties文件 -->
75     <!-- <context:property-placeholder location="classpath:redis.properties" /> -->
76     <!-- 导入redis的配置文件 -->
77     <import resource="classpath:redis-config.xml"/>
78 </beans>

4.dbconfig.properties  -数据库配置文件

1 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
2 jdbc.driverClass=com.mysql.jdbc.Driver
3 jdbc.user=root
4 jdbc.password=root

5.dispatcherServlet.xml  -springMVC配置文件

 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     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
 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-4.3.xsd">
 9 
10     <!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置  -->
11     <context:component-scan base-package="com.shao.demo" use-default-filters="false">
12         <!--只扫描控制器。  -->
13         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
14     </context:component-scan>
15     
16     <!--配置视图解析器,方便页面返回  -->
17     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18         <property name="prefix" value="/WEB-INF/jsp/"></property>
19         <property name="suffix" value=".jsp"></property>
20     </bean>
21     
22     <!--两个标准配置  -->
23     <!-- 将springmvc不能处理的请求交给tomcat -->
24     <mvc:default-servlet-handler/>
25     <!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
26     <mvc:annotation-driven/>
27 
28 </beans>

6.log4j.properties   --日志配置文件

 1 log4j.rootLogger=DEBUG,stdout,error
 2 
 3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 4 log4j.appender.stdout.Target=System.out
 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 6 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 7 
 8 log4j.logger.EntityLog=INFO,EntityLog
 9 log4j.appender.EntityLog=org.apache.log4j.DailyRollingFileAppender
10 log4j.appender.EntityLog.File=${catalina.home}/logs/EntityLog.log
11 log4j.appender.EntityLog.layout=org.apache.log4j.PatternLayout
12 log4j.appender.EntityLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS} %c{1} %m%n
13 log4j.appender.EntityLog.DatePattern=_yyyyMMdd\'.log\'
14 log4j.additivity.EntityLog=false
15 
16 
17 log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
18 log4j.appender.error.File=${catalina.home}/logs/error.log
19 log4j.appender.error.threshold=ERROR
20 log4j.appender.error.layout=org.apache.log4j.PatternLayout
21 log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS} %c{1} %m%n
22 log4j.appender.error.DatePattern=_yyyyMMdd\'.log\'
23 
24 # Druid
25 log4j.logger.druid.sql=DEBUG
26 log4j.logger.druid.sql.DataSource=warn
27 log4j.logger.druid.sql.Connection=warn
28 log4j.logger.druid.sql.Statement=DEBUG
29 log4j.logger.druid.sql.ResultSet=warn

7.mybatis-config.xml  --mybatis配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration
 3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <settings>
 7         <!-- 设置自动驼峰命名转换(mapUnderscoreToCamelCase) -->
 8         <setting name="mapUnderscoreToCamelCase" value="true"/>
 9     </settings>
10     
11     <!-- 默认别名为类的名称 -->
12     <typeAliases>
13         <package name="com.shao.demo.entity"/>
14     </typeAliases>
15     
16     <plugins>
17         <!-- 分页插件 -->
18         <plugin interceptor="com.github.pagehelper.PageInterceptor">
19             <!--分页参数合理化  -->
20             <property name="reasonable" value="true"/>
21         </plugin>
22     </plugins>
23 
24 </configuration>

8.redis-config.xml  --redis缓存配置

 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:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 7      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
 8      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
 9      http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"
10     default-autowire="byName" default-lazy-init="true">
11     
12     
13     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
14         <property name="maxIdle" value="100" />
15         <property name="maxWaitMillis" value="1000" />
16         <property name="testOnBorrow" value="true" />
17     </bean>
18     <!-- redis服务器中心 -->
19     <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
20         <property name="poolConfig" ref="poolConfig" />
21         <property name="port" value="6379" />
22         <property name="hostName" value="127.0.0.1" />
23         <!-- <property name="password" value="${redis.password}" /> -->
24         <property name="timeout" value="100000"></property>
25     </bean>
26     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
27         <property name="connectionFactory" ref="connectionFactory" />
28         <property name="keySerializer">
29             <bean
30                 class="org.springframework.data.redis.serializer.StringRedisSerializer" />
31         </property>
32         <property name="valueSerializer">
33             <bean
34                 class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
35         </property>
36     </bean>
37     
38     <!-- <bean id="redisUtil" class="com.shao.demo.utils.RedisUtil">
39         <property name="redisTemplate" ref="redisTemplate" />
40     </bean> -->
41 
42 </beans>

9.redis.properties  --缓存配置文件

 1 #redis setting  
 2 redis.host=127.0.0.1
 3 redis.port=6379
 4 #redis.password=123456
 5 redis.maxIdle=100
 6 redis.maxActive=300
 7 redis.maxWait=1000
 8 redis.testOnBorrow=true
 9 redis.timeout=100000
10 
11 fep.local.cache.capacity =10000

缓存配置的不是太成功,待后续我掌握了,再来进行修改,不设置缓存项目也是可以运行的,但是缓存是大势所趋,以后的项目,缓存大部分都会用到,很重要

redis.properties这个文件没用到,我在xml配置文件写死了,但不影响项目运行

 

每个章节末尾一句话勉励自己

再聪明的人,你不努力,你也仅仅只是聪明。。。。。

以上是关于ssm框架的整合搭建的主要内容,如果未能解决你的问题,请参考以下文章

SSM框架整合 环境搭建

SSM框架整合搭建教程

SSM框架——整合搭建流程

SSM框架整合spring框架的搭建

SSM框架整合——源码超详细(IDEA 搭建 ssm 项目)

史上最详细的IDEA优雅整合Maven+SSM框架(详细思路+附带源码)