关于ssm整合,一个整合的实例
创建一个动态的web工程 SSM01
我用的是spring-tool-suite,一种带了spring插件的eclipse。
导入整合所需要的包,放到lib目录里。
大概需要这些包,可供参考。
c3p0-0.9.1.2.jar
classmate-0.8.0.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-fileupload-1.2.1.jar
commons-io-2.0.jar
commons-logging-1.1.3.jar
hibernate-validator-5.0.0.CR2.jar
hibernate-validator-annotation-processor-5.0.0.CR2.jar
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
jboss-logging-3.1.1.GA.jar
jstl.jar
mybatis-3.4.1.jar
mybatis-spring-1.3.0.jar
mysql-connector-java-5.1.37-bin.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE-sources.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE-sources.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE-sources.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE-sources.jar
spring-webmvc-4.0.0.RELEASE.jar
standard.jar
validation-api-1.1.0.CR1.jar
配置连接数据库 db.properties文件
我用的mysql数据库,这里可以改成自己连接的数据库
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/book?characterEncoding=utf-8&useUnicode=true
user=root
password=wbyq
maxSize=10
initSize=5
在SSM01工程中整合Spring
- 新建spring配置文件applicationContext.xml
- 配置自动扫描的包,spring配置扫描除Controller和ControllerAdvice的注解
<context:component-scan base-package="com.wanbangee">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
导入外部属性文件
<context:property-placeholder location="classPath:db.properties"/>
配置连接池
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="maxPoolSize" value="${maxSize}"></property>
<property name="initialPoolSize" value="${initSize}"></property>
</bean>
配置事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven />
<!-- mybatis的相关配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-conf.xml"></property>
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 注册sql映射文件 -->
<property name="mapperLocations" value="classpath:com.wanbangee.dao.*.xml"></property>
<!-- 配置别名处理器 -->
<!-- <property name="typeAliasesPackage" value="com.wanbangee.entities"></property> -->
</bean>
<!-- 扫描所有的DAO接口中的@Mapper注解,并将DAO接口的代理实现类加入到Spring的IOC容器中 -->
<!-- <bean id="configure" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wanbangee.dao"></property>
</bean>
-->
在SSM01工程中整合SpringMVC
- 新建SpringMVC-servlet.xml配置文件
配置自动扫描的包 SpringMVC只扫描 Controller注解和ControllerAdvice注解
<context:component-scan base-package="com.wanbangee" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
springMVC配置
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
配置静态资源解析器和注解驱动
<mvc:default-servlet-handler/>
<!--
mvc:annotation-driven : 是万能的注解驱动
conversion-service : 配置SpringMVC上下文中中的类型转换器
- 不配置:使用默认的
- 配置:默认的+配置的
-->
<mvc:annotation-driven></mvc:annotation-driven>
配置MultipartResolver,这里用到文件上传就需要配置,
不用可不用配。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 文件上传的最大字节数 1024*1024*5 -->
<property name="maxUploadSize" value="6000000"></property>
</bean>
在SSM01工程中整合MyBatis
- 需要使用Spring和MyBatis的适配包(mybatis-spring-1.3.0.jar【支持MyBatis3.4.0及更高版本,支持Spring3.0及更高版本】)
- 需要加入MyBatis的jar包
在SpringIOC容器的配置文件 中加入MyBatis的配置 - 新建mybatis-conf.xml文件
<configuration>
<settings>
<!--设置驼峰命名 -->
<setting name="mapUnderscoreToCamelCase" value="true"></setting>
<!-- 开启延迟加载策略 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<databaseIdProvider type="DB_VENDOR">
<property name="MySQL" value="mysql"/>
<property name="Oracle" value="oracle"/>
<property name="SQL Server" value="sqlserver"/>
</databaseIdProvider>
</configuration>