ssm框架为中小企业一般使用的java web开发框架非常方便快捷,在项目开发中,我们经常会遇到,项目开发中往往会看到各种各样的配置文件让人眼花缭乱,我认为,一切事物都有一个开始的过程,如果开始上来在别人搭好的框架中进行开发,无可厚非,可以提高你的开发编码的水平,让你从一个编码的初学者迅速入门融入开发项目中,但是久而久之,不自己搭建常用的开发框架,心里难免会发慌,就像在高数的学习中,有一些复杂的公式很实用很好用,往往由一些较简单的公式或者理论作为基础推倒而来,这些复杂公式难记,有些还容易混淆出错,有些是在特定条件下成立(这一点我觉得在我们开发中注解的配置有点相似,有些时候注解编译时不报错,运行发现找不到适合的bean,可能就是由于使用其他框架的类似注解),不了解它是怎么推导出来的,心里有点发慌,不知道你有没有这种感觉,而且面试的过程中,也常常会作为问题被问道,在背了一大推原理理论时,甚至心里也不是很有低,不是很理解,所以自己搭建ssm框架然后配合理论进行深入理解,之后对于面试还是平时纠错都是大有裨益,都对自身能力,竞争力的提升是有帮助的
进行maven web项目的搭建
不同开发工具可能有所区别,这里是针对eclipse + maven
使用maven帮助我们快速引入jar包,很方便
2spring + mybatis整合
在建立web项目结构之后,mybatis作为持久层框架,首先要进行数据库相关配置
a.在我们的maven web中,在main/resources中创建jdbc.properties(数据库配置文件)
1. driver=com.mysql.jdbc.Driver
2. url=jdbc:mysql://10.221.10.111:8080/db_zsl
3. username=demao
4. password=demao
5. #定义初始连接数
6. initialSize=0
7. #定义最大连接数
8. maxActive=20
9. #定义最大空闲
10. maxIdle=20
11. #定义最小空闲
12. minIdle=1
13. #定义最长等待时间
14. maxWait=60000
这里是mysql的数据库驱动,oracle有所不同
b.建立spring-mybatis.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: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"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans
7. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
8. http://www.springframework.org/schema/context
9. http://www.springframework.org/schema/context/spring-context-3.1.xsd
10. http://www.springframework.org/schema/mvc
11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12. <!-- 自动扫描 -->
13. <context:component-scan base-package="com.cn.hnust" />
14. <!-- 引入配置文件 -->
15. <bean id="propertyConfigurer"
16. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
17. <property name="location" value="classpath:jdbc.properties" />
18. </bean>
19.
20. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
21. destroy-method="close">
22. <property name="driverClassName" value="${driver}" />
23. <property name="url" value="${url}" />
24. <property name="username" value="${username}" />
25. <property name="password" value="${password}" />
26. <!-- 初始化连接大小 -->
27. <property name="initialSize" value="${initialSize}"></property>
28. <!-- 连接池最大数量 -->
29. <property name="maxActive" value="${maxActive}"></property>
30. <!-- 连接池最大空闲 -->
31. <property name="maxIdle" value="${maxIdle}"></property>
32. <!-- 连接池最小空闲 -->
33. <property name="minIdle" value="${minIdle}"></property>
34. <!-- 获取连接最大等待时间 -->
35. <property name="maxWait" value="${maxWait}"></property>
36. </bean>
37.
38. <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
39. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
40. <property name="dataSource" ref="dataSource" />
41. <!-- 自动扫描mapping.xml文件 -->
42. <property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"></property>
43. </bean>
44.
45. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
46. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
47. <property name="basePackage" value="com.cn.hnust.dao" />
48. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
49. </bean>
50.
51. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
52. <bean id="transactionManager"
53. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
54. <property name="dataSource" ref="dataSource" />
55. </bean>
56.
57. </beans>
c.为了方便调试,一般都会使用日志来输出信息,这一点在ssm中不是必须
详细配置参考http://blog.csdn.net/zhshulin/article/details/37937365
在main/resources中增加配置文件log4j.properties
1. #定义LOG输出级别
2. log4j.rootLogger=INFO,Console,File
3. #定义日志输出目的地为控制台
4. log4j.appender.Console=org.apache.log4j.ConsoleAppender
5. log4j.appender.Console.Target=System.out
6. #可以灵活地指定日志输出格式,下面一行是指定具体的格式
7. log4j.appender.Console.layout = org.apache.log4j.PatternLayout
8. log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
9.
10. #文件大小到达指定尺寸的时候产生一个新的文件
11. log4j.appender.File = org.apache.log4j.RollingFileAppender
12. #指定输出目录
13. log4j.appender.File.File = logs/ssm.log
14. #定义文件最大大小
15. log4j.appender.File.MaxFileSize = 10MB
16. # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
17. log4j.appender.File.Threshold = ALL
18. log4j.appender.File.layout = org.apache.log4j.PatternLayout
19. log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
d.完成以上配置之后,通过junit单元测试,我们可以完成service,serviceImpl,dao,pojo,mapper等的编写工作然后进行单元测试,测试spring-mybatis配置正确
其中dao,pojo,mapper等的生成可以使用mybatis-generator生成工具自动生成,如果熟悉的同学可以直接编写
这是service代码
创建单元测试类进行测试
测试成功输入结果
至此spring-mybatis集成完毕(注意上述过程省略了数据库制作测试数据的过程)
e.main/resources中创建配置文件spring-mvc.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: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"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans
7. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
8. http://www.springframework.org/schema/context
9. http://www.springframework.org/schema/context/spring-context-3.1.xsd
10. http://www.springframework.org/schema/mvc
11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12. <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
13. <context:component-scan base-package="com.cn.hnust.controller" />
14. <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
15. <bean id="mappingJacksonHttpMessageConverter"
16. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
17. <property name="supportedMediaTypes">
18. <list>
19. <value>text/html;charset=UTF-8</value>
20. </list>
21. </property>
22. </bean>
23. <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
24. <bean
25. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
26. <property name="messageConverters">
27. <list>
28. <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
29. </list>
30. </property>
31. </bean>
32. <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
33. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
34. <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
35. <property name="prefix" value="/WEB-INF/jsp/" />
36. <property name="suffix" value=".jsp" />
37. </bean>
38.
39. <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
40. <bean id="multipartResolver"
41. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
42. <!-- 默认编码 -->
43. <property name="defaultEncoding" value="utf-8" />
44. <!-- 文件大小最大值 -->
45. <property name="maxUploadSize" value="10485760000" />
46. <!-- 内存中的最大值 -->
47. <property name="maxInMemorySize" value="40960" />
48. </bean>
49.
50. </beans>
f.最后在web.xml中配置以上xml配置文件,完成ssm的集成搭建
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_3_0.xsd"
5. version="3.0">
6. <display-name>Archetype Created Web Application</display-name>
7. <!-- Spring和mybatis的配置文件 -->
8. <context-param>
9. <param-name>contextConfigLocation</param-name>
10. <param-value>classpath:spring-mybatis.xml</param-value>
11. </context-param>
12. <!-- 编码过滤器 -->
13. <filter>
14. <filter-name>encodingFilter</filter-name>
15. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
16. <async-supported>true</async-supported>
17. <init-param>
18. <param-name>encoding</param-name>
19. <param-value>UTF-8</param-value>
20. </init-param>
21. </filter>
22. <filter-mapping>
23. <filter-name>encodingFilter</filter-name>
24. <url-pattern>/*</url-pattern>
25. </filter-mapping>
26. <!-- Spring监听器 -->
27. <listener>
28. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
29. </listener>
30. <!-- 防止Spring内存溢出监听器 -->
31. <listener>
32. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
33. </listener>
34.
35. <!-- Spring MVC servlet -->
36. <servlet>
37. <servlet-name>SpringMVC</servlet-name>
38. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
39. <init-param>
40. <param-name>contextConfigLocation</param-name>
41. <param-value>classpath:spring-mvc.xml</param-value>
42. </init-param>
43. <load-on-startup>1</load-on-startup>
44. <async-supported>true</async-supported>
45. </servlet>
46. <servlet-mapping>
47. <servlet-name>SpringMVC</servlet-name>
48. <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
49. <url-pattern>/</url-pattern>
50. </servlet-mapping>
51. <welcome-file-list>
52. <welcome-file>/index.jsp</welcome-file>
53. </welcome-file-list>
54.
55. </web-app>
完成以上工作之后,启动测试一下,如果顺利,按照这一套流程下来基本无误。这个是我从网上整理处理出来按照这个步骤一个上午搭建完成,基本无误。可能不同人根据自身风格,项目不同,搭建有所不同。