基本概念
1.1、Spring
Spring是一个轻量级的Java开源框架。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情,是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
1.2、SpringMVC
SpringMVC属于SpringFrameWork的后续产品。SpringMVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
1.3、MyBatis
MyBatis是一个基于Java的持久层框架。MyBatis使用简单的 XML或注解用于配置和原始映射,将接口和Java 的POJOs(普通的 Java对象)映射成数据库中的记录。
配置文件解析:
1、web.xml
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!—加载页面的列表-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!—配置文件路径,src名称-->
<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>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!—配置spring_mvc文件路径,src名称-->
<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>100</load-on-startup>
</servlet>
<!—拦截以do.结尾的web请求-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2、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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.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">
<!-- 自动扫描(自动注入) -->
<context:component-scan base-package="org.jit.ssm.service.impl" />
<!-- 数据源 : DriverManagerDataSource ,配置数据源,连接到数据库,其中默认端口号3306是可以自己改的,注意一定要加入mysql-connector的jar包-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://120.79.56.217:3306/archiveManagement?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username"value="root" />
<property name="password"value="root" />
</bean>
<!-- . mybatis的SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource"ref="dataSource" />
<property name="mapperLocations"value="classpath:org/jit/ssm/mapping/*.xml"/>
<property name="plugins">
<!-- 配置分页插件 -->
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- mybatis自动扫描加载Sql映射文件 ,扫描目录是org.jit.ssm.mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"value="org.jit.ssm.mapper" />
<property name="sqlSessionFactory"ref="sqlSessionFactory" />
</bean>
</beans>
3、springmvc-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件
注意Spring版本,如果是4的话,对应MappingJackson2HttpMessageConverter -->
<bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!-- json转换器 -->
<ref bean="mappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"value="/"></property>
<property name="suffix"value=".html"></property>
<property name="contentType"value="text/html"/>
</bean>
<!-- 配置注解驱动 -->
<mvc:annotation-driven />
<!-- 启动自动扫描 -->
<!--配置@Controller注解扫描器 -->
<context:component-scan base-package="org.jit.ssm.controller"></context:component-scan>
<!-- 文件上传配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding"value="UTF-8"></property>
<property name="maxUploadSize"value="5400000"></property>
<property name="maxInMemorySize"value="4096">
</property>
</bean>
<!--国际化配置 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的文件名 -->
<property name="basename"value="messages" />
</bean>
<!-- 异常处理 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="Exception">error</prop>
</props>
</property>
</bean>
</beans>
4、studentMapper.xml
mybits的映射文件,再写一个类Student,实现IStudentMapper接口。
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTDMapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.jit.ssm.mapper.IStudentMapper">
<!—此处id需与自己定义的方法名保持一致-->
<select id="getAllStudent"resultType="org.jit.ssm.entity.Student">
SELECT * FROM student
</select>
</mapper>