最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javax.persistence中@Column定義字段類型方式

 更新時間:2022年11月17日 14:27:31   作者:站在墻頭上  
這篇文章主要介紹了javax.persistence中@Column定義字段類型方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

javax.persistence中@Column定義字段類型

在@Column中有個比較強大的配置 columnDefinition,如果有不好定義或者java沒有這個屬性的直接用columnDefinition根據(jù)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;//跳轉(zhuǎn)鏈接
        private String click_url_IOS;//跳轉(zhuǎn)鏈接
        private String click_url_Android;//跳轉(zhuǎn)鏈接
        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實體類的屬性上,一般用來標(biāo)識該屬性的數(shù)據(jù)庫值(name,其他用途不提)。

BUG是這樣產(chǎn)生的

項目中的實體類生成時每個字段均生成了@Column注解,且準(zhǔn)確無誤。

然后通用mapper使用沒有任何問題,直到有一次,在*Mapper接口中手寫了SQL(查詢某表中滿足條件的最新的一條記錄):

@Select("SELECT * FROM t_test WHERE test_name = #{testName} ORDER BY gmt_create DESC LIMIT 1")

測試時,發(fā)現(xiàn)除了單個字段的屬性有返回值(如:id、creator),其他多個單詞組成的屬性均沒有值(如:testName、gmtCreate)。

因為找到了這個規(guī)律,所以斷定是字段映射出錯了,后來驗證確實如此。

解決方法

在mybatis配置中加上駝峰命名自動轉(zhuǎn)換規(guī)則:

mybatis.configuration.map-underscore-to-camel-case=true

由于水平有限,暫不清楚@Column在什么條件下有用。經(jīng)測試,刪除@Column,保留mybatis駝峰命名轉(zhuǎn)換規(guī)則,通用mapper查詢、*Mapper.java接口手寫sql(包括屬性寫在查詢條件中、返回結(jié)果中)、*Mapper.xml手寫sql,均無問題。

有點不負(fù)責(zé)任地建議,實體類中不需要添加@Column注解,添加mybatis自動轉(zhuǎn)換規(guī)則即可。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

桑日县| 张家界市| 宁化县| 开远市| 互助| 道真| 鸡西市| 焦作市| 伊金霍洛旗| 六安市| 福泉市| 延吉市| 永济市| 濉溪县| 和硕县| 武宁县| 漯河市| 江口县| 信阳市| 邯郸市| 蕲春县| 荣昌县| 宣武区| 济源市| 二连浩特市| 沈阳市| 大埔县| 黄浦区| 长寿区| 铁岭县| 平原县| 房产| 万载县| 昭平县| 孟连| 沛县| 南部县| 六安市| 定边县| 安吉县| 和龙市|