SSM框架整合基本操作

1.首先导入各种需要的配置包,在这里个人的习惯就是先导入mybatis相关包,然后通过编程软件集成一个spring3.0或者spring3.1进来并选择里面相应的包,这样就不需要我们自己去导入相应的spring包了。

2.新建spring-mybatis.xml文件以及spring-mvc.xml文件,分别配置如下:

  spring-mybatis.xml配置文件

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 9     http://www.springframework.org/schema/context 
10     http://www.springframework.org/schema/context/spring-context-3.1.xsd
11     ">
12     <!-- 采用注释的方式配置bean -->
13     <context:annotation-config />
14     <!-- 配置要扫描的包 -->
15     <context:component-scan base-package="com.julong.testoracle" />
16     
17     <!-- 分解配置 jdbc.properites -->
18     <context:property-placeholder location="classpath:jdbc.properties" />
19     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">
20         <property name="driverClassName" value="${jdbc.driverClassName}" />
21         <property name="url" value="${jdbc.url}" />
22         <property name="username" value="${jdbc.username}" />
23         <property name="password" value="${jdbc.password}" />
24         <!-- 队列中的最小等待数 -->
25         <property name="minIdle" value="${jdbc.minIdle}"></property>
26         <!-- 队列中的最大等待数 -->
27         <property name="maxIdle" value="${jdbc.maxIdle}"></property>
28         <!-- 最长等待时间,单位毫秒 -->
29         <property name="maxWait" value="${jdbc.maxWait}"></property>
30         <!-- 最大活跃数 -->
31         <property name="maxActive" value="${jdbc.maxActive}"></property>
32         <property name="initialSize" value="${jdbc.initialSize}"></property>    
33     </bean>
34     
35      <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
36     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
37         <property name="dataSource" ref="dataSource" />  
38         <!-- 自动扫描mapping.xml文件 -->  
39         <property name="mapperLocations" value="classpath:com/julong/testoracle/mapper/*.xml"></property>  
40     </bean> 
41     
42      <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
43     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
44         <property name="basePackage" value="com.julong.testoracle.dao"> </property> 
45         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
46     </bean>
47     
48     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
49     <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
50         <property name="dataSource" ref="dataSource" />  
51     </bean>  
52 </beans>
View Code

spring-mvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
  <!-- 配置需要用过mvc监听的文件 --> <context:component-scan base-package="com.julong.testoracle.controller" /> </beans>

3.配置相应的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 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 7   <display-name>oracle</display-name>
 8   <!-- spring的监听器 -->
 9   <listener>
10     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
11   </listener>
12   
13   <!--全局范围内环境参数初始化,即初始化spring-mybatis.xml-->  
14   <context-param>
15     <param-name>contextConfigLocation</param-name>  <!--参数名称-->  
16     <param-value>classpath:spring-mybatis.xml</param-value><!--参数取值--> 
17   </context-param>
18   
19   !--用来声明一个servlet的数据 --> 
20   <servlet>
21         <servlet-name>springMVC</servlet-name><!--指定servlet的名称-->  
22         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--指定servlet的类名称,这里配置了前端控制器-->
23         <init-param>
24             <param-name>contextConfigLocation</param-name><!--参数名称-->
25             <param-value>classpath:spring-mvc.xml</param-value><!--参数值-->
26         </init-param>
27     </servlet>
28     <servlet-mapping>
29         <servlet-name>springMVC</servlet-name> <!--指定servlet的名称--> 
30         <url-pattern>*.mvc</url-pattern> <!--指定servlet所对应的URL-->  
31     </servlet-mapping>
32     
33     <!-- 编码过滤器 -->
34     <filter>  
35         <filter-name>CharacterEncodingFilter</filter-name>  
36         <filter-class>
37             org.springframework.web.filter.CharacterEncodingFilter
38         </filter-class>  
39         <init-param>  
40             <param-name>encoding</param-name>  
41             <param-value>UTF-8</param-value>  
42         </init-param>  
43     </filter>
44     <filter-mapping>  
45         <filter-name>CharacterEncodingFilter</filter-name>  
46         <url-pattern>/*</url-pattern>  
47     </filter-mapping>
48 </web-app>
View Code

4.指定数据库连接时需要引入的资源文件(因前面给其分解开来了)

#jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.driverClassName=oracle.jdbc.OracleDriver
#jdbc.url=jdbc:mysql://localhost:3306/operationLog
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
#jdbc.url=jdbc:oracle:thin:@192.168.1.111:1521:orcl
jdbc.username=oracle
jdbc.password=oracle
#jdbc.password=root
jdbc.maxActive = 2
jdbc.maxIdle =5
jdbc.minIdle=1
jdbc.initialSize =3
jdbc.maxWait =3000
View Code

 

posted @ 2016-09-05 13:39  一念花开满天下  阅读(386)  评论(0编辑  收藏  举报