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一下依赖。
.
.
.
就此完成!!!撒花!!!