接着我們的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訪問數據庫了