好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Boot集成MyBatis的方法

spring boot 集成mybatis

在集成mybatis前,我们先配置一个druid数据源。

spring boot 集成druid

druid 有很多个配置选项,使用spring boot 的配置文件可以方便的配置 druid 。

在 application.yml 配置文件中写上:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

spring:

   datasource:

     name: test

     url: jdbc:mysql: //192.168.16.137:3306/test

     username: root

     password:

     # 使用druid数据源

     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

这里通过 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 中添加依赖:

?

1

2

3

4

5

<dependency>

   <groupid>org.mybatis.spring.boot</groupid>

   <artifactid>mybatis-spring-boot-starter</artifactid>

   <version> 1.0 . 0 </version>

</dependency>

mybatis-spring-boot-starter 依赖树如下:

其中mybatis使用的3.3.0版本,可以通过: <mybatis.version>3.3.0</mybatis.version>属性修改默认版本。 mybatis-spring使用版本1.2.3,可以通过: <mybatis-spring.version>1.2.3</mybatis-spring.version>修改默认版本。

在application.yml中增加配置:

?

1

2

3

mybatis:

  mapperlocations: classpath:mapper/*.xml

  typealiasespackage: tk.mapper.model

除了上面常见的两项配置,还有:

mybatis.config:mybatis-config.xml配置文件的路径 mybatis.typehandlerspackage:扫描typehandlers的包 mybatis.checkconfiglocation:检查配置文件是否存在 mybatis.executortype:设置执行模式(simple, reuse, batch),默认为simple

二、mybatis-spring方式

这种方式和平常的用法比较接近。需要添加mybatis依赖和mybatis-spring依赖。

然后创建一个mybatisconfig配置类:

?

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

/**

  * mybatis基础配置

  */

@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);

   }

}

上面代码创建了一个 sqlsessionfactory 和一个 sqlsessiontemplate ,为了支持注解事务,增加了 @enabletransactionmanagement 注解,并且反回了一个 platformtransactionmanagerbean 。

另外应该注意到这个配置中没有 mapperscannerconfigurer ,如果我们想要扫描mybatis的mapper接口,我们就需要配置这个类,这个配置我们需要单独放到一个类中。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

/**

  * mybatis扫描接口

  */

@configuration

//todo 注意,由于mapperscannerconfigurer执行的比较早,所以必须有下面的注解

@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;

   }

}

这个配置一定要注意 @autoconfigureafter(mybatisconfig.class) ,必须有这个配置,否则会有异常。原因就是这个类执行的比较早,由于 sqlsessionfactory 还不存在,后续执行出错。

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

关于分页插件和通用mapper集成

分页插件作为插件的例子在上面代码中有。

通用mapper配置实际就是配置 mapperscannerconfigurer 的时候使用 tk.mybatis.spring.mapper.mapperscannerconfigurer 即可,配置属性使用 properties 。

spring boot集成mybatis的基础项目

我上传到github一个采用第二种方式的集成项目,并且集成了分页插件和通用mapper,项目包含了简单的配置和操作,仅作为参考。

项目地址: https://github测试数据/abel533/mybatis-spring-boot

分页插件和通用mapper的相关信息可以通过上面地址找到。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/isea533/article/details/50359390

查看更多关于Spring Boot集成MyBatis的方法的详细内容...

  阅读:13次