spring-boot mybatis配置


接着我们的spring boot项目,spring boot如何使用mybatis访问数据库呢?

个人习惯使用mapper接口和xml配置sql,从pom.xml入手

1.1 添加依赖

<dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.2.0version>
        dependency>
        
        <dependency>
            <groupId>com.oraclegroupId>
            <artifactId>ojdbc6artifactId>
            <version>11.2.0.3.0version>
            <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访问数据库了