SpringBoot标准集成MyBatis的2种方式是怎样的

2023-04-14 02:29:00 方式 集成 是怎样

MyBatis是一款优秀的持久层框架,广受Java开发人员的青睐。SpringBoot是目前非常流行的Java开发框架,它可以让Java开发人员轻松地构建高效、可靠、可扩展的企业级应用程序。

SpringBoot集成MyBatis有两种方式:第一种是通过MyBatis官方提供的Starter来集成,第二种是通过官方提供的MyBatis-Spring-Boot-Starter来集成。

第一种方式要求我们在pom.xml文件中添加MyBatis的Starter依赖:

org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2

然后在application.properties文件中配置MyBatis的相关属性:

mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.cache-enabled=true mybatis.configuration.jdbc-type-for-null=OTHER mybatis.configuration.lazy-loading-enabled=true mybatis.configuration.aggressive-lazy-loading=false mybatis.configuration.multiple-result-sets-enabled=true mybatis.configuration.use-generated-keys=true mybatis.configuration.default-executor-type=SIMPLE mybatis.configuration.default-statement-timeout=25000 mybatis.configuration.default-fetch-size=100 mybatis.configuration.default-result-set-type=FORWARD_ONLY mybatis.configuration.safe-row-bounds-enabled=false mybatis.configuration.local-cache-scope=SESSION mybatis.configuration.jdbc-type-for-null=OTHER mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.entity

第二种方式要求我们在pom.xml文件中添加MyBatis-Spring-Boot-Starter的依赖:

org.mybatis.spring.boot mybatis-spring-boot-starter-test 1.3.2

然后在application.properties文件中配置MyBatis的相关属性:

mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.cache-enabled=true mybatis.configuration.jdbc-type-for-null=OTHER mybatis.configuration.lazy-loading-enabled=true mybatis.configuration.aggressive-lazy-loading=false mybatis.configuration.multiple-result-sets-enabled=true mybatis.configuration.use-generated-keys=true mybatis.configuration.default-executor-type=SIMPLE mybatis.configuration.default-statement-timeout=25000 mybatis.configuration.default-fetch-size=100 mybatis.configuration.default-result-set-type=FORWARD_ONLY mybatis.configuration.safe-row-bounds-enabled=false mybatis.configuration.local-cache-scope=SESSION mybatis.configuration.jdbc-type-for-null=OTHER mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.entity

以上就是SpringBoot标准集成MyBatis的两种方式,希望对大家有所帮助。

相关文章