一、项目执行机制:
项目启动 ——> 读取web.xml文件 ——> 读取springMvc.xml和spring.xml文件 ——> 由配置文件扫描包 ——> 找到相关类执行业务逻辑
二、SSM所需jar
spring:beans、core、context、experssion、aop、aspects、tx、test、jdbc spring日志:commons-lang、commongs-logging springMvc:web、webmvc mybatis:mybatis、mysql-connector-java、mybatis-spring aop特殊需要:aopalliance、aspectjweaver 分页插件:pageHelper、jsplparser json格式:jackson-annotations、jackson-core、jackson-databind、fastjson 文件上传/下载:commons-fileupload、commont-io 数据库连接池:druid 个人编写用来做验证码jar:kaptcha
三、SSM常用注解
//一、@Controller注解有两种作用:
//1、让spring管理这个类的对象,对象名为:userController
//2、让这个类变成控制器,可以接收请求
@Controller
public class UserController {//这是什么
@Autowired
private UserServiceI userServiceI;
@Autowired
private ResultMap resultMap;
}
//二、@Service注解:让spring管理这个类的对象,对象名为:userServiceImpl,并且将这个对象注入到UserServiceI
@Service
public class UserServiceImpl implements UserServiceI{//这是什么层
@Autowired//这个注解的作用是自动装配,给接口注入的值通常为接口实现类
private UserDao userDao;
}
//三、@Repository注解:利用FactoryBean动态代理,将UserDao接口创建出一个代理对象
@Repository
public interface UserDao {
//接口中的抽象方法
}
//四、@Component注解:让spring管理整个类的对象,对象名为:resultMap
@Component("resultMap")
public class ResultMap {
//返回结果类
}
四、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" version="3.0">
<display-name>VideoSystem</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 用来监听Context容器的加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 启动项目向ApplicationContext中配置spring.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- 关与springMvc的配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<!-- 前端控制器:总调度,负责拿到所有的请求 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- springMvc.xml文件的路径 classpath:路径就代表项目中的resources文件夹-->
<param-value>classpath:springMvc.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>
<!-- 编码格式过滤器,所有请求都会经过这个过滤器 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 它是一个在web应用关闭的时候,清除JavaBeans Introspector的监听器.在web.xml中注册这个listener.可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class loader 和由它管理的类 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
</web-app>
五、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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描控制器,哪个类头上有@Controller,那么这个类就可以接收请求 -->
<context:component-scan base-package="com.wangc.controller"/>
<!-- 注册注解:@ResponseBody @RequestMapping @Controller -->
<mvc:annotation-driven/>
<!-- 过滤静态资源请求,例如:页面引入了 js/jquery.js,那么利用这个配置,就不会把页面引入的文件当作请求了 -->
<mvc:default-servlet-handler/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!--
如果项目中拥有jackson.jar,那么springMvc会借助@ResponseBody将返回结果自动转换为json,如果项目没有jackson.jar
如果项目用的fastjson.jar,那么需要特殊配置以下
利用jastjson.jar,将@ResponseBody返回结果自动转换为json
-->
<mvc:annotation-driven>
<!-- 安装FastJson,转换器 -->
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 声明转换类型:json -->
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 上传/下载解析器 id必须是:“multipartResolver”-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 最大可上传的文件大小 单位:byte 超出后会抛出MaxUploadSizeExceededException异常,可以异常解析器捕获 -->
<property name="maxUploadSize" value="104857600"></property>
</bean>
</beans>
六、spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- spring管理这些包下的类 -->
<context:component-scan base-package="com.wangc.controller"/>
<context:component-scan base-package="com.wangc.service"/>
<context:component-scan base-package="com.wangc.db.dao"/>
<context:component-scan base-package="com.wangc.utils"/>
<!-- 读取db.propertise文件内容 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- spring管理数据源dataSource对象,数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--
数据库连接池相关配置
项目只要启动,就向数据库连接池中注入initialSize=10个链接,这些连接都是待机状态,使用时不用创建对象而是直接从连接池中取出,使用结束后也不需要关闭连接,而是重新回归池中
-->
<property name="initialSize" value="10"/>
<property name="minIdle" value="10"/>
<property name="maxActive" value="50"/>
<property name="maxWait" value="60000"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
</bean>
<!-- 让spring管理mybatis核心对象sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 给sqlSessionFactory对象注入数据源dataSource属性值 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 给sqlSessionFactory对象注入给实体类起别名typeAliasesPackage属性值 -->
<property name="typeAliasesPackage" value="com.wangc.db.bean"></property>
<!-- 给sqlSessionFactory对象注入注册mapper.xml映射文件mapperLocations属性值 -->
<property name="mapperLocations" value="classpath:com/wangc/db/mapping/*.xml"></property>
<!-- 给sqlSessionFactory对象注入mybatis.xml核心配置文件configLocation属性值 -->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!-- spring管理事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 纯粹手动配置,跟任何的类、jar包都没有关系 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 只要方法名带有delete、save、update、insert,那么这些方法就开启了事务 -->
<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<!-- 只要方法名带有load、find、search、select、get,那么这些方法就不会开启事务 -->
<tx:method name="load*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--
因为dao层下所有的文件都是接口,接口的方法没有方法体,所以当数据请求结束后,dao接口方法无法提交或回滚事务
需要让service层的方法调用dao接口的方法,然后对事务进行提交或回滚
所以,利用面向切面编程将所有事务的管理,配置到service包下的所有类的所有方法中,只要方法名带有delete、save、update、insert,就会对事务自行处理
-->
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* com.wangc.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
<!-- 只有配置了以下内容后,才可以在Dao层接口上使用@Repository注解 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wangc.db.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
七、mybatis.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>
<plugins>
<!-- 利用pageHelper插件,实现分页查询 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
<!--
如果单独使用mybatis框架,此核心配置文件需要配置:
1、数据源
2、是否开启事务,事务的管理
3、扫描实体类,方便mapper.xml映射文件中parameterType或resultType利用别名
4、注册mapper.xml映射文件
如果使用的是spring管理mybatis,以上配置都放在了spring.xml配置文件中
-->
</configuration>