springboot+myatis连接oracle数据库案例和可能出现的问题,解决方法

1、springboot项目的创建

添加oracle驱动的依赖和mybatis依赖:
在这里插入图片描述

pom.xml文件中的依赖:

    <!--oracle的驱动依赖-->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- Mybatis 依赖配置 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

.

在application.yml配置文件中配置数据源连接信息:

spring:
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@ip地址:端口号:实例名称
    username: 用户名
    password: 密码


mybatis:
  mapper-locations: classpath:映射文件路径

启动类上扫描dao的接口:


@MapperScan("com.example.dao")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

dao中的接口:

@Mapper
public interface TestMapper {

    @Select("select count(*) from 表名")
    Integer getCount();


}

springboot中的测试类:

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private TestMapper testMapper;

    @Test
    void contextLoads() {
        System.out.println(testMapper.getCount());
    }

}

项目结构图:
在这里插入图片描述
运行结果:

在这里插入图片描述
出现了报错信息:
Cause: java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK。
.
.
解决方法:
在项目的pom.xml文件中添加一个orai18n依赖包

   				<dependency>
                    <groupId>cn.easyproject</groupId>
                    <artifactId>orai18n</artifactId>
                    <version>12.1.0.2.0</version>
                </dependency>

运行结果:
在这里插入图片描述
结果可以正常打印在控制台了!!!
.
.
.

2、其他报错信息:

java.sql.SQLRecoverableException: IO 错误: The Network Adapter could not establish the connection
这个问题是你配置文件中的url写错了,只有确保配置文件的数据源的值,字符格式正确。基本上不会有什么问题,如果提示启动类找不到就Reimport一下依赖。
在这里插入图片描述
.
.
.
就此完成!!!撒花!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值