Sometimes our database design may not be completely consistent with the code language. For example, we will add a specific prefix in front of each table in the database to distinguish it. In BeetlSQL, the name of the code Pojo corresponds to the name of the database Table. It is converted using NameConversion. BeetlSQL has built-in converters such as DefaultNameConversion, UnderlinedNameConversion and JPANameConversion, which can basically meet most requirements. Today I will show you how to customize NameConversion.
Foreword
To understand the content of this article, first of all, you need to understand the two projects Beetl and BeetlSQL. They are the Java template engine and Java database full-featured Dao that were painstakingly built by the Chinese @Xian·Dafu, and currently The official has provided integration solutions for Beetl and BeetlSQL with various mainstream MVC frameworks on the market
I dare not say how Beetl is better than some well-known template engines such as JSP, FreeMarker, Velocity, etc., but I want to say that in my personal opinion For comparison, Beetl will definitely not be worse than them, and BeetlSQL will not be worse than Hibernate and MyBatis. If you are interested in learning more about Beetl and BeetlSQL, you can visit the Beetl official forum for details.
Why not give Beetl a chance to prove Beetl yourself, and give yourself a chance to learn and work. Maybe you will fall in love with it accidentally like me.
Text
This article discusses the problem of removing the mapping table name prefix through a custom converter when using BeetlSQL to automatically generate Pojo objects.
Let’s first look at a set of development situations:
Suppose there is currently a project beetl. When designing the database, all table names have a project prefix bt_, that is, all tables start with bt_, such as bt_user, bt_role, bt_user_role, when we see such a table name design, the first thing we may think of is to use UnderlinedNameConversion for conversion, so:
bt_user-> BtUser
bt_role -> BtRole
bt_user_role -> BtUserRole
I believe that many people, like me, cannot accept such naming, so we need to define a NameConversion ourselves for conversion, and remove the Bt prefix automatically added when Table generates Pojo
The core methods getPropertyName and getColName in NameConversion , getTableName, getClassName, it is easy to understand based on the method name and parameters
So we only need to re-implement a NameConversion according to our own actual needs to convert the table name to a class name and the class name to table name. Re-implement the following methods .
Specific method: First, we create a new class that inherits from the DefaultNameConversion class, override the getClassName and getTableName methods respectively, and finally enable the Conversion we wrote in our project
@Override
@Overridepublic String getTableName(Class<?> c) { //遵循BeetlSQL規(guī)范,@Table擁有最高優(yōu)先級 Table table = (Table)c.getAnnotation(Table.class); if(table!=null){ return table.name(); } //UserRole -> user_role String tableName = StringKit.enCodeUnderlined(c.getSimpleName()); //user_role -> bt_user_role return "bt_"+tableName; }@Overridepublic String getClassName(String tableName){ //假定所有表都是以bt_開頭 //bt_user_role -> user_role tableName = tableName.substring(tableName.indexOf("_")+1); //user_role -> userRole String clsName = StringKit.deCodeUnderlined(tableName); //userRole -> UserRole return StringKit.toUpperCaseFirstOne(clsName); }
It’s done easily like this Remove prefixes from table names in the entire database.
It backfired! ! ! Often we may encounter more special situations. Let's look at a set of situations that development may encounter:
Currently there is a project with some user data tables (such as User) and background management data tables. (such as Admin) and the data table that was previously shared between users and the backend (such as City). Then, in order to distinguish it during development, different prefixes were added to the tables of different functional modules. In the end, our table name may be: usr_user , mgr_admin and base_city. At this time, it will definitely not work if we use the above method to uniformly remove prefixes and then uniformly add prefixes. After all, the prefixes are different and it is difficult to directly restore them after removing the prefixes.
Initially I encountered this problem. My first idea was to let BeetlSQL reversely generate Pojos by removing the prefix and automatically adding @Table annotations to all Pojos, such as usr_user, mgr_admin and base_city in the example. The table may finally be generated in the following form:
usr_user -> @Table(name="usr_user") User
mgr_admin -> @Table(name="mgr_admin") Admin
base_city -> @Table(name= "base_city") City
理想是豐滿的,現(xiàn)實是骨感的。在QQ上與作者溝通遇到這種情況時的解決方案時發(fā)現(xiàn)可能會出現(xiàn)這樣的情況: 前臺用戶使用的數(shù)據(jù)表可能是 usr_user,而后臺管理員也屬于用戶呀,因此可能后臺管理員的數(shù)據(jù)表為mgr_user,最后這樣會導(dǎo)致兩個類沖突了,生成代碼的時候可能會被覆蓋一個類,是有問題的。好在機智如我~遇到這樣的根據(jù)功能模塊給不同的表加上不同的前綴,我們在Java程序開發(fā)時也經(jīng)常會使用不同的包名來區(qū)分不同的模塊,我可以將不同前綴的實體放到對應(yīng)的包下面,還原表名的時候讀取一下包名即可反向解析出表名,下面是我的具體實現(xiàn):
@Overridepublic String getClassName(String tableName){ //為了安全起見,做了個判斷,理論上項目數(shù)據(jù)庫設(shè)計好了應(yīng)該是無需判斷直接截取所有前綴 //usr_user_role -> user_role if(tableName.matches("^(usr_|base_|mgr_).*")){ tableName = tableName.substring(tableName.indexOf("_")+1); } //user_role -> UserRole String clsName = StringKit.deCodeUnderlined(tableName); return StringKit.toUpperCaseFirstOne(clsName); }@Overridepublic String getTableName(Class<?> c) { Table table = (Table)c.getAnnotation(Table.class); if(table!=null){ return table.name(); } //獲取Package 最后一層 xxx.pojo.usr -> Usr String pkg = c.getPackage().getName(); pkg = pkg.substring(pkg.lastIndexOf(".")+1); pkg = Character.toUpperCase(pkg.charAt(0))+pkg.substring(1); //Usr+User -> UsrUser -> usr_user return StringKit.enCodeUnderlined(pkg+c.getSimpleName()); }
然后在使用BeetlSQL反向生成Pojo的時候,使用Filter將數(shù)據(jù)表分門別類的生成到不同的包下面
//記得初始化SQLManager時要使用自己寫好的NameConvertionSQLManager sql = initSQLManager(); GenConfig config = new GenConfig(); sql.genALL("xxxx.pojo.usr", config, new GenFilter(){ @Override public boolean accept(String tableName) { return tableName.startsWith("usr_"); } }); sql.genALL("xxxx.pojo.mgr", config, new GenFilter(){ @Override public boolean accept(String tableName) { return tableName.startsWith("mgr_"); } }); sql.genALL("xxxx.pojo.base", config, new GenFilter(){ @Override public boolean accept(String tableName) { return tableName.startsWith("base_"); } });
最終會在項目中會生成以下Pojo
usr_user -> xxxx.pojo.usr.User.java
mgr_admin -> xxxx.pojo.mgr.Admin.java
base_city -> xxxx.pojo.base.City.java
即便是有類名沖突的,因為在不同的包下,所以也不會影響
就這樣一次輕松而又愉快的BeetlSQL自定義去除Pojo和表前綴被解決啦~
最后
之前所有項目的開發(fā)都是Jsp,幾乎沒使用過其他第三方模板庫,因為一次誤打誤撞讓我認識了Beetl,它的輕巧,它獨特的語法都深深的吸引了我,因為Beetl又讓我認識了BeetlSQL,抱著試一試的心態(tài)嘗試過之后總有一種把當前項目(使用的Hibernate)推倒重來的沖動,雖然最后因為項目的工程量和工期原因,當前的這個項目最終還是使用了Hibernate,但是我可以確定的是,在以后的開發(fā)道路上,如果允許,如果沒有能讓我更心動的通用Dao,BeetlSQL將會是我不二的選擇。
今天在這里分享這篇自定義NameConvertion的功能,其實并沒有多少技術(shù)含量,更多的是想做一次簡單的推廣,讓更多人知道Beetl,讓更多人愛上Beetl~

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
