1.配置web.xml
//注册Spring的全局配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/application.xml</param-value>
</context-param>
//ContextLoaderListener监听器注册ApplicationContext
<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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispacherServelt</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/dispacherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispacherServelt</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2.配置Spring配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
//数据库连接池
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- SqlsessionFactory-->
//Session工厂
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:conf/mybatis.xml"/>
</bean>
//Mapper映射文件
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.bdj.dao"/>
</bean>
//注解扫描
<context:component-scan base-package="com.bdj.service"/>
</beans>
3.配置DispacherServlet调度器
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
//扫描Controller注解
<context:component-scan base-package="com.bdj.controller"/>
//视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
//开启注解驱动
<mvc:annotation-driven/>
</beans>
SSM整合分析: Spring负责整合Mybatis Springmvc 感觉就像个大树干或者像个箱体 将其他散件整合到一起形成特定的功能 Spring和SpringMVC是父子容器 不需要整合就可以直接用Spring中的东西 Spring只用整合Mybatis即可.
SpringMVC执行流程:
从页面发来请求交给DispacherServlet处理 DispacherServlet将请求先交给HandleMapping处理器映射器,找到能处理该请求的处理器并将其封装为处理器执行链也就是拦截器和Controller的执行链又return给DispacherServlet,DispacherServlet根据处理器执行链中的处理器找到能执行该处理器的处理器适配器.HandleAdaptor处理适配器器执行Controller的方法并返回一个视图,中央调度器又将返回的视图交给视图解析器赋值路径 最终中央调度器调用视图完成响应.
下面是简单的CRUD
Controller层
public class MyController {
@Resource
StudentService ss;
@RequestMapping("/search.do")
@ResponseBody
public List search(){
List<Student> list=ss.search();
return list;
}
@RequestMapping("/update.do")
public ModelAndView update(Student student){
int result=ss.update(student);
ModelAndView mv=new ModelAndView();
if(result>=1){
mv.setViewName("success");
}else{
mv.setViewName("failed");
}
return mv;
}
@RequestMapping("/delete.do")
public ModelAndView delete(String name){
System.out.println(name);
int result=ss.deleteByName(name);
ModelAndView mv=new ModelAndView();
if(result>=1){
mv.setViewName("success");
}else{
mv.setViewName("failed");
}
return mv;
}
@RequestMapping("/insert.do")
public ModelAndView insert(Student student){
int result=ss.insert(student);
ModelAndView mv=new ModelAndView();
if(result>=1){
mv.setViewName("success");
}else{
mv.setViewName("failed");
}
return mv;
}
}
service层
import com.bdj.dao.StudentDao;
import com.bdj.domain.Student;
import com.bdj.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
StudentDao studentDao;
public int insert(Student student) {
int result=studentDao.insert(student);
return result;
}
public List<Student> search() {
List<Student> list=new ArrayList<>();
list=studentDao.search();
return list ;
}
public int deleteByName(String name) {
int result=studentDao.deleteByName(name);
return result;
}
public int update(Student student) {
int result=studentDao.update(student);
return result;
}
}
dao层
<?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="com.bdj.dao.StudentDao">
<select id="search" resultType="Student">
select id,name,age from student
</select>
<delete id="deleteByName">
delete from student where name=#{name}
</delete>
<insert id="insert">
insert into student(id,name,age)values (
#{id},#{name},#{age})
</insert>
<update id="update">
update student set name=#{name},age=#{age} where id=#{id}
</update>
</mapper>