javax.persistence中@Column定义字段类型方式
作者:站在墙头上 时间:2021-12-03 21:21:44
javax.persistence中@Column定义字段类型
在@Column中有个比较强大的配置 columnDefinition,如果有不好定义或者java没有这个属性的直接用columnDefinition根据ddl来定义即可,字段的注释也是可以定义的。
package com.ld.entity;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "banner")
public class BannerN {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Integer id;
@Column(name = "module_id",columnDefinition="bigint(20)")
private Long moduleId;
private String name;
@Column(name = "detail",columnDefinition="varchar(255) comment '详情'")
private String detail;
@Column(name = "banners",columnDefinition="text comment '集合字符串'")
private String banners;
@Transient
private List<Banner> bannerList;
private Integer type;
@Transient
private Integer index;
private Integer orderIndex;
private Long pageId;
static class Banner {
private Long id;
private Date createTime;
private Integer index;
private String pictrue;
private String click_url;//跳转链接
private String click_url_IOS;//跳转链接
private String click_url_Android;//跳转链接
private String copywriting;//文案
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Integer getIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
public String getPictrue() {
return pictrue;
}
public void setPictrue(String pictrue) {
this.pictrue = pictrue;
}
public String getClick_url() {
return click_url;
}
public void setClick_url(String click_url) {
this.click_url = click_url;
}
public String getClick_url_IOS() {
return click_url_IOS;
}
public void setClick_url_IOS(String click_url_IOS) {
this.click_url_IOS = click_url_IOS;
}
public String getClick_url_Android() {
return click_url_Android;
}
public void setClick_url_Android(String click_url_Android) {
this.click_url_Android = click_url_Android;
}
public String getCopywriting() {
return copywriting;
}
public void setCopywriting(String copywriting) {
this.copywriting = copywriting;
}
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
。。。
public String getBanners() {
if (this.banners != null) {
this.setBannerList(JSONArray.parseArray(banners, Banner.class));
}
return banners;
}
public void setBanners(String banners) {
this.banners = banners;
this.bannerList = JSONArray.parseArray(banners, Banner.class);
}
public List<Banner> getBannerList() {
return bannerList;
}
public void setBannerList(List<Banner> bannerList) {
this.bannerList = bannerList;
this.banners = JSONArray.toJSONString(bannerList, SerializerFeature.UseISO8601DateFormat);
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Integer getIndex() {
this.orderIndex = index;
return index;
}
public void setIndex(Integer index) {
this.index = index;
this.orderIndex = index;
}
public Integer getOrderIndex() {
if(orderIndex!=null){
this.index = orderIndex;
}
return orderIndex;
}
public void setOrderIndex(Integer orderIndex) {
this.orderIndex = orderIndex;
if(orderIndex!=null){
this.index = orderIndex;
}
}
@Transient
:自动生成表时忽略该字段。@Id
:主键@GeneratedValue(strategy = GenerationType.IDENTITY)
:主键策略(IDENTITY:自增)
记一个@Column的坑
注解@Column(javax.persistence.Column),我们通常使用在DAO实体类的属性上,一般用来标识该属性的数据库值(name,其他用途不提)。
BUG是这样产生的
项目中的实体类生成时每个字段均生成了@Column注解,且准确无误。
然后通用mapper使用没有任何问题,直到有一次,在*Mapper接口中手写了SQL(查询某表中满足条件的最新的一条记录):
@Select("SELECT * FROM t_test WHERE test_name = #{testName} ORDER BY gmt_create DESC LIMIT 1")
测试时,发现除了单个字段的属性有返回值(如:id、creator),其他多个单词组成的属性均没有值(如:testName、gmtCreate)。
因为找到了这个规律,所以断定是字段映射出错了,后来验证确实如此。
解决方法
在mybatis配置中加上驼峰命名自动转换规则:
mybatis.configuration.map-underscore-to-camel-case=true
由于水平有限,暂不清楚@Column在什么条件下有用。经测试,删除@Column,保留mybatis驼峰命名转换规则,通用mapper查询、*Mapper.java接口手写sql(包括属性写在查询条件中、返回结果中)、*Mapper.xml手写sql,均无问题。
有点不负责任地建议,实体类中不需要添加@Column注解,添加mybatis自动转换规则即可。
来源:https://blog.csdn.net/qq_19674263/article/details/105924391
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot下使用MyBatis-Puls代码生成器的方法
![](https://img.aspxhome.com/file/2023/1/60141_0s.png)
线程局部变量的实现 ThreadLocal使用及场景介绍
![](https://img.aspxhome.com/file/2023/1/59031_0s.jpg)
关于Java中的IO流总结(推荐)
![](https://img.aspxhome.com/file/2023/1/58191_0s.jpg)
2020JDK1.8安装教程详解(一次就可安装成功)
![](https://img.aspxhome.com/file/2023/3/60253_0s.png)
Android 使用 SharedPreferences 保存少量数据的实现代码
![](https://img.aspxhome.com/file/2023/4/83534_0s.png)
Java内部类知识汇总
![](https://img.aspxhome.com/file/2023/0/61000_0s.png)
Java最全文件操作实例汇总
SpringBoot如何使用RateLimiter通过AOP方式进行限流
![](https://img.aspxhome.com/file/2023/3/58583_0s.png)
Hibernate实现批量添加数据的方法
![](https://img.aspxhome.com/file/2023/5/60815_0s.jpg)
Java移动文件夹及其所有子文件与子文件夹
Java泛型映射不同的值类型详解及实例代码
java实现的简单猜数字游戏代码
![](https://img.aspxhome.com/file/2023/6/57606_0s.jpg)
基于idea 的 Java中的get/set方法之优雅的写法
![](https://img.aspxhome.com/file/2023/2/60092_0s.jpg)
解决问题:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources
![](https://img.aspxhome.com/file/2023/1/59871_0s.jpg)
java搭建ftp/sftp进行数据传递的全过程
![](https://img.aspxhome.com/file/2023/6/60486_0s.png)
JavaWeb工程中集成YMP框架快速上手
![](https://img.aspxhome.com/file/2023/9/58709_0s.png)
JAVA布局管理器与面板组合代码实例
![](https://img.aspxhome.com/file/2023/4/60534_0s.png)
spring中的FactoryBean代码示例
Flutter模仿实现微信底部导航栏流程详解
![](https://img.aspxhome.com/file/2023/3/100603_0s.png)