SSM整合详解

叙述:

         最近在撘SSM框架,突然觉得自己知识还是不够,各种查资料慢慢的摸索,所以写下心得纪念一下,也为以后的使用留个参考


Spring:

         Spring是一个管理框架,底层运用了23中设计模式中的工厂设计模式,用来将现有的项目中的每一个组件统一生产并管理(注意:实体类一般不管理)

         两大核心思想:

             IOC:控制反转(将原来的new对象,交给Spring工厂进行管理)

            AOP:面向切面编程(从切面入手,通过动态代理类给目标类增加额外功能)

                   动态代理类的两种实现方式:  JDK提供和cglib动态代理(以后单独分析代理)

           关于DI的依赖注入:Spring不但要创建对象,还要建立类与类之间的关系,所以引入了DI注入,

               DI 注入的三中方式:1 set 注入 

                                              2 构造注入

                                              3 自动注入

          (曾经在面试宝典看到过五中方式,另外两种没有用过暂且不叙述)

SpringMVC:

         SpringMVC是一个控制层(Controller),用来和接受客户端的数据,相当于Struts的action,更好的与Spring结合,是一个轻量级框架

Mybatis:

        Mybatis是一个持久层框架,封装了jdbc,主要用来操作数据库的增删改查,自动完成了对象之间的映射


开发流程:

      

    1  Maven需要的jar,在pom.xml中配置

<properties>  
02.         <!-- spring版本号 -->  
03.         <spring.version>4.0.2.RELEASE</spring.version>  
04.         <!-- mybatis版本号 -->  
05.         <mybatis.version>3.2.6</mybatis.version>  
06.         <!-- log4j日志文件管理包版本 -->  
07.         <slf4j.version>1.7.7</slf4j.version>  
08.         <log4j.version>1.2.17</log4j.version>  
09.     </properties>  
10.   
11.     <dependencies>  
12.         <dependency>  
13.             <groupId>junit</groupId>  
14.             <artifactId>junit</artifactId>  
15.             <version>4.11</version>  
16.             <!-- 表示开发的时候引入,发布的时候不会加载此包 -->  
17.             <scope>test</scope>  
18.         </dependency>  
19.         <!-- spring核心包 -->  
20.         <dependency>  
21.             <groupId>org.springframework</groupId>  
22.             <artifactId>spring-core</artifactId>  
23.             <version>${spring.version}</version>  
24.         </dependency>  
25.   
26.         <dependency>  
27.             <groupId>org.springframework</groupId>  
28.             <artifactId>spring-web</artifactId>  
29.             <version>${spring.version}</version>  
30.         </dependency>  
31.         <dependency>  
32.             <groupId>org.springframework</groupId>  
33.             <artifactId>spring-oxm</artifactId>  
34.             <version>${spring.version}</version>  
35.         </dependency>  
36.         <dependency>  
37.             <groupId>org.springframework</groupId>  
38.             <artifactId>spring-tx</artifactId>  
39.             <version>${spring.version}</version>  
40.         </dependency>  
41.   
42.         <dependency>  
43.             <groupId>org.springframework</groupId>  
44.             <artifactId>spring-jdbc</artifactId>  
45.             <version>${spring.version}</version>  
46.         </dependency>  
47.   
48.         <dependency>  
49.             <groupId>org.springframework</groupId>  
50.             <artifactId>spring-webmvc</artifactId>  
51.             <version>${spring.version}</version>  
52.         </dependency>  
53.         <dependency>  
54.             <groupId>org.springframework</groupId>  
55.             <artifactId>spring-aop</artifactId>  
56.             <version>${spring.version}</version>  
57.         </dependency>  
58.   
59.         <dependency>  
60.             <groupId>org.springframework</groupId>  
61.             <artifactId>spring-context-support</artifactId>  
62.             <version>${spring.version}</version>  
63.         </dependency>  
64.   
65.         <dependency>  
66.             <groupId>org.springframework</groupId>  
67.             <artifactId>spring-test</artifactId>  
68.             <version>${spring.version}</version>  
69.         </dependency>  
70.         <!-- mybatis核心包 -->  
71.         <dependency>  
72.             <groupId>org.mybatis</groupId>  
73.             <artifactId>mybatis</artifactId>  
74.             <version>${mybatis.version}</version>  
75.         </dependency>  
76.         <!-- mybatis/spring包 -->  
77.         <dependency>  
78.             <groupId>org.mybatis</groupId>  
79.             <artifactId>mybatis-spring</artifactId>  
80.             <version>1.2.2</version>  
81.         </dependency>  
82.         <!-- 导入java ee jar 包 -->  
83.         <dependency>  
84.             <groupId>javax</groupId>  
85.             <artifactId>javaee-api</artifactId>  
86.             <version>7.0</version>  
87.         </dependency>  
88.         <!-- 导入Mysql数据库链接jar包 -->  
89.         <dependency>  
90.             <groupId>mysql</groupId>  
91.             <artifactId>mysql-connector-java</artifactId>  
92.             <version>5.1.30</version>  
93.         </dependency>  
94.         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->  
95.         <dependency>  
96.             <groupId>commons-dbcp</groupId>  
97.             <artifactId>commons-dbcp</artifactId>  
98.             <version>1.2.2</version>  
99.         </dependency>  
100.         <!-- JSTL标签类 -->  
101.         <dependency>  
102.             <groupId>jstl</groupId>  
103.             <artifactId>jstl</artifactId>  
104.             <version>1.2</version>  
105.         </dependency>  
106.         <!-- 日志文件管理包 -->  
107.         <!-- log start -->  
108.         <dependency>  
109.             <groupId>log4j</groupId>  
110.             <artifactId>log4j</artifactId>  
111.             <version>${log4j.version}</version>  
112.         </dependency>  
113.           
114.           
115.         <!-- 格式化对象,方便输出日志 -->  
116.         <dependency>  
117.             <groupId>com.alibaba</groupId>  
118.             <artifactId>fastjson</artifactId>  
119.             <version>1.1.41</version>  
120.         </dependency>  
121.   
122.   
123.         <dependency>  
124.             <groupId>org.slf4j</groupId>  
125.             <artifactId>slf4j-api</artifactId>  
126.             <version>${slf4j.version}</version>  
127.         </dependency>  
128.   
129.         <dependency>  
130.             <groupId>org.slf4j</groupId>  
131.             <artifactId>slf4j-log4j12</artifactId>  
132.             <version>${slf4j.version}</version>  
133.         </dependency>  
134.         <!-- log end -->  
135.         <!-- 映入JSON -->  
136.         <dependency>  
137.             <groupId>org.codehaus.jackson</groupId>  
138.             <artifactId>jackson-mapper-asl</artifactId>  
139.             <version>1.9.13</version>  
140.         </dependency>  
141.         <!-- 上传组件包 -->  
142.         <dependency>  
143.             <groupId>commons-fileupload</groupId>  
144.             <artifactId>commons-fileupload</artifactId>  
145.             <version>1.3.1</version>  
146.         </dependency>  
147.         <dependency>  
148.             <groupId>commons-io</groupId>  
149.             <artifactId>commons-io</artifactId>  
150.             <version>2.4</version>  
151.         </dependency>  
152.         <dependency>  
153.             <groupId>commons-codec</groupId>  
154.             <artifactId>commons-codec</artifactId>  
155.             <version>1.9</version>  
156.         </dependency>  
157.           
158.           
159.     </dependencies>  


2 Spring+Mybatis的整合

jdbc.propertes外部资源文件配置

driver=com.mysql.jdbc.Driver  
02. url=jdbc:mysql://10.221.10.111:8080/db_zsl  
03. username=demao  
04. password=demao  
05. #定义初始连接数  
06. initialSize=0  
07. #定义最大连接数  
08. maxActive=20  
09. #定义最大空闲  
10. maxIdle=20  
11. #定义最小空闲  
12. minIdle=1  
13. #定义最长等待时间  
14. maxWait=20000  


aplicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>  
02. <beans xmlns="http://www.springframework.org/schema/beans"  
03.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
04.     xmlns:context="http://www.springframework.org/schema/context"  
05.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
06.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
07.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
08.                         http://www.springframework.org/schema/context    
09.                         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>  


log4j的配置文件

    log4j.properties

log4j.rootLogger=INFO,Console,File  
03. #定义日志输出目的地为控制台  
04. log4j.appender.Console=org.apache.log4j.ConsoleAppender  
05. log4j.appender.Console.Target=System.out  
06. #可以灵活地指定日志输出格式,下面一行是指定具体的格式  
07. log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
08. log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  
09.   
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


Junit测试

  首先PowerDisney建立一个表

  利用Generator自动生成实体类,SQL,和Dao

  在创建Service接口和实现类

      

17.   
18. @RunWith(SpringJUnit4ClassRunner.class)     //表示继承了SpringJUnit4ClassRunner类  
19. @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})  
20.   
21. public class TestMyBatis {  
22.     private static Logger logger = Logger.getLogger(TestMyBatis.class);  
23. //  private ApplicationContext ac = null;  
24.     @Resource  
25.     private IUserService userService = null;  
26.   
27. //  @Before  
28. //  public void before() {  
29. //      ac = new ClassPathXmlApplicationContext("applicationContext.xml");  
30. //      userService = (IUserService) ac.getBean("userService");  
31. //  }  
32.   
33.     @Test  
34.     public void test1() {  
35.         User user = userService.getUserById(1);  
36.         // System.out.println(user.getUserName());  
37.         // logger.info("值:"+user.getUserName());  
38.         logger.info(JSON.toJSONString(user));  
39.     }  
40. }  

测试通过了就成功一半了


配置SpringMVC

<?xml version="1.0" encoding="UTF-8"?>  
02. <beans xmlns="http://www.springframework.org/schema/beans"  
03.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
04.     xmlns:context="http://www.springframework.org/schema/context"  
05.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
06.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
07.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
08.                         http://www.springframework.org/schema/context    
09.                         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>  


配置web.xml

<?xml version="1.0" encoding="UTF-8"?>  
02. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
03.     xmlns="http://java.sun.com/xml/ns/javaee"  
04.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
05.     version="3.0">  
06.     <display-name>Archetype Created Web Application</display-name>  
07.     <!-- Spring和mybatis的配置文件 -->  
08.     <context-param>  
09.         <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>  

建立一个简单的Controller

  
13. @Controller  
14. @RequestMapping("/user")  
15. public class UserController {  
16.     @Resource  
17.     private IUserService userService;  
18.       
19.     @RequestMapping("/showUser")  
20.     public String toIndex(HttpServletRequest request,Model model){  
21.         int userId = Integer.parseInt(request.getParameter("id"));  
22.         User user = this.userService.getUserById(userId);  
23.         model.addAttribute("user", user);  
24.         return "showUser";  
25.     }  
26. }  


部署项目

输入网址:localhost:8080/项目名称/user/showUser?id=1


如果不成功 就继续和bug战斗吧,,,,,,,


参考文献点击打开链接












































评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值