输出示例
15:23:30.846 [main] INFO com.example.myutilsproject.utils.MybatisGenerateSQLTags -- MybatisPlus SQL标签:
<sql id="user_base_column">
id, user_name, password, real_name, phone, email, dept_id, create_time, create_by, update_time, update_by, deleted
</sql>
15:23:30.852 [main] INFO com.example.myutilsproject.utils.MybatisGenerateSQLTags -- Mybatis SQL标签:
<sql id="user_base_column">
id, user_name, password, real_name, phone, email, dept_id, create_time, create_by, update_time, update_by, deleted, roles, dept_name
</sql>
1. 实体类
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
/**
* @author: 苏
* @createDate: 2023-12-28 14:05
* @description:
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class User implements Serializable {
@TableId
private String id;
private String userName;
private String password;
private String realName;
private String phone;
private String email;
private String deptId;
private Timestamp createTime;
private String createBy;
private Timestamp updateTime;
private String updateBy;
@TableLogic
private Integer deleted;
@TableField(exist = false)
private List<Role> roles;
@TableField(exist = false)
private String deptName;
}
2. Mybatis-plus或Mybatis 生成 SQL 标签的工具类代码
import com.baomidou.mybatisplus.annotation.TableField;
import com.example.myutilsproject.entity.User;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* @author: 苏
* @createDate: 2023-12-28 13:53
* @description: Mybatisplus 生成 SQL 标签
*/
@Slf4j
public class MybatisGenerateSQLTags {
public static void main(String[] args) {
// 获取实体类的所有字段名,并生成对应的 <sql> 标签
String mybatisPlusColumns = generateSqlColumns(User.class, true);
String mybatisColumns = generateSqlColumns(User.class, false);
log.info("MybatisPlus SQL标签:\n{}", mybatisPlusColumns );
log.info("Mybatis SQL标签:\n{}", mybatisColumns );
}
private static String generateSqlColumns(Class<?> clazz, boolean isPlus) {
Field[] fields = clazz.getDeclaredFields();
String[] split = clazz.getName().split("\\.");
String name = split[split.length - 1].toLowerCase();
String columns = Arrays.stream(fields)
.filter(field -> !isPlus
|| field.getAnnotation(TableField.class) == null
|| field.getAnnotation(TableField.class).exist()
).
map(field -> CamelUnderConvUtils.convertCamelToUnderscore(field.getName()))
.collect(Collectors.joining(", "));
return String.format("<sql id=\"%s_base_column\">\n\t%s\n</sql>", name, columns);
}
}
3. 自动将驼峰命名转为下划线格式工具类
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author: 苏
* @createDate: 2023-12-28 14:10
* @description: 驼峰下划线转换工具类
*/
public class CamelUnderConvUtils {
public static String convertCamelToUnderscore(String camelCase) {
Pattern pattern = Pattern.compile("([a-z])([A-Z])");
Matcher matcher = pattern.matcher(camelCase);
StringBuilder buffer = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(buffer, matcher.group(1) + "_" + matcher.group(2).toLowerCase());
}
matcher.appendTail(buffer);
return buffer.toString().toLowerCase();
}
public static void main(String[] args) {
// 测试字符串
String camelCase = "mainProposalDept";
// 转换驼峰命名为下划线格式
String underscoreCase = convertCamelToUnderscore(camelCase);
System.out.println(underscoreCase); // 输出:main_proposal_dept
}
}