1、前言
SSM框架是企业中常用的框架,是SpringMVC+Spring+Mybatis框架的整合。框架的的核心思想就是将各个独立的框架都交给spring容器进行管理。
2、SSM框架需要的依赖(pom.xml)
<!--
主要包含六部分内容:
springmvc子容器的依赖(包含了spring容器的依赖),以及数据处理的依赖;
mybatis以及和spring容器整合的依赖
数据库相关依赖(数据库驱动,数据库连接池)
事务相关依赖(jdbc和AOP联盟)
JSTL相关依赖以及测试工具依赖
日志依赖
-->
<dependencies>
<!-- 配置springmvc相关的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!-- 配置controller处理数据的jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<!-- 配置mybatsi依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- 配置spring和mybatis的依赖包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 配置mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!-- 配置c3p0数据库连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
<!-- 配置事物相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!-- 配置事务的切面依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<!-- 配置测试工具 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<!-- 解析jsp页面需要的依赖 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- 添加日志的依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
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_2_5.xsd" version="2.5">
<!-- 配置spring容器:spring核心监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置全局过滤器 -->
<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>
<!-- 配置springmvc容器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 默认欢迎页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4、父容器的整合配置applicationContext.xml
<!-- 启动注解扫描包,获取bean -->
<context:component-scan base-package="ws.spring.mybatis.service" />
<!-- 引入数据源 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注入数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
5、事务的配置applicationContext-tx.xml
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 定义事务策略、通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有以query开头的方法都是只读的 -->
<tx:method name="query*" read-only="true" />
<!-- 其他方法使用默认事务策略 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- pointcut元素定义一个切入点,execution中的第一个星号 用以匹配方法的返回类型,
这里星号表明匹配所有返回类型。 com.abc.dao.*.*(..)表明匹配ws.spring.mybatis.service包下的所有类的所有方法 -->
<aop:pointcut id="methodPointcut" expression="execution(* ws.spring.mybatis.service.*.*(..))"/>
<!-- 将定义好的事务处理策略应用到上述的切入点 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="methodPointcut" />
</aop:config>
6、spring和mybatis的整合配置:applicationContext-mybatis.xml
<!-- 配置sessionFatory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 读取mybatis的配置文件 -->
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml" />
<!-- 读取SQL的映射文件 -->
<property name="mapperLocations" value="classpath:/mybatis/mapper/*.xml" />
<!-- 别名扫描包 -->
<property name="typeAliasesPackage" value="ws.spring.mybatis.pojo" />
</bean>
<!-- 配置接口扫描包:用来指定mapper映射的 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="ws.spring.mybatis.mapper" />
</bean>
7、配置子容器:springmvc-servlet.xml
<!-- 开启注解扫描:获取handler -->
<context:component-scan base-package="ws.spring.mybatis.controller"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 将静态资源交给tomcat处理: 非必要配置-->
<mvc:default-servlet-handler/>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
8、mybatis-config.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">
<!-- 主要用来配置mybatis的相关设置 -->
configuration>
<settings>
<!-- 开启驼峰命名规则 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
9、日志配置:log4j.properites
log4j.rootLogger=INFO,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=INFO
log4j.appender.console.ImmediateFlush=true
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%p] %d %C: %m%n
10、mapper映射的配置
<!-- 具体的内容根据实际代码配置 -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ws.spring.mybatis.mapper.UserMapper">
<insert id="insertUser" parameterType="User">
insert into user_info values(#{id},#{name},#{age},#{sex},#{job})
</insert>
<select id="queryAllUser" resultType="User">
select * from user_info
</select>
<select id="queryById" resultType="User" parameterType="java.lang.Integer">
select * from user_info where id = #{id}
</select>
</mapper>
11、数据库的配置文件:jdbc.properties
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
12、包结构以及controller
13、根据配置访问即可
http://127.0.0.1:8080