beetlSQL實踐 (二) 命名轉換及表和列名映射

項目啓動成功,接下來我們通過存儲的功能來實踐一番。
上篇已經講述過beetlSQL基本使用,本篇則不再過多的介紹。
剛開始創建的表,語句如下:

CREATE TABLE `exam` (
  `exam_d` int(10) NOT NULL AUTO_INCREMENT,
  `exam_ame` varchar(60) NOT NULL DEFAULT '',
  `subject_d` int(20) NOT NULL DEFAULT '0',
  `score` int(10) DEFAULT '0',
  `evaluation_evel` int(3) DEFAULT '0',
  PRIMARY KEY (`exam_d`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4;

examID爲自增主鍵,字段都是下劃線命名;其對應的實體是駝峯式命名.

public class Exam implements Serializable {

    private Integer examId;

    private String examName;

    private Integer subjectId;

    private Integer score;

    private Integer evaluationLevel;

    public Integer getExamId() {
        return examId;
    }

    public void setExamId(Integer examId) {
        this.examId = examId;
    }

    public String getExamName() {
        return examName;
    }

    public void setExamName(String examName) {
        this.examName = examName;
    }

    public Integer getSubjectId() {
        return subjectId;
    }

    public void setSubjectId(Integer subjectId) {
        this.subjectId = subjectId;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public Integer getEvaluationLevel() {
        return evaluationLevel;
    }

    public void setEvaluationLevel(Integer evaluationLevel) {
        this.evaluationLevel = evaluationLevel;
    }

    @Override
    public String toString() {
        return "Exam{"
                + "examId=" + examId +
                "examName=" + examName +
                "subjectId=" + subjectId +
                "score=" + score +
                "evaluationLevel=" + evaluationLevel +
                '}';
    }
}

在這種對應情況下,當程序執行成功,啓動功能的時候,報錯了。
nullException
爲何會報空指針錯誤呢,表字段一共就5個,兩個可以爲空,剩餘的三個明明已經賦值了,真是爲何?
這是因爲在配置SqlManagerFactoryBean工廠類配置的時候,沒有明確beetlSQL的映射類。在報錯的情況下,我的SqlManagerFactoryBean是這樣配置的:

 @Bean(name = "examSqlManager")
    @Primary
    public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("examDB")DataSource master){
        SqlManagerFactoryBean factoryBean=new SqlManagerFactoryBean();
        BeetlSqlDataSource source=new BeetlSqlDataSource();
        source.setMasterSource(master);
        factoryBean.setCs(source);
        factoryBean.setDbStyle(new MySqlStyle());
        //sql文件路徑
        factoryBean.setSqlLoader(new ClasspathLoader("/sql"));
        return factoryBean;
    }

缺少了factoryBean.setNc()映射的處理。
Beetlsql 默認提供了三種列明和屬性名的映射類

映射 說明
DefaultNameConversion 數據庫名和java屬性名保持一致,如數據庫表User,對應Java類也是User,數據庫列是sysytemId,則java屬性也是systemId,反之亦然
UnderlinedNameConversion 將數據庫下劃線去掉,首字母大寫,如數據庫是SYS_USER(oralce數據庫的表和屬性總是大寫的), 則會改成SysUser
JPA2NameConversion 支持JPA方式的映射,適合不能用確定的映射關係

一般情況下推薦使用UnderlinedNameConversion,實體爲駝峯命名,表字段爲下劃線命名。

@Bean(name = "examSqlManager")
    @Primary
    public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("examDB")DataSource master){
        SqlManagerFactoryBean factoryBean=new SqlManagerFactoryBean();
        BeetlSqlDataSource source=new BeetlSqlDataSource();
        source.setMasterSource(master);
        factoryBean.setCs(source);
        factoryBean.setDbStyle(new MySqlStyle());
        //開啓駝峯
        factoryBean.setNc(new UnderlinedNameConversion());
        //sql文件路徑
        factoryBean.setSqlLoader(new ClasspathLoader("/sql"));
        return factoryBean;
    }

假設設置了駝峯配置,而數據庫卻也弄得駝峯式,這種情況下,自己通過beetlSQL實現插入方法則不會有任何影響,但是這種情況下,假設你要調用beetlSQL API的insert功能,不會報錯,但是字段值不會插入成功,只會通過主鍵增添一條空記錄而已。
存儲
當然,使用哪種映射處理,都是憑藉自己習慣或者公司規定的規範來的。除了這幾種以外,還可以通過JPA標籤來幫助解析實體類到數據庫的轉換,具體可以去參考官網,beetlSQL官網

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章