mybatisplu核心配置
package com.entor.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.entor.mapper")
@ComponentScan(basePackages = "com.entor")
@PropertySource(value = "classpath:db.properties")
@EnableTransactionManagement
public class SpringMybatisConfig {
@Value("${entor.driver}")
private String driver;
@Value("${entor.url}")
private String url;
@Value("${entor.username}")
private String username;
@Value("${entor.password}")
private String password;
@Value("${entor.mapperLocations}")
private String mapperLocations;
@Value("${entor.typeAliasesPackage}")
private String typeAliasesPackage;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public TransactionManager transactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(patternResolver.getResources(mapperLocations));
bean.setTypeAliasesPackage(typeAliasesPackage);
MybatisConfiguration config = new MybatisConfiguration();
config.setMapUnderscoreToCamelCase(true);
config.setLogImpl(StdOutImpl.class);
config.setCacheEnabled(true);
bean.setConfiguration(config);
bean.setPlugins(new PageInterceptor());
return bean.getObject();
}
}
mybatis 是在