Simplified configuration and content sublimation of spring boot and spring mvc.
How to integrate mybatis in spring boot?
Part 1: maven dependency (you can check it when creating the spring boot project)
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency>
Part 2: configure mybatis in application.yml
mybatis: type-aliases-package: com.example.demo.entity mapper-locations: classpath:mapper/*.xml
Yes, that's how simple it is to complete the configuration. By now, mybatis has completely abandoned its own core configuration file.
Part 2: mybatis has an enhanced framework, mybatis plus, which we can integrate.
The first one, of course, is to add dependency
<!--Mybatis-Plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.2.0</version> </dependency>
Then use @ Configuration to add Configuration
@Configuration @EnableConfigurationProperties(MybatisProperties.class) public class MyBatisPulsConfig { @Autowired private DataSource dataSource; @Autowired private MybatisProperties properties; @Autowired @SuppressWarnings("unused") private ResourceLoader resourceLoader = new DefaultResourceLoader(); @Autowired(required = false) private Interceptor[] interceptors; @Autowired(required = false) private DatabaseIdProvider databaseIdProvider; @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); paginationInterceptor.setDialectType("mysql"); return paginationInterceptor; } @Bean public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() throws IOException{ MybatisSqlSessionFactoryBean mybatisPuls = new MybatisSqlSessionFactoryBean(); mybatisPuls.setDataSource(dataSource); // set up data sources mybatisPuls.setVfs(SpringBootVFS.class); // Use pathmatchingresourcepratternresolver to avoid path not found PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); mybatisPuls.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); if(StringUtils.hasText(this.properties.getConfigLocation())); if(!ObjectUtils.isEmpty(this.interceptors)) { mybatisPuls.setPlugins(this.interceptors); } GlobalConfiguration globalConfig = new GlobalConfiguration(); globalConfig.setDbType(DBType.MYSQL.name()); //Set database type //Use ID work STR, because the front and back ends are separated and use shaping, the front JS will lose precision globalConfig.setIdType(IdType.ID_WORKER_STR.getKey()); globalConfig.setSqlInjector(new AutoSqlInjector()); MybatisConfiguration mc = new MybatisConfiguration(); // For a fully customized mapper, you need to add this configuration to realize underline to hump mc.setMapUnderscoreToCamelCase(true); mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class); mybatisPuls.setConfiguration(mc); if(this.databaseIdProvider != null) { mybatisPuls.setDatabaseIdProvider(this.databaseIdProvider); } if(StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { mybatisPuls.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); } if(StringUtils.hasLength(this.properties.getTypeHandlersPackage())) { mybatisPuls.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); } if(!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) { mybatisPuls.setMapperLocations(this.properties.resolveMapperLocations()); } return mybatisPuls; } }