笔者的使用mysql数据库,实体如下
import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.ibatis.type.JdbcType;import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Data @NoArgsConstructor @AllArgsConstructor @Builder @Table(name = "api_ocr_document") public class ApiOCRDocument{ @Id @GeneratedValue(generator = "JDBC") private Long id; private String mimeType; private String hash; private byte[] binary; private String text; private String createTime; }
插入语句生成的sql如下
INSERT INTO api_ocr_document ( id,mime_type,hash,binary,text,create_time ) VALUES( ?,?,?,?,?,? )
放到navicat一看,两个关键字(hash,binary)
解决方法,使用tk.mybatis私有注解ColumnType将字段使用mysql关键字·转义一下
正确代码如下:
package com.netmarch.web.open.bean; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.ibatis.type.JdbcType; import tk.mybatis.mapper.annotation.ColumnType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Data @NoArgsConstructor @AllArgsConstructor @Builder @Table(name = "api_ocr_document") public class ApiOCRDocument{ @Id @GeneratedValue(generator = "JDBC") private Long id; private String mimeType; @ColumnType(column = "`hash`",jdbcType = JdbcType.VARCHAR) private String hash; @ColumnType(column = "`binary`",jdbcType = JdbcType.BLOB) private byte[] binary; private String text; private String createTime; }
再次生成的sql如下
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。