spring-boot mybatis配置

时间:2021-05-27 05:07:20

接着我们的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

spring-boot mybatis配置

1.4 修改启动类MyApplication.java

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

class类上添加注解MapperScan扫描mapper接口

经过以上步骤就可以在controller调用service访问数据库了