zoukankan      html  css  js  c++  java
  • Spring整合MyBatis

    1.导入jar包

      mybatis-spring.jar和mybatis包、spring的基本包

    2.编写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"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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.xsd">
        
        <!-- 数据源封装类  数据源:获取数据库连接-->
        <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"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>
        <!-- 创建SqlSessionFactory对象 -->
        <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 扫描器相当于mybatis.xml中mappers下package标签,扫描com.mxj.mapper包后会给对应接口创建对象 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 要扫描哪个包 -->
            <property name="basePackage" value="com.mxj.mapper"></property>
            <!-- 和factory产生联系 -->
            <property name="sqlSessionFactory" ref="factory"></property>
        </bean>
        
        <!-- 由spring管理service实现类 -->
        <bean id="airportService" class="com.mxj.service.impl.AirportServiceImpl">
            <property name="airportMapper" ref="airportMapper"></property>
        </bean>
    </beans>

    3编写代码

      (1)正常编写pojo

    public class Airport {
        private int id;
        private String portName;
        private String cityName;
    }

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

    public interface AirportMapper {
        @Select("select * from airport")
        List<Airport> sellAll();
    }
      (3)正常编写Service接口和实现类(需在Service实现类中声明Mapper接口对象,并生成get/set方法)
    public interface AirportService {
        List<Airport> show();
    }
    public class AirportServiceImpl implements AirportService{
        private AirportMapper airportMapper;
        
        public AirportMapper getAirportMapper() {
            return airportMapper;
        }
    
        public void setAirportMapper(AirportMapper airportMapper) {
            this.airportMapper = airportMapper;
        }
    
        @Override
        public List<Airport> show() {   
            return airportMapper.sellAll();
        }
    }

      (4)Spring无法管理Servlet,需要用到SpringMVC

     
     
     
     
     
  • 相关阅读:
    LeetCode 654. 最大二叉树
    LeetCode 617. 合并二叉树
    LeetCode 234. 回文链表
    LeetCode 328. 奇偶链表
    LeetCode 24. 两两交换链表中的节点
    LeetCode 21. 合并两个有序链表
    LeetCode 876. 链表的中间结点
    顺序表的定义及其相关基本操作
    LeetCode 206. 反转链表
    LeetCode 111. 二叉树的最小深度
  • 原文地址:https://www.cnblogs.com/mxj961116/p/11264248.html
Copyright © 2011-2022 走看看