写在前面:
如本文描述有错误,希望读到这篇文章的您能够提出批评指正。 联系方式:172310978@qq.com
参考文章:
- https://blog.csdn.net/weixin_44384605/article/details/118449107
1. java中用spring boot连接oracle数据库
代码下载链接
百度云:https://pan.baidu.com/s/1dU\_z2pUS2NSfowI4\_mJ4Ow
提取码:mmlm
连接oracle主要是引进ojdbc 可以在pom.xml文件中加入
1 2 3 4 5
| <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.4</version> </dependency>
|
项目创建详解
1.创建新的spring boot项目 项目名为text
2.创建有关文件
下面是项目中创建的大体目录
3.pom.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>oracle_java</artifactId> <version>0.0.1-SNAPSHOT</version> <name>oracle_java</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- 默认导入oracle连接包 因测试时会报错因此为采用--> <!-- <dependency>--> <!-- <groupId>com.oracle.database.jdbc</groupId>--> <!-- <artifactId>ojdbc8</artifactId>--> <!-- <scope>runtime</scope>--> <!-- </dependency>--> <!-- 引入ojdk6 用来替换ojdk8--> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--后期引入 @Mapper用--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
<!--后期引入 @Mapper用--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <!--后期引入--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> <!--后期引入--> <resources> <!--将Java代码下的xml编译到class下面去--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.yml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> <include>**/*.yml</include> </includes> </resource> </resources> </build>
</project>
|
4.application.yml (源文件文件类型不是这样的,将原来的文件后缀改成.yml )
设置自己的数据库信息 将username和password改成自己的
1 2 3 4 5 6 7 8 9 10 11 12 13
| server: port: 8080 spring: datasource: driverClassName: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@127.0.0.1:1521:orcl username: system password: 123456 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.zhg.demo.mybatis.entity configuration: map-underscore-to-camel-case: true
|
5.TextConterller.java文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.text.conterller;
import com.text.mapper.TextMapper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller @RequestMapping("/text") public class TextConterller { @Resource TextMapper textMapper;
@RequestMapping(value = "/name",method = RequestMethod.GET) @ResponseBody public String getName(String code){ String name=textMapper.getName(code); return name; } }
|
6.TextMapper.java文件
1 2 3 4 5 6 7 8
| package com.text.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper public interface TextMapper { String getName(String code); }
|
7.TextMapper.xml文件
1 2 3 4 5 6 7 8 9
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.text.mapper.TextMapper">
<select id = "getName" resultType="String"> select name from v_emp where barcode=#{code} and rownum = 1 </select>
</mapper>
|
8.项目运行测试
测试连接
http://localhost:8080/text/name?code=输入的内容