Spring MyBatis 简单整合

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 封装了数据连接数据库所需的信息,相当于myBatis.xml中的dataSource标签 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8"></property>
        <property name="username" value="root"></property>    
        <property name="password" value=""></property>
    </bean>
    <!-- 创建SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 注入dataSource连接信息 -->
        <property name="dataSource">
            <ref bean="dataSource"></ref>
        </property>
    </bean>
    <!-- 扫描器,相当于myBatis中<mappers>下的package标签 -->
    <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描包 -->
        <property name="basePackage" value="com.spring.mapper"></property>
        <!-- 与sqlSessionFactory关联 -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    <!-- 向Service层注入接口对象 -->
    <bean id="airPortMapperService" class="com.spring.service.impl.AirPortServiceImpl">
        <property name="airPortMapper" >
            <ref bean="airPortMapper"></ref>
        </property>
    </bean>

Service层实现类:

public class AirPortServiceImpl implements AirPortService{
    //该类将由Spring进行管理,airportMapper对象由Spring进行注入
    AirPortMapper airPortMapper ;
    public AirPortMapper getAirPortMapper() {
        return airPortMapper;
    }
    public void setAirPortMapper(AirPortMapper airPortMapper) {
        this.airPortMapper = airPortMapper;
    }
    @Override
    public List<AirPort> selectAll() {
        return airPortMapper.selectAll();
    }
}

Web配置文件:

<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>
    <listener>
        <!-- 监听器,帮助Spring读取配置文件 -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>AirPortServlet</servlet-name>
        <servlet-class>com.spring.servlet.AirPortServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AirPortServlet</servlet-name>
        <url-pattern>/show</url-pattern>
    </servlet-mapping>
</web-app>

Servlet:

public class AirPortServlet extends HttpServlet{
    AirPortService airPortSerivce;
    @Override
    //实例化,获得tomcat启动时加载的spring配置文件的对象
    public void init() throws ServletException {
        /**
         * 第一种写法:需要在web.xml配置文件中加载Spring配置文件
         * spring配置文件的信息封装在WebApplicationContext中
         */
        //WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        //Service对象由Spring管理,获取Service层对象
        //airPortSerivce = wac.getBean("airPortMapperService",AirPortServiceImpl.class);
        
        /**
         * 第二种写法:直接读取Spring配置文件
         */
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        airPortSerivce = applicationContext.getBean("airPortMapperService",AirPortServiceImpl.class);
        
    }
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("list",airPortSerivce.selectAll());
        request.getRequestDispatcher("/index.jsp").forward(request, response);;
    }
}

 

posted @ 2018-11-19 22:30  caiJava  阅读(167)  评论(0编辑  收藏  举报