画布 1
SSM整合
  spring容器整合进javaweb
    1.Listener启动加载spring容器：web.xml
      <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.创建spring容器配置文件applicationContext.xml
  springMVC整合进javaweb
    controller层集成
      1.servlet配置加载DispatcherServlet：web.xml
        <servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
      2.配置spring-mvc.xml对controller组件扫描
        <?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 http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="it.ssm.controller"/>
</beans>
      3.添加controller测试
        @Controller
@RequestMapping("/index")
public class index {
@ResponseBody
@RequestMapping("/show")
public String show(){
return "hello world";
}
}
    filter对字符utf-8编码
      配置web.xml中的filter
        <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>
    配置拦截器
      1.spring-mvc.xml中添加拦截器配置
        <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="it.ssm.interceptor.interceptor"/>
</mvc:interceptor>
</mvc:interceptors>
      2.添加拦截器类it.ssm.interceptor.interceptor
        public class interceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器运行..");
return true; //return false 表示不执行目标函数，return true表示向下执行目标函数
}
}
    文件上传配置
      1.pom文件添加依赖
        commons-io/commons-fileupload
      2.spring-mvc.xml 配置文件上传解析器
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
</bean>
      3.文件上传测试
        @Controller
@RequestMapping("/upload")
public class fileupload {
@ResponseBody
@RequestMapping("/index")
public String fileupload(MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
file.transferTo(new File("/tmp/"+originalFilename));
return "上传成功!";
}
}
  Mybatis整合进spring容器
    Mybatis集成
      1.配置spring容器配置文件applicationContext.xml
        <!--配置dataSource-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mytest"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置sqlSessionFactory,并且记载sqlmapper的配置文件(SQL语句)-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlmapper.xml"/>
</bean>
<!--自动扫描Dao层Mapper对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="it.ssm.dao"/>
</bean>
      2.创建sqlmapper.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>
<mappers>
<mapper resource="mapper/UserinfoMapper.xml"/>
</mappers>
</configuration>
      3.创建Dao层Interface接口
        package it.ssm.dao;
import it.ssm.domain.Userinfo;
import java.util.List;
public interface UserinfoDao {
public List<Userinfo> getUsers();
public Userinfo getUserById(int id);
}
      4.创建resources/mapper/UserinfoMapper.xml配置文件
        <?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="it.ssm.dao.UserinfoDao">
<select id="getUsers" resultType="it.ssm.domain.Userinfo">
select * from Userinfo where 1
</select>
<select id="getUserById" resultType="it.ssm.domain.Userinfo">
select * from Userinfo where id = #{id}
</select>
</mapper>
      5.测试it/ssm/controller/index.java
        @Controller
@RequestMapping("/index")
public class index {
@ResponseBody
@RequestMapping("/show")
public String show(){
ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserinfoDao bean = app.getBean(UserinfoDao.class);
List<Userinfo> users = bean.getUsers();
System.out.println(users);
return "hello world";
}
}
    plugins分页插件集成
      1.添加pom依赖
        <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
      2.sqlmapper.xml配置plugins
        <plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
      3.使用，在调用sql查询前设置即可
        PageHelper.startPage(1,1);