一:导入jar包

导入Mybatis所有jar包和Spring基本包,spring-jdbc,spring-tx,spring-aop,spring-web,如下图:

Spring整合Mybatis

二:web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <!--上下文参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!--Spring配置文件-->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--封装了一个监听器,帮助加载Spring配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

三:编写Spring配置文件

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--数据源封装类. 数据源:获取数据库连接,在spring-jdbc.jar中-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/ssm?characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="yechaoze"/>
    </bean>

    <!--创建SqlSessionFactory对象-->
    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--数据库连接信息来源于dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 扫描器相当于mybatis.xml中mappers下package标签,
    扫描com.mapper包后会给对应接口创建对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 要扫描哪个包 -->
        <property name="basePackage" value="com.mapper"></property>
        <!--和SqlSessionFactory产生关系-->
        <property name="SqlSessionFactory" ref="factory"></property>
    </bean>

    <!--- 由 spring 管理 service 实现类-->
    <bean id="airportService" class="com.service.impl.AirportServiceImpl">
        <property name="airportMapper" ref="airportMapper"></property>
    </bean>
</beans>

四:编写代码

4.1 正常编写pojo

4.2 编写mapper包下时必须使用接口绑定方案或注解方案(必须有接口)

4.3 正常编写Service接口和Service实现类

需要在Service实现类中声明Mapper接口对象,并生成get/set 方法

4.4 Spring无法管理Servlet,在Service中取出Servie对象

package com.servlet;

import com.service.AirportService;
import com.service.impl.AirportServiceImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "airportServlet",urlPatterns = "/airport")
public class airportServlet extends HttpServlet {
    private AirportService airportService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        //spring和web整合后所有信息都存放在WebApplicationContext
        WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
         airportService = wc.getBean("airportService", AirportServiceImpl.class);
    }
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("list",airportService.show());
        req.getRequestDispatcher("/index.jsp").forward(req,resp);
    }
}

For sharing , For emulating , For enterprising