Spring整合Mybatis方式一 - 常规整合 - 注册映射器

前置工作

  • 导包(mybatis-spring、mysql-connector-java、mybatis、spring-webmvc等)

  • 实体类

  • DAO层两个文件(接口、xml文件);Service层的接口

编写Spring管理mybatis的xml-spring-dao.xml

核心代码(两种方式实现)

第一种:xml

<!-- 将会话工厂对象托管给spring -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <!--  配置MyBatis的全局配置文件:mybatis-config.xml -->
  <property name="configLocation" value="classpath:mybatis-config.xml"/>
  <property name="mapperLocations" value="classpath:com/ylzl/mapper/*.xml"/>
</bean>

<!-- 注册映射器:将映射器接口托管到Spring中 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  <property name="mapperInterface" value="com.ylzl.mapper.UserMapper" />
</bean>
<!--    MapperFactoryBean对象 负责 SqlSession 的创建和关闭,
如果使用了 Spring 事务,当事务完成时,session 将会被提交或回滚。
最终任何异常都会被转换成 Spring 的 DataAccessException 异常-->
<!--    mybatis映射器接口(如:interface UserMapper):sql部分可以使用mybatis的xml配置,与接口在同一路径下,会被 MapperFactoryBean自动解析-->

第二种:annotation方式

点击查看代码
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
  SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
  factoryBean.setDataSource(dataSource());
  return factoryBean.getObject();
}

@Bean
public MapperFactoryBean<UserMapper> userMapper() throws Exception {
    MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);
    factoryBean.setSqlSessionFactory(sqlSessionFactory());
    return factoryBean;
}

完整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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

<!--    使用Spring配置dataSource 相当于MyBatis配置文件的<environments>-->
<!--    需要spring-jdbc包-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ssmbuild"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

<!--    配置SqlSessionFactoryBean 等同于SqlSessionFactory
        做读取数据源以及注册mapper.xml的工作-->
<!-- SqlSessionFactoryBean会调用类中的getObject()方法,返回SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/ylzl/mapper/BookMapper.xml"/>
    </bean>
<!--    获得Mapper代理对象 等同于getMapper()-->
    <bean id="BookMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        <property name="mapperInterface" value="com.ylzl.mapper.BookMapper" />
    </bean>

<!--    注册employeeServiceImpl-->
    <bean id="bookServiceImpl" class="com.ylzl.service.impl.BookServiceImpl"/>
</beans>

重新编写Service实现类

public class BookServiceImpl implements BookService{
    private BookMapper bookMapper;

    @Autowired //需要spring-aop包
    public BookServiceImpl(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public Book getBookById(Integer bookID) {
        return bookMapper.getBookById(bookID);
    }
}

测试

ApplicationContext context=new ClassPathXmlApplicationContext("spring-dao.xml");
BookService bookServiceImpl = context.getBean("bookServiceImpl", BookService.class);
Book book = bookServiceImpl.getBookById(2);
System.out.println(book);

改进注册映射器方式:使用发现映射器方式

MapperFactoryBean注册映射器的最大问题,就是需要一个个注册所有的映射器,而实际上mybatis-spring提供了扫描包下所有映射器接口的方法。

注意:以下两种配置方法,均可替换上述MapperFactoryBean配置,而其余代码与配置不变

方式一: 配置扫描器标签

1.与上面配置MapperFactoryBean不同,该配置无需注入SqlSessionFactory,它会自动匹配已有的会话工厂bean

2.如果配置了多个DataSource,也就是多个sqlSessionFactory时,可以使用factory-ref参数指定需要的会话工厂

<mybatis:scan base-package="com.ylzl.dao" factory-ref="sqlSessionFactory" />

<!-- annotation方式-注解配置类:
@MapperScan(basePackages = "com.ylzl.dao", sqlSessionFactoryRef = "sqlSessionFactory") -->
    
<!-- 省略其他... -->

方式二: MapperScannerConfigurer类

1.与上面标签的功能差不多,同样是扫描基准包,自动注入会话工厂

2.如果要更换注入的会话工厂,不同于常用的ref引入bean,而是使用value指定bean名,且属性是sqlSessionFactoryBeanName

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="com.ylyl.mapper" />
     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

annotation方式

@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
    MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
    mapperScannerConfigurer.setBasePackage("com.ylzl.dao");
    mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
    return mapperScannerConfigurer;
}

图片:
p

posted @ 2024-03-27 17:01  御灵之灵  阅读(53)  评论(0编辑  收藏  举报