Java使用ORM Bee自动生成Javabean.
配置数据库连接,添加了pom.xml依赖后,就可以写Java代码,自动生成Javabean了.
可参考:https://gitee.com/automvc/bee
https://github.com/automvc/bee
还可以生成字段文件, 这样可以避免硬编码引用字段,速度也比反射快.
package org.teasoft.exam.bee.osql.autogen;
import org.teasoft.bee.osql.BeeException;
import org.teasoft.honey.osql.autogen.GenBean;
import org.teasoft.honey.osql.autogen.GenConfig;
public class GenBeanSimple3 {
public static void main(String[] args) {
try {
GenConfig config = new GenConfig();
config.setGenToString(true);// 生成toString方法
config.setGenComment(true); // 可生成注释
config.setCommentPlace(1); // 注释的位置
GenBean genBean = new GenBean(config);
// 生成字段文件, 这样可以避免硬编码引用字段,速度也比反射快.
config.setGenFieldFile(true);
config.setOverride(true); // 是否覆盖原来的文件
config.setUpperFieldNameInFieldFile(true);
genBean.genSomeBeanFile("exam");
// it is easy way. 最简单的用法,所有的都用默认配置.
// new GenBean().genSomeBeanFile("orderhistory");
} catch (BeeException e) {
e.printStackTrace();
}
}
}
DB:
生成的Javabean:
package entity;
import java.io.Serializable;
/**
* @author Honey
* Create on 2024-12-13 23:20:08
*/
public class Exam implements Serializable {
private static final long serialVersionUID = 1590598107298L;
private Long id;
private Long userid;
private String lastName;
private String firstName;
private String birthDate;
private String sex;
private Long groupId;
private String groupName;
private String checkDate;
private String checkTime;
private String operator;
private String updatetime;
private String checkData;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getUserid() {
return userid;
}
public void setUserid(Long userid) {
this.userid = userid;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getCheckDate() {
return checkDate;
}
public void setCheckDate(String checkDate) {
this.checkDate = checkDate;
}
public String getCheckTime() {
return checkTime;
}
public void setCheckTime(String checkTime) {
this.checkTime = checkTime;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getUpdatetime() {
return updatetime;
}
public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}
public String getCheckData() {
return checkData;
}
public void setCheckData(String checkData) {
this.checkData = checkData;
}
public String toString() {
StringBuilder str = new StringBuilder();
str.append("Exam[");
str.append("id=").append(id);
str.append(",userid=").append(userid);
str.append(",lastName=").append(lastName);
str.append(",firstName=").append(firstName);
str.append(",birthDate=").append(birthDate);
str.append(",sex=").append(sex);
str.append(",groupId=").append(groupId);
str.append(",groupName=").append(groupName);
str.append(",checkDate=").append(checkDate);
str.append(",checkTime=").append(checkTime);
str.append(",operator=").append(operator);
str.append(",updatetime=").append(updatetime);
str.append(",checkData=").append(checkData);
str.append("]");
return str.toString();
}
}
生成字段文件
config.setUpperFieldNameInFieldFile(true); //属性名用大写.
package entity.field;
/**
* Relative field name for Javabean Exam
* @author Honey
* Create on 2024-12-13 22:30:39
*/
public interface Exam_F {
String ID = "id";
String USERID = "userid";
String LAST_NAME = "lastName";
String FIRST_NAME = "firstName";
String BIRTH_DATE = "birthDate";
String SEX = "sex";
String GROUP_ID = "groupId";
String GROUP_NAME = "groupName";
String CHECK_DATE = "checkDate";
String CHECK_TIME = "checkTime";
String OPERATOR = "operator";
String UPDATETIME = "updatetime";
String CHECK_DATA = "checkData";
String ENTITY_NAME = "Exam";
String TABLE_NAME = "exam";
String ALL_NAMES = "id,userid,lastName,firstName,birthDate,sex,groupId,groupName,checkDate,checkTime,operator,updatetime,checkData";
}
另外一种风格. 属性名和字符串是一样的. 用一样就好了,可必让自己那么累,同时看两种风格的命名呢,达到去硬编码的目的就好.
package entity.field;
/**
* Relative field name for Javabean Exam
* @author Honey
* Create on 2024-12-13 22:28:20
*/
public interface Exam_F {
String id = "id";
String userid = "userid";
String lastName = "lastName";
String firstName = "firstName";
String birthDate = "birthDate";
String sex = "sex";
String groupId = "groupId";
String groupName = "groupName";
String checkDate = "checkDate";
String checkTime = "checkTime";
String operator = "operator";
String updatetime = "updatetime";
String checkData = "checkData";
String ENTITY_NAME = "Exam";
String TABLE_NAME = "exam";
String ALL_NAMES = "id,userid,lastName,firstName,birthDate,sex,groupId,groupName,checkDate,checkTime,operator,updatetime,checkData";
}