springboot集成mybatis

springboot集成mybatis

application.yml

從哪裏找到這些配置項:
html

springboot 本身的配置項

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

mybatis的配置項

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

阿里 druid的配置項

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

不一樣的環境使用不一樣的數據源

使用 spring.profiles實現
參考: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-change-configuration-depending-on-the-environmentspring

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

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:mybatis

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);
    }
}
相關文章
相關標籤/搜索