SpringBoot整合mybatis
SpringBoot整合mybatis
小兵成长记 发表于10个月前
SpringBoot整合mybatis
  • 发表于 10个月前
  • 阅读 42
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

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);
    }
}

 

共有 人打赏支持
粉丝 67
博文 57
码字总数 24043
×
小兵成长记
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: