javax.persistence中@Column定义字段类型方式
站在墙头上 人气:0javax.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自动转换规则即可。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
加载全部内容