先总结
- 库名,表名和字段名尽量大写。小写必须要使用双引号,不然会报各种问题,包括【语法解析错误】,【无效的列名】
- 如果列名必须小写,那么要自己写mapper.xml。mybatis-plus在操作insert的时候会报【dm.jdbc.driver.DMException: 无效的列】,加了双引号也不行。参见我另外一篇文章mybatis-plus insert无效的列。
以下是部分代码
包引用
xml
<?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.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dmdba.demo</groupId>
<artifactId>dmdbaDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dmdbaDemo</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.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>
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.3.62</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</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>
</plugins>
</build>
</project>
表结构
sql
CREATE TABLE "test"."user"
(
"ID" INTEGER IDENTITY(1, 1) NOT NULL,
"USERNAME" VARCHAR2(50),
"login_name" VARCHAR2(50),
UNIQUE("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
mapper
错误案例:login_name没有带双引号,不带的话就变大写了,报错【无效的列名[LOGIN_NAME]】。当然,如果列名本身是大写的就不用带双引号了
另外,库名和表名也要带双引号,不然就大写,不然报【语法分析出错】
xml
<?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.dmdba.demo.dmdbademo.mappers.UserMapper">
<insert id="ins">
insert into "test"."user" ("login_name")
values(#{loginName})
</insert>
</mapper>
entity
注意,库名和表名需要带双引号,不想带的话要大写
如果小写而没有带双引号则报【语法分析出错】
java
@Data
@TableName("\"test\".\"user\"")
public class User {
@TableId(type = IdType.AUTO, value = "ID")
private Integer id;
@TableField("USERNAME")
private String username;
private String loginName;
}