Spring整合MyBatis纯注解开发

19 篇文章 5 订阅
12 篇文章 2 订阅

为什么要学习mybatis-spring注解版开发呢?可以帮助我们了解到mybatis在spring-boot中自动装配做了什么事情。

1:环境搭建

maven依赖:

<dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <!-- mybatis 基础包依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>

        <!-- mybatis spring包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

    </dependencies>

配置类

@Configuration
/**
 * Spring 扫包
 */
@ComponentScan({"com.liuqi"})

/**
 * 启用 spring 事务
 */
@EnableTransactionManagement

/**
 *引入 properties资源文件
 */
@PropertySource(value = "mysql.properties")

/**
 * Mapper接口扫包
 */
@MapperScan({"com.liuqi.mapper"})
public class MeMyBatisConfig {

    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    /**
     * 注入数据源
     *
     * @return {@link PooledDataSource} MyBatis提供的
     */
    @Bean
    public DataSource dataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    /**
     * 注入一个 SqlSessionFactoryBean
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);

        /* Spring 资源解析器 */
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        /* 设置 mapper.xml 所在的路径 */
        factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis-mapper/**/*.xml"));

        return factoryBean.getObject();
    }

    /**
     * 注入一个 SqlSessionTemplate 防止创建 Mapper接口代理的时候频繁创建 SqlSessionTemplate实例
     * @param sqlSessionFactory
     * @return
     */
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /**
     * spring 事务
     * @param dataSource
     * @return
     */
    @Bean
    public PlatformTransactionManager platformTransactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}

Mapper接口,Mapper.XML,Service这些就不写了。效果演示
在这里插入图片描述


  • 首先我们需要注入一个数据源。
  • 通过SqlSessionFactoryBean创建一个SqlSessionFactory,注入到容器。
  • 利用SqlSessionFactory去注入一个SqlSessionTemplate。
  • 用同一个数据源注入PlatformTransactionManager事务管理器。

SqlSessionFactoryBean,用于创建SqlSessionFactory,基本配置

  • setDataSource 设置数据源。
  • setConfigLocation 设置config.xml资源文件。
  • setTypeAliases 注册别名。
  • setCache 设置三级缓存。
  • setPlugins 设置插件。
  • setMapperLocations 设置 mapper.xml资源路径。
  • setTransactionFactory 设置事务管理,在Spring环境下不要设置,否则不会使用spring的事务管理。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Me_Liu_Q

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

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

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

打赏作者

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

抵扣说明:

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

余额充值