SSM整合开发
所谓的SSM整合开发就是使用SpringMvc框架,Spring框架,MyBatis框架技术进行一个大的整合来开发一个项目。
这个技术点很重要,是每个JavaEE程序员的一门必修课,废话不多说,直接开始。
1,创建一个动态的Web工程(可以使用项目构建工具Maven),导入项目所需要的jar包
jar包包括:Spring和SpringMvc的jar包,MyBatis的jar包,Spring和Mybatis的整合适配包(注意版本问题),
日志jar包,数据库驱动包,数据库连接池包,servlet-api.jar,jsp-api.jar等等。。。
2,在各个配置文件中进行配置(核心步骤)
文件包括:动态Web工程的web.xml文件,Spring的核心配置文件applicationContext.xml,SpringMvc的配置文件
springmvc.xml,MyBatis的全局配置文件sqlMapConfig.xml
小的配置文件有:数据库连接文件db.proterties,日志文件log4j.properties或者log4j.xml
各个配置文件具体内容如下:
3,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- <display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> -->
<!-- 1,初始化SpringIOC容器的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2,Springmvc的前端控制器 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 3,字符编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 4,REST 过滤器(可以实现REST风格的URI) -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4,applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 1,组件扫描,扫描除了controller之外的(排除扫描) -->
<context:component-scan base-package="com.atguigu.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 2,数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3,事务 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--基于注解使用事务 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- 4,Spring 整合 Mybatis -->
<!-- a. SqlSession对象的创建 管理等 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- Mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<!-- Mybatis的SQL映射文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.atguigu.ssm.bean"></property>
</bean>
<!-- b. Mapper接口代理实现类对象的创建 管理等
MapperScannerConfigurer 会为指定包下的Mapper接口生成代理实现类对象并管理到IOC容器中.
EmployeeMapper ==>代理实现类 ==>对象 :对象在IOC容器中的id: employeeMapper
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.atguigu.ssm.mapper"></property>
</bean>
<!-- b. Mapper接口实现类代理对象的创建 管理等(这是一种新的实现方式,是mybatis-spring整合包里面为我们配置的) -->
<!-- <mybatis-spring:scan base-package="com.atguigu.ssm.mapper"/> -->
</beans>
5,springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 1,组件扫描,只扫描controller(指定扫描) -->
<context:component-scan base-package="com.atguigu.ssm" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 2,视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 3,两个mvc标配 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
6,sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 1. settings: 全局参数配置,包含了很多重要的设置项 -->
<settings>
<!-- 映射下划线到驼峰命名,驼峰命名法 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 配置按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
</configuration>
7,db.proterties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
8,以上是主要的配置,还需要编写各个层
src,config目录结构,
ssm
src
com.atguigu.ssm.bean
实体类
com.atguigu.ssm.mapper
Mapper.java
数据持久层
com.atguigu.ssm.service
业务逻辑层
com.atguigu.ssm.controller
表现层
com.atguigu.ssm.test
测试
config
mapper
Mapper.xml
applicationContext.xml
db.properites
log4j.xml
springmvc.xml
sqlMapConfig.xml
9,整合的大部分配置就在上面了,大家可以看看注释理解理解,等到熟练以后就可以直接Copy。
同时我也写了一个具体的SSM整合工程实例,大家可以在我的CSDN上面下载,里面有一个工程源码,
有jar包资源,有整合步骤(很详细易懂),同时也附上了MyBatis官方给我们提供的整合实例地址,
为了大家更好的理解,里面有一套SSM整合视频供大家观看。
===========================================================================
更多疑问,大家可以加我QQ:2040423570,一起交流分享。