SpringBoot整合mybatis 原

dataSource配置文件

#jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/yudian_user?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
jdbc.maxActive=2335
jdbc.maxIdel=120
jdbc.maxWait=100

DataSourceConfig配置

package com.yudian.springboot.mybatis;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
//这个注解导入刚才增加的jdbc配置文件
@PropertySource("classpath:datasource.properties")
public class DataSourceConfiguration {
  @Value("${jdbc.driver}")
  private String driver;
  @Value("${jdbc.url}")
  private String url;
  @Value("${jdbc.username}")
  private String username;
  @Value("${jdbc.password}")
  private String password;
  @Value("${jdbc.maxActive}")
  private int maxActive;
  @Value("${jdbc.maxIdel}")
  private int maxIdel;
  @Value("${jdbc.maxWait}")
  private long maxWait;

  
  @Bean
  public BasicDataSource dataSource(){
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName(driver);
      dataSource.setUrl(url);
      dataSource.setUsername(username);
      dataSource.setPassword(password);
      dataSource.setMaxActive(maxActive);
      dataSource.setMaxIdle(maxIdel);
      dataSource.setMaxWait(maxWait);
      dataSource.setValidationQuery("SELECT 1");
      dataSource.setTestOnBorrow(true);
      return dataSource;
  }
}

MybatisConfig

package com.yudian.springboot.mybatis;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;


@Configuration
//加上这个注解,使得支持事务
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {
    @Autowired
    private DataSource dataSource;

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
         return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
        	     bean.setMapperLocations(resolver.getResources("classpath*:com/yudian/springboot/mapper/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏程序员笔记

Note_Motivation & Gamification

2186
来自专栏技术小黑屋

Package Stopped State Since Android 3.1

Since Android 3.1, Android has introduced a LaunchControl mechanism. It’s call S...

971
来自专栏后端之路

springboot的mvc测试

背景 写代码中必然有一环是测试代码的书写 以前我们写代码的测试基本是针对Service这一层,这样有可能会遗漏部分Controller代码 为了和最终用户完全相...

3196
来自专栏技术小黑屋

Issues About Installing Octopress

Actually I am fresh to Write Blog with Octopress in Github Pages.According to th...

1102
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1372
来自专栏WindCoder

How to Build an Effective Initial Deployment Pipeline

I love building things—what developer doesn’t? I love thinking up solutions to i...

791
来自专栏everhad

转载:Package by feature, not layer

The first question in building an application is "How do I divide it up into pac...

1210
来自专栏一个会写诗的程序员的博客

Caused by: android.os.NetworkOnMainThreadException

Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$And...

1172
来自专栏xingoo, 一个梦想做发明家的程序员

[Hadoop大数据]——Hive部署入门教程

Hive是为了解决hadoop中mapreduce编写困难,提供给熟悉sql的人使用的。只要你对SQL有一定的了解,就能通过Hive写出mapreduce的程...

4725
来自专栏运维

U盘安装FreeBSD8.2+Panabit安装配置+Samba文件服务器+DNS缓存服务器

  dd if=FreeBSD-8.2-RELEASE-i386-memstick.img of=/dev/sdb bs=10240 conv=sync 二,安...

1412

扫码关注云+社区