(注解版)spring整合 mybatisPlus
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
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;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@PropertySource("classpath:druid.properties")
@EnableTransactionManagement
@MapperScan(basePackages = "org.neuedu.boot.mapper")
@ComponentScan("org.neuedu.boot.service.impl")
public class ApplicationMybatisPlusConfig {
@Value("${driver}")
private String driverClassName;
@Value("${url}")
private String url;
@Value("${user}")
private String userName;
@Value("${pwd}")
private String password;
@Bean("dataSource")
public DruidDataSource druidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(DruidDataSource dataSource){
MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setPlugins(new PaginationInterceptor());
return factory;
}
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
@Bean
public DataSourceTransactionManager dataSourceTransactionManager(DruidDataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}