JPA之映射mysql text類型的問題
JPA之映射mysql text類型
問題背景
jpa如果直接映射mysql的text/longtext/tinytext類型到String字段會報錯。需要設(shè)置一下@Lob和@Column。
@Lob代表是長字段類型,默認(rèn)的話,是longtext類型,所以需要下面這個屬性來指定對應(yīng)的類型。
columnDefinition="text"里面的類型可以隨意改,后面mysql可能會有新的類型,只要是對應(yīng)java的String類型,就可以在這里動態(tài)配置。
解決方案
@Data
@Entity
@Table(name="question")
public class Question {
@Id
@GeneratedValue
public int questionId;
//....省略其他字段
@Lob
@Column(columnDefinition="text")
public String explainStr;
public Date createTime;
}
JPA各種類型映射處理
1.日期格式類型字段的映射,利用@Temporal(TemporalType.Date)進(jìn)行注解;例如:
private Date birthday;
@Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
2.枚舉類型的映射,利用@Enumerated,其參數(shù)EnumType表示指定存放在數(shù)據(jù)表中的形式,整型還是String;
首先創(chuàng)建一個枚舉:
public enum Gender
{
Male,Female
}
在實體類中調(diào)用:
private Gender gender = Gender.Male; // 為枚舉設(shè)置默認(rèn)值
@Enumerated(EnumType.STRING)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
3.大文本數(shù)據(jù)類型的映射,例如:網(wǎng)絡(luò)日志,字符串類型的長度顯然不夠,利用@Lob注解,該注解用在字符串類型之上在數(shù)據(jù)庫生成LongText類型的數(shù)據(jù);例如:
private String diary;
@Lob
public String getDiary() {
return diary;
}
public void setDiary(String diary) {
this.diary = diary;
}
4.@Lob注解用在Byte[]數(shù)組類型,例如:保存一個文件可以用此類型,用在這個上面在數(shù)據(jù)庫中可以生成LongBolb數(shù)據(jù)類型;例如:
private Byte[] file;
@Lob
public Byte[] getFile() {
return file;
}
public void setFile(Byte[] file) {
this.file = file;
}
5.如果在實體類中不需要該字段與數(shù)據(jù)庫中的表進(jìn)行映射,但是默認(rèn)的情況下是將實體類的全部字段映射成數(shù)據(jù)表的列,那該怎樣做呢?利用@Transient注解,例如:
private String other;
@Transient
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
6.如果一個實體類中包含一個大數(shù)據(jù)類型的字段,如Byte[]類型,當(dāng)我們查詢該實體類時不得不查詢出該數(shù)據(jù)類型的字段,如果我們在查詢時只用到一個其它字段的數(shù)據(jù),但是默認(rèn)情況下是查詢?nèi)康模窃撛鯓颖苊獠樵冊摯髷?shù)據(jù)類型的數(shù)據(jù)呢?利用@Basic注解進(jìn)行標(biāo)注,將fetch屬性值設(shè)置為Lazy即可,這樣只有在我們調(diào)用該屬性的get方法時才會進(jìn)行加載;
private Byte[] file;
@Lob @Basic(fetch=FetchType.LAZY)
public Byte[] getFile() {
return file;
}
public void setFile(Byte[] file) {
this.file = file;
}
7.@Column 該注解表示數(shù)據(jù)表的映射列,放在屬性的getter方法上:
.length:該屬性表示該映射列的長度;.nullable:該屬性表示該列是否可為空,true表示可為空,false表示不可為空;.name:該屬性表示為該列起別名,不讓實體類的屬性名與數(shù)據(jù)庫的列名相同;
8.@Table 該注解表示映射的表;放在該實體類之上:
.name:該屬性表示為表起別名,讓實體類與數(shù)據(jù)表名不相同;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Springboot2.0配置JPA多數(shù)據(jù)源連接兩個mysql數(shù)據(jù)庫方式
- Spring boot基于JPA訪問MySQL數(shù)據(jù)庫的實現(xiàn)
- 解決springboot的JPA在Mysql8新增記錄失敗的問題
- Spring Data Jpa Mysql使用utf8mb4編碼的示例代碼
- springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫
- Spring-Data-JPA整合MySQL和配置的方法
- SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進(jìn)行操作
- Spring Boot 添加MySQL數(shù)據(jù)庫及JPA實例
- 在JPA項目啟動時如何新增MySQL字段
相關(guān)文章
Intellij idea使用Statistic統(tǒng)計代碼行數(shù)的方法
這篇文章主要介紹了Intellij idea使用Statistic統(tǒng)計代碼行數(shù)的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
SpringBoot如何使用ApplicationContext獲取bean對象
這篇文章主要介紹了SpringBoot 如何使用ApplicationContext獲取bean對象,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
看動畫學(xué)算法之Java實現(xiàn)doublyLinkedList
這篇文章主要介紹Java實現(xiàn)doublyLinkedList,LinkedList:doublyLinkedList相對比較復(fù)雜,今天就來簡單學(xué)習(xí)一下doublyLinkedList的基本操作和概,感興趣的小伙伴可以參考下面具體文章內(nèi)容2021-10-10
jxl操作excel寫入數(shù)據(jù)不覆蓋原有數(shù)據(jù)示例
網(wǎng)上很多例子,都是用Jxl讀或者寫excel,本文實現(xiàn)的功能就是將數(shù)據(jù)源in.xls的第幾行第幾列數(shù)據(jù)寫入到out.xls的第幾行第幾列,不覆蓋out.xls其他原有的數(shù)據(jù)。2014-03-03

