如何使用Spring整合Mybatis?

  • 因为有了 Spring,我们可以管理 Mybatis 中的对象,比如数据源、SqlSessionFactory

1、在 Spring 配置文件中配置数据源,首先引入外部数据源文件,源 properties 文件应放在 resources 文件夹下

<!--引入数据源文件-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="db.properties"/>
</bean>

2、配置数据源,这里我们使用 Spring 自带的,日后我们也能使用 c3p0 dbcp 和阿里的 druid

<!--DataSource:使用 Spring 的数据源替换 Mybatis 的数据源配置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${driver}"/>
    <property name="url" value="#{url}"/>
    <property name="username" value="#{root}"/>
    <property name="password" value="#{password}"/>
</bean>

3、让 Spring 来管理 SqlSessionFactory 的创建

<!--让 Spring 管理 mybatis 中的核心对象 SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--注入依赖对象 dataSource,ref 引用上文配置好的数据源-->
    <property name="dataSource" ref="dataSource"/>
</bean>

当然在此 bean 标签内还可以配置 Mybatis 的一些其他属性,比如别名,也可以绑定写好的 Mybatis 核心配置文件,这样 Mybatis 就与 Spring 连起来了

绑定写好的 Mybatis 核心配置文件的方法:

<property name="configLocation" value="classpath:mybatis-config.xml"/>

当然我们也可以完全舍弃 Mybatis 的核心配置文件,完全可以由 Spring 来管理

在 Spring 配置文件中注册 mapper :

<property name="mapperLocations" value="classpath:com/hy/dao/*.xml"/>

在 Spring 配置文件中配置 Mybatis 别名:

<property name="typeAliasesPackage" value="com.hy.dao"/>

但建议将 mapper 映射注册和别名配置放在 Mybatis 核心配置文件中 mybatis-config.xml:

<configuration>
    <typeAliases>
        <package name="com.hy.entity"/>
    </typeAliases>
    <mappers>
        <mapper class="com.hy.dao.UserMapper"/>
    </mappers>
</configuration>

4、用 Spring 来创建 SqlSession,因为 SqlSessionTemplate 没有 set 注入,因此使用构造器注入

Template 意为模板,使用它来代替创建 SqlSession 更佳

<bean id="SqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

5、创建 Mapper 接口的实现类 UserMapperImpl.java,实现接口方法来返回数据

public class UserMapperImpl implements UserMapper {
    private SqlSessionTemplate sqlSession;

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

    @Override
    public List<User> selectUser() {
        return sqlSession.getMapper(UserMapper.class).selectUser();
    }
}

6、在 Spring 配置文件中创建 UserMapperImpl 对象,并注入 SqlSessionTemplate

<bean id="userMapper" class="com.hy.dao.UserMapperImpl">
    <property name="sqlSession" ref="sqlSession"/>
</bean>

7、测试

@Test
public void test() {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
    List<User> users = userMapper.selectUser();
    for (User user : users) {
        System.out.println(user);
    }
}

8、文件结构如下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sadness°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值