日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Spring Boot 集成MyBatis

 瀚海璨夜 2017-03-14

Spring Boot 集成MyBatis

在集成MyBatis前,我們先配置一個druid數(shù)據(jù)源。

Spring Boot 系列

  1. Spring Boot 入門

  2. Spring Boot 屬性配置和使用

  3. Spring Boot 集成MyBatis

  4. Spring Boot 靜態(tài)資源處理

  5. Spring Boot - 配置排序依賴技巧

Spring Boot 集成druid

druid有很多個配置選項,使用Spring Boot 的配置文件可以方便的配置druid。

application.yml配置文件中寫上:

spring:
    datasource:
        name: test
        url: jdbc:mysql://192.168.16.137:3306/test
        username: root
        password:
        # 使用druid數(shù)據(jù)源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

這里通過type: com.alibaba.druid.pool.DruidDataSource配置即可!

Spring Boot 集成MyBatis

Spring Boot 集成MyBatis有兩種方式,一種簡單的方式就是使用MyBatis官方提供的:

mybatis-spring-boot-starter

另外一種方式就是仍然用類似mybatis-spring的配置方式,這種方式需要自己寫一些代碼,但是可以很方便的控制MyBatis的各項配置。

一、mybatis-spring-boot-starter方式

pom.xml中添加依賴:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

mybatis-spring-boot-starter依賴樹如下:
依賴樹

其中mybatis使用的3.3.0版本,可以通過:
<mybatis.version>3.3.0</mybatis.version>屬性修改默認(rèn)版本。
mybatis-spring使用版本1.2.3,可以通過:
<mybatis-spring.version>1.2.3</mybatis-spring.version>修改默認(rèn)版本。

application.yml中增加配置:

mybatis: 
  mapperLocations: classpath:mapper/*.xml
  typeAliasesPackage: tk.mapper.model 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

除了上面常見的兩項配置,還有:

  • mybatis.config:mybatis-config.xml配置文件的路徑
  • mybatis.typeHandlersPackage:掃描typeHandlers的包
  • mybatis.checkConfigLocation:檢查配置文件是否存在
  • mybatis.executorType:設(shè)置執(zhí)行模式(SIMPLE, REUSE, BATCH),默認(rèn)為SIMPLE

二、mybatis-spring方式

這種方式和平常的用法比較接近。需要添加mybatis依賴和mybatis-spring依賴。

然后創(chuàng)建一個MyBatisConfig配置類:

/**
 * MyBatis基礎(chǔ)配置
 *
 * @author liuzh
 * @since 2015-12-19 10:11
 */
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {

    @Autowired
    DataSource dataSource;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("tk.mybatis.springboot.model");

        //分頁插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);

        //添加插件
        bean.setPlugins(new Interceptor[]{pageHelper});

        //添加XML目錄
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

上面代碼創(chuàng)建了一個SqlSessionFactory和一個SqlSessionTemplate,為了支持注解事務(wù),增加了@EnableTransactionManagement注解,并且反回了一個PlatformTransactionManagerBean。

另外應(yīng)該注意到這個配置中沒有MapperScannerConfigurer,如果我們想要掃描MyBatis的Mapper接口,我們就需要配置這個類,這個配置我們需要單獨(dú)放到一個類中。

/**
 * MyBatis掃描接口
 * 
 * @author liuzh
 * @since 2015-12-19 14:46
 */
@Configuration
//TODO 注意,由于MapperScannerConfigurer執(zhí)行的比較早,所以必須有下面的注解
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("tk.mybatis.springboot.mapper");
        return mapperScannerConfigurer;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

這個配置一定要注意@AutoConfigureAfter(MyBatisConfig.class),必須有這個配置,否則會有異常。原因就是這個類執(zhí)行的比較早,由于sqlSessionFactory還不存在,后續(xù)執(zhí)行出錯。

做好上面配置以后就可以使用MyBatis了。

關(guān)于分頁插件和通用Mapper集成

分頁插件作為插件的例子在上面代碼中有。

通用Mapper配置實際就是配置MapperScannerConfigurer的時候使用tk.mybatis.spring.mapper.MapperScannerConfigurer即可,配置屬性使用Properties。

Spring Boot集成MyBatis的基礎(chǔ)項目

我上傳到github一個采用第二種方式的集成項目,并且集成了分頁插件和通用Mapper,項目包含了簡單的配置和操作,僅作為參考。

項目地址:https://github.com/abel533/MyBatis-Spring-Boot

分頁插件和通用Mapper的相關(guān)信息可以通過上面地址找到。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多