完成以下工具函数的代码编写 功能需求: 1. 传入数据对象列表,生成对应的数据库insert语句。 2. 生成数据库语句的可以应用于数据库MySQL 5.7 版本。 3. 生成对应的单元测试案例。 4. 代码要求详尽的注释 代码要求: 1. 程序实现基于java语言,jdk版本要求为1.8. 2. 入参为List集合,集合内的内容为基于JPA规范的实体类对象,数据库字段与数据库名基于注解在实体类上注释。 3. 不需要考虑数据库表间物理外键的场景。
时间: 2024-03-03 14:48:13 浏览: 113
将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本
下面是符合要求的Java代码:
```java
import java.lang.reflect.Field;
import java.util.List;
public class SqlGenerator {
/**
* 生成insert语句
* @param list 数据对象列表
* @param <T> 数据对象类型
* @return insert语句
*/
public static <T> String generateInsertSql(List<T> list) {
if (list == null || list.isEmpty()) {
throw new IllegalArgumentException("list不能为空");
}
T first = list.get(0);
String tableName = getTableName(first);
StringBuilder sb = new StringBuilder();
sb.append("insert into ").append(tableName).append("(");
Field[] fields = first.getClass().getDeclaredFields();
for (Field field : fields) {
String columnName = getColumnName(field);
sb.append(columnName).append(",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(") values");
for (T data : list) {
sb.append("(");
for (Field field : fields) {
Object value = getValue(data, field);
sb.append("'").append(value).append("',");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("),");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(";");
return sb.toString();
}
/**
* 生成单元测试案例
* @param list 数据对象列表
* @param <T> 数据对象类型
* @return 单元测试案例
*/
public static <T> String generateUnitTest(List<T> list) {
StringBuilder sb = new StringBuilder();
sb.append("import org.junit.Test;\n");
sb.append("import org.junit.runner.RunWith;\n");
sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
sb.append("import org.springframework.boot.test.context.SpringBootTest;\n");
sb.append("import org.springframework.test.context.junit4.SpringRunner;\n");
sb.append("import javax.persistence.EntityManager;\n");
sb.append("import javax.persistence.PersistenceContext;\n");
sb.append("import javax.transaction.Transactional;\n");
sb.append("import java.util.Arrays;\n");
sb.append("import java.util.List;\n");
sb.append("\n");
sb.append("@RunWith(SpringRunner.class)\n");
sb.append("@SpringBootTest\n");
sb.append("@Transactional\n");
sb.append("public class ").append(getClassName(list)).append(" {\n");
sb.append("\n");
sb.append(" @PersistenceContext\n");
sb.append(" private EntityManager entityManager;\n");
sb.append("\n");
sb.append(" @Autowired\n");
sb.append(" private ").append(getRepositoryName(list)).append(" ").append(getRepositoryVariable(list)).append(";\n");
sb.append("\n");
sb.append(" @Test\n");
sb.append(" public void testInsert() {\n");
sb.append(" List<").append(getEntityName(list)).append("> list = Arrays.asList(\n");
for (T data : list) {
sb.append(" new ").append(getEntityName(list)).append("(");
Field[] fields = data.getClass().getDeclaredFields();
for (Field field : fields) {
Object value = getValue(data, field);
sb.append(value).append(",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append("),\n");
}
sb.deleteCharAt(sb.length() - 2);
sb.append(" );\n");
sb.append(" ").append(getRepositoryVariable(list)).append(".saveAll(list);\n");
sb.append(" }\n");
sb.append("\n");
sb.append("}");
return sb.toString();
}
private static <T> String getTableName(T data) {
Class<?> clazz = data.getClass();
String tableName = clazz.getSimpleName();
if (clazz.isAnnotationPresent(javax.persistence.Table.class)) {
tableName = clazz.getAnnotation(javax.persistence.Table.class).name();
}
return tableName;
}
private static String getColumnName(Field field) {
String columnName = field.getName();
if (field.isAnnotationPresent(javax.persistence.Column.class)) {
columnName = field.getAnnotation(javax.persistence.Column.class).name();
}
return columnName;
}
private static <T> Object getValue(T data, Field field) {
field.setAccessible(true);
try {
return field.get(data);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
private static <T> String getClassName(List<T> list) {
T data = list.get(0);
Class<?> clazz = data.getClass();
return clazz.getSimpleName() + "Test";
}
private static <T> String getEntityName(List<T> list) {
T data = list.get(0);
Class<?> clazz = data.getClass();
return clazz.getSimpleName();
}
private static <T> String getRepositoryName(List<T> list) {
T data = list.get(0);
Class<?> clazz = data.getClass();
return clazz.getSimpleName() + "Repository";
}
private static <T> String getRepositoryVariable(List<T> list) {
T data = list.get(0);
Class<?> clazz = data.getClass();
String repositoryName = clazz.getSimpleName() + "Repository";
return repositoryName.substring(0, 1).toLowerCase() + repositoryName.substring(1);
}
}
```
使用时可以按照如下代码示例:
```java
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 构造数据对象列表
User user1 = new User(1, "张三", "男");
User user2 = new User(2, "李四", "女");
List<User> userList = Arrays.asList(user1, user2);
// 生成insert语句
String insertSql = SqlGenerator.generateInsertSql(userList);
System.out.println(insertSql);
// 生成单元测试案例
String unitTest = SqlGenerator.generateUnitTest(userList);
System.out.println(unitTest);
}
}
@Table(name = "user")
class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "gender")
private String gender;
public User() {}
public User(Integer id, String name, String gender) {
this.id = id;
this.name = name;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
```
阅读全文