SpringBoot integrates Oracle combat and pits

Foreword:

Some time ago, I worked on an oracle project, which took a lot of time. Now that the project is online as a whole, I will record the actual combat process and the pitfalls encountered here. Netizens who need it can also use it directly.

1. Guide package

springboot version: 2.1.3. It is best to have more than 10, and there are some problems with less than 10 on the Internet. I didn't test it myself.

<!--oracle驱动-->
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3</version>
</dependency>

2. Configuration file: database information

# oracle datasource 数据库访问配置  默认 
spring.datasource.platform.url = jdbc:oracle:thin:@127.0.0.1:1521/orcl
spring.datasource.platform.username = LOCAL
spring.datasource.platform.password = LOCAL
spring.datasource.platform.driver-class-name = oracle.jdbc.OracleDriver

Note : here orcl is the service name, not sid. If it is sid, it is 1521: sid. The meanings of the two symbols are different. Generally, the service name is given by the operation and maintenance. If you can’t connect and then confirm the password is correct, you can confirm it with your colleagues. It’s a small pit

Also, there are more platform names in the configuration here. It is because I use the sub-database and sub-table, and the self-configured database config. If you are not, but the automatic acquisition needs to be removed, otherwise spring will not be able to connect if it cannot obtain it.

Here username is not allowed to be an administrator level , otherwise an error connecting to the database will be reported.

Rumors:

Some bloggers talk nonsense without experimenting by themselves, and the above error says: add as sysdba after the user name, it can be solved. I tried it myself and did not report an error connecting to the database, but when querying the database, it reported that the table or view could not be found. It is recommended that you create a user yourself, which is related to oracle's own rules.

3. Code

The controller, service, mapper, model, etc. we usually use can be generated by tools (idea). You can refer to this article of mine, which contains a prepared oracle template and detailed tutorials: juejin.cn/post/722333…

I won't post the specific code, and there are specific renderings in the website. Matching number template, the whole process can be done automatically with one key. But if you are not using an idea, you may have to write it manually. Here I will talk about a few points to pay attention to when writing by hand, mainly xml:

1. There is no self-incrementing ID when Oracle creates a table, which needs to be configured. I use varchar2 without self-increment configuration, and netizens need to search for it by themselves;

2.虽然我不是自增,但是模板是按照自增的设计的(涉及到初始模板)。所以如果你和我一样的话,新增需要手动加下,然后修改有需要的话也改一下。不推荐改动模板;

3.oracle中是没有limit分页的,所以需要注意修改,我的模板中没有因为这次需求只有一个分页,需求量大的可以自己加上,研究一下很简单;

其次我们有的会集成MyBatis分页,如果传入page参数就会触发,然后sql报错

4.手动写代码时,字段名需要用“”包含,否则会报sql执行错误

问题更新:

1.一直报这个驱动类找不到,但是这个驱动类jar包已经添加到项目了,并且我也反编译jar包,确认路径是对的。后来将target文件夹删掉重新编译,就没有问题了;

2.在上面中说到字段名需要加“”,是因为有的字段是特殊字段,比如DATE等,所以在模板中就直接都加上了;

3.其实使用管理员登录也是可以,但是查询时需要加上库名.表名(自身感觉多此一举,不推荐);

今天就到这里吧,感觉有用的小伙伴可以点个赞;有其它意见和问题的也可以在下面评论,看到后会及时回复。你的支持就是我更新的最大动力!

Guess you like

Origin juejin.im/post/7233698667847335995