springboot集成mybatis

本文介绍了如何在SpringBoot项目中集成MyBatis,并详细解释了相关配置项来源及使用不同环境的数据源方法。此外还提供了Gradle依赖配置示例及主类中Mapper路径的指定方式。

springboot集成mybatis

application.yml

从哪里找到这些配置项:

springboot 自己的配置项

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

mybatis的配置项

http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

阿里 druid的配置项

https://github.com/alibaba/druid/blob/master/druid-spring-boot-starter/src/test/resources/application.properties

不同的环境使用不同的数据源

使用 spring.profiles实现
参考: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environment

A YAML file is actually a sequence of documents separated by --- lines, and each document is parsed separately to a flattened map.

If a YAML document contains a spring.profiles key, then the profiles value (a comma-separated list of profiles) is fed into the Spring Environment.acceptsProfiles() method. If any of those profiles is active, that document is included in the final merge (otherwise, it is not), as shown in the following example:

server:
	port: 9000
---

spring:
	profiles: development
server:
	port: 9001

---

spring:
	profiles: production
server:
	port: 0

gradle 配置依赖

//配置mybatis
    compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1"
	compile 'com.alibaba:druid-spring-boot-starter:1.1.9'
    compile 'com.alibaba:druid:1.1.9'

主类指定mapper的路径

@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.xxsoft.mapper")  //扫描的是mapper.xml中namespace指向值的包位置
@ServletComponentScan //配置druid必须加的注解,如果不加,访问页面打不开,filter和servlet、listener之类的需要单独进行注册才能使用,spring boot里面提供了该注解起到注册作用
public class Application {

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

转载于:https://my.oschina.net/huangweiindex/blog/1842856

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值