spring boot 及 spring mvc 的簡化配置及內容昇華版。前端
在 spring boot 中如何整和mybatis 呢?java
第一部:maven 依賴(能夠在建立 spring boot 項目是勾選中)mysql
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency>
第二部:在application.yml 中配置 mybatis spring
mybatis: type-aliases-package: com.example.demo.entity mapper-locations: classpath:mapper/*.xml
對就是這麼簡單就能夠完成配置。到如今已經徹底拋棄 mybatis 本身的核心配置文件了。sql
番外篇:mybatis 有一個加強版框架 mybatis-plus 咱們也能夠整和它。數據庫
第一部固然也是要添加依賴後端
<!--Mybatis-Plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.2.0</version> </dependency>
而後使用@Configuration 添加配置mybatis
@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); // 設置數據源 mybatisPuls.setVfs(SpringBootVFS.class); // 使用 PathMatchingResourcePatternResolver 避免路徑找不到 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()); //設置數據庫類型 //使用ID_WORKER_STR,由於先後端分離使用整形,前端JS會有精度丟失 globalConfig.setIdType(IdType.ID_WORKER_STR.getKey()); globalConfig.setSqlInjector(new AutoSqlInjector()); MybatisConfiguration mc = new MybatisConfiguration(); // 對於徹底自定義的mapper須要加此項配置,才能實現下劃線轉駝峯 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; } }