Springboot转换Bean工具类

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caiyi.yu
 */
public class ConvertBeanUtil {
    // 转换单个对象
    public static <T, R> R convertBean(T source, Class<R> targetClass) {
        if (source == null) {
            return null;
        }
        try {
            R target = targetClass.getDeclaredConstructor().newInstance();
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    // 转换 List 对象
    public static <T, R> List<R> convertListBean(List<T> sourceList, Class<R> targetClass) {
        if (sourceList == null) {
            return null;
        }
        List<R> targetList = new ArrayList<>();
        for (T source : sourceList) {
            R target = convertBean(source, targetClass);
            targetList.add(target);
        }
        return targetList;
    }
}
  • 12
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!我可以为您提供一个工具类来将一个XML文件写入指定的数据库。以下是示例代码: ```java import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class XMLToDatabaseUtil { public static void main(String argv[]) { String xmlFilePath = "path/to/xml/file"; String databaseUrl = "jdbc:mysql://localhost:3306/mydatabase"; String username = "root"; String password = "mypassword"; try { // Load JDBC driver Class.forName("com.mysql.jdbc.Driver"); // Connect to database Connection conn = DriverManager.getConnection(databaseUrl, username, password); // Create a prepared statement to insert data into the database String sql = "INSERT INTO mytable (name, email, phone) VALUES (?, ?, ?)"; PreparedStatement preparedStatement = conn.prepareStatement(sql); // Parse the XML file to extract information to be added to the database File file = new File(xmlFilePath); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); // Traverse the XML document and extract required data doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("person"); for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); String name = element.getAttribute("name"); String email = element.getElementsByTagName("email").item(0).getTextContent(); String phone = element.getElementsByTagName("phone").item(0).getTextContent(); // Set parameters in prepared statement and execute it preparedStatement.setString(1, name); preparedStatement.setString(2, email); preparedStatement.setString(3, phone); preparedStatement.executeUpdate(); } // Close prepared statement and database connection preparedStatement.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,您需要将“path/to/xml/file”替换为实际的XML文件路径,以及将“mydatabase”替换为您实际的数据库名称,“root”和“mypassword”替换为数据库的用户名和密码,以及将“mytable”替换为您要将数据添加到其中的实际数据库表名。 希望这有助于您将XML文件写入指定的数据库!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值