import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
public class MyBatisConfig {
@Bean
public SqlSessionFactory newSqlSessionFactoryBean(DataSource dataSource) throws Exception {
//需要使用MybatisSqlSessionFactoryBean 而不是 SqlSessionFactoryBean
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
//配置完下面这两个在 application.properties 就不需要配置了
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/*Mapper.xml"));
bean.setTypeAliasesPackage("com.gc.modular.entity.model");
//configuration 需要使用 MybatisConfiguration 如果使用 org.apache.ibatis.session.Configuration 也有可能会报错
MybatisConfiguration configuration = new MybatisConfiguration();
// 返回类型Map时字段为空也要返回字段名称
configuration.setCallSettersOnNulls(true);
bean.setConfiguration(configuration);
return bean.getObject();
}
}