【Spring】【SpringBoot】【MyBatis】一篇文章带你理解Spring/SpringBoot框架整合MyBatis框架的实质

时间:2024-11-14 11:22:52

Spring其实就是一个容器,管理所有注册到其中的bean,并通过IOC机制注入依赖。MyBatis是一个持久层框架,这个框架的作用就是规范Java与数据库的交互。

MyBatis框架中,与数据库的交互通过如下步骤。

  1. 读取配置文件
  2. 根据配置文件通过SqlSessionFactoryBuilder对象创建sqlSessionFactory对象。
  3. 调用sqlSessionFactoryopenSession()获得sqlSession
  4. 调用sqlSessiongetMapper()方法并传入接口类型获取这个接口类型的代理对象。
  5. 通过代理对象去执行该接口的配置文件中的SQL语句。

总结下来,MyBatis框架所必须的文件就是配置文件、与数据库表结构对应的实体bean、接口interface及对应的xml配置文件。

在Spring框架中,基本思想是通过Spring容器直接获得装配好的实体bean。在文件中配置相应的实体bean并将依赖注入,然后根据配置文件创建context上下文,由上下文通过传入idclass直接获取实体bean。

Spring和MyBatis整合后,显然不能再通过MyBatis框架中的方式获得sqlSession,那么一个解决方案就是在*中设置一个属性为sqlSession,并在*中通过()获得代理对象,代理对象执行*配置的SQL。其实就是把获得SqlSession和通过getMapper()方法获得代理对象的步骤进行封装,封装到不同业务的代码中。这样从Spring中拿到对象后,执行CRUD时,实际上是由UserMapper的代理对象执行的。

可以把中的配置移动到Spring的配置文件中,并在Spring中配置Factory和SqlSession。

<!--Spring管理DataSource,即Spring管理与数据库的配置-->
    <bean id="dataSource" class="">
        <property name="driverClassName" value=""/>
        <property name="url" value=""/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:"/>
        <property name="mapperLocations" value="classpath:"/>
    </bean>

    <!--sqlSessionTemplate-->
    <bean id="sqlSession" class="">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

又因为接口中不能写方法的具体实现,因此可以通过一个接口的实现类*,并在Spring配置文件中注册这个实现类,当然需要进行sqlSession属性的依赖注入。当从Spring容器取用UserMapper时也是取用的*对应的对象。而这个对象的CRUD方法中都封装了获得代理对象(即getMapper()方法)。因此可以顺利实现MyBatis框架的精华。

public class UserMapperImpl implements UserMapper{

    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> getUserList() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.getUserList();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这里还有另一种方法,就是继承Spring框架已经编写好的SqlSessionDaoSupport类,这个类中有sqlSession对象,可以通过getSession()方法获得这个对象。这样不需要进行依赖注入。

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{

    public List<User> getUserList() {
        UserMapper mapper = getSession().getMapper(UserMapper.class);
        return mapper.getUserList();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

值得注意的是,为了更规范,可以将Spring的配置文件分为,在中import,并专注配置bean。这里的就可以理解为这个Spring容器的特性,在创建context上下文的时候,就导入这个配置文件。

其实这里的流程是十分繁琐的,而且每创建一个*文件,首先必需的是一个*文件用来配置namespace和SQL语句,其次还需要这个*文件,整个文件的数量呈三倍增加。因此将创建*来获得sqlSession对象和代理对象的步骤简化为一个动态扫描的过程。

配置文件中,添加如下配置。

<bean class="">
	<!--这里的sqlSessionFactory需要在上文中配置好-->
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	<!--路径是Mapper接口包的路径-->
	<property name="basePackage" value=""/>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

配置好了之后,就不需要再去创建*文件及注册到Spring容器中了。直接从Spring容器中获得就可以了。

SpringBoot框架也是建立在Spring容器基础上的,只不过在SpringBoot框架中,配置被大大简化。SpringBoot框架中,整合MyBatis的配置如下。

mybatis:
	type-aliases-package: 
	mapper-locations: classpath:mybatis/mapper/*.xml
  • 1
  • 2
  • 3