springboot-mybatis-MySQL-集成

       这也是我第一次搭建springboot-mybatis的项目环境,记录一下。我是用IntelliJ IDEA,你可以创建maven项目, 也可以直接创建spring项目,最终的项目结构如下,这里说明下,resources下面的mappers里面是存放mybatis的SQL映射文件,static下面存放前端静态资源文件,如js,css等,template下存放前端模板文件,本项目使用的freemarker,springboot官方提供了支持,可以无缝接入。

 

      如果是创建spring项目,直接选择依赖,web,mybatis,mysql,test,aop,freemarker即可,最后在pom中加上我们数据库连接池的依赖,最终pom.xml如下:



    4.0.0

    com.lhx
    springboot-mybatis-demo
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.7
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
            mysql
            mysql-connector-java
        

        
            com.alibaba
            druid
            1.0.13
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 

 

      下面先说配置文件application.yml, 具体配置如下:

spring:
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/maven-web
        username: root
        password: root
        type: com.alibaba.druid.pool.DruidDataSource

 数据库连接池使用的阿里巴巴的druid,当然druid还有其他的很多配置,这里只是配置最简单DataSource。

 

日志文件为logback.xml,这里不多介绍, 下面开始进入项目

 

    由于springboot自动配置帮我们节省了很多配置,且官方推荐JavaConfig,所以这里着重说一下config里面的东西,这里总共有两个要配置的东西,都是mybatis相关的,上代码:

@Configuration
@EnableTransactionManagement
public class MybatisConfig implements TransactionManagementConfigurer {

    @Autowired
    DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.lhx.demo.domain");

        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        try {
            sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath:mappers/**/*.xml"));
            return sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("SqlSessionFactory build error", e);
        }
    }

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

}

 还有一个 MybatisMapperScannerConfig配置,为什么要分开两个类配置呢,因为spring加载这个配置的时机比较靠前,加载的时候还没有价值DataSource,所以导致会出错,所以在这个MybatisMapperScannerConfig上不要忘记配置@AutoConfigureAfter(MybatisConfig.class)这个配置,不然会出错,源码如下: 

@Configuration
@AutoConfigureAfter(MybatisConfig.class)
public class MybatisMapperScannerConfig {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.lhx.demo.mapper");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");

        return mapperScannerConfigurer;
    }
}

 配置到这里, 基本的就完成了,其他的dao,service和controller的代码我这里就不多说了, 需要的后面会有git的项目源码地址, 剩下的启动类, springboot是基于main方法启动的, 源码如下 :

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

 这里要注意一下, 这个DemoApplication启动类,最好放于所有java类源码的顶层包中,因为它会有个@ComponentScan的默认配置,默认扫描启动类所在的包及其子包,所以如果不在一个包下, 则需要显示配置@ComponentScan所扫描的包, 到这里, 项目就基本完成了, 可以启动看看效果了。

 

最后, 本项目的源码在 https://github.com/363230482/springboot-mybatis-demo.git,第一次分享,肯定有很多写得不是很清楚,逻辑有点乱的地方,欢迎拍砖!

你可能感兴趣的:(springboot,mybatis,maven,springboot,mybatis,idea)