spring-boot mybatis配置


接着我們的spring boot項目,spring boot如何使用mybatis訪問數據庫呢?

個人習慣使用mapper接口和xml配置sql,從pom.xml入手

1.1 添加依賴

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>
        <!--oracle驅動-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3.0</version>
            <scope>${jar-scope}</scope>
        </dependency>

1.2 application.properties配置

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@*.*.*.*:*/*
spring.datasource.username=*
spring.datasource.password=*

mybatis.mapper-locations=classpath*:sqlmapper/*Mapper.xml
mybatis.type-aliases-package=hello.dal.model

1.3 編碼

像其它項目一樣mapper.xml,mapper接口,表model,服務service

1.4 修改啟動類MyApplication.java

@SpringBootApplication
@MapperScan("hello.dal.mapper")
public class MyApplication

class類上添加注解MapperScan掃描mapper接口

 

經過以上步驟就可以在controller調用service訪問數據庫了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM