接着我们的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访问数据库了

 

相关文章:

  • 2022-02-19
  • 2021-07-06
  • 2018-05-22
  • 2021-08-11
  • 2022-01-23
  • 2021-09-25
  • 2021-10-20
猜你喜欢
  • 2022-01-01
  • 2021-06-26
  • 2021-10-22
  • 2022-12-23
  • 2021-12-25
  • 2021-08-31
  • 2019-09-30
相关资源
相似解决方案