Hibernate反向工程—java.math.BigDecimal替换

Struts2与Hibernate数据类型转换
本文介绍在使用Struts2框架时遇到的问题:从Oracle数据库中读取的Integer字段通过Hibernate反向工程生成的Java Bean字段为BigDecimal类型,导致Struts2中的XWork无法正确转换。文中提供了一种快速解决大量字段类型转换的方法。

       

        从oracle数据库中的integer字段通过hibernate的反向工程,生成的bean字段为java.math.BigDecimal类型。但是struts2框架中的xworks对从jsp页面穿过来的bean对象不能对java.math.BigDecimal类型从string类型转换到BigDecimal类型,但能转换为integer和long类型。因此要将BigDecimal类型改成integer和long类型

          现在的问题是如果我有很多字段类型需要转换的话,那我的替换工作量实在太大了,所以我用了一个好办法,很快完成整个项目所有的替换:

步骤如下:

1、

 

2、

 

3、

修改后的Product类: package com.warehouse.demo.entity; import javax.persistence.*; import java.math.BigDecimal; import java.time.LocalDateTime; @Entity @Table(name = "products") public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true) private String productCode; @Column(nullable = false) private String name; private String description; @Column(nullable = false) private BigDecimal price; @Column(nullable = false) private Integer stockQuantity; @ManyToOne @JoinColumn(name = "category_id") private Category category; @ManyToOne @JoinColumn(name = "supplier_id") private Supplier supplier; @Column(nullable = false) private LocalDateTime createTime; private LocalDateTime updateTime; // 构造方法 public Product() {} public Product(String productCode, String name, String description, BigDecimal price, Integer stockQuantity) { this.productCode = productCode; this.name = name; this.description = description; this.price = price; this.stockQuantity = stockQuantity; this.createTime = LocalDateTime.now(); } // Getter 和 Setter 方法 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getProductCode() { return productCode; } public void setProductCode(String productCode) { this.productCode = productCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public Integer getStockQuantity() { return stockQuantity; } public void setStockQuantity(Integer stockQuantity) { this.stockQuantity = stockQuantity; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } public Supplier getSupplier() { return supplier; } public void setSupplier(Supplier supplier) { this.supplier = supplier; } public LocalDateTime getCreateTime() { return createTime; } public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; } public LocalDateTime getUpdateTime() { return updateTime; } public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; } @PrePersist protected void onCreate() { createTime = LocalDateTime.now(); } @PreUpdate protected void onUpdate() { updateTime = LocalDateTime.now(); } @Override public String toString() { return "Product{" + "id=" + id + ", productCode='" + productCode + '\'' + ", name='" + name + '\'' + ", price=" + price + ", stockQuantity=" + stockQuantity + '}'; } }
最新发布
10-11
此异常表明 MyBatis 在尝试为 `Object[]` 数组寻找合适的构造函数,但未找到匹配的构造函数。`Object[]` 是一个数组类型,本身不存在接收多个特定类型参数的构造函数。以下是一些解决办法: ### 1. 直接使用数组元素 在 Mapper XML 文件或者注解 SQL 里,直接使用数组的元素,而非尝试把数组当作一个对象来处理。 ```xml <select id="yourMethod" resultType="YourResultType"> SELECT * FROM your_table WHERE column1 = #{array[0]} AND column2 = #{array[1]} AND ... </select> ``` 在 Java 代码中,可按如下方式调用: ```java List<Object[]> paramList = new ArrayList<>(); // 添加数据到 paramList yourMapper.yourMethod(paramList); ``` ### 2. 封装为自定义对象 把 `Object[]` 数组封装成自定义对象,如此 MyBatis 就能使用自定义对象的构造函数或者 setter 方法。 ```java public class YourCustomObject { private String field1; private String field2; private BigDecimal field3; // 其他字段 // 构造函数 public YourCustomObject(String field1, String field2, BigDecimal field3, ...) { this.field1 = field1; this.field2 = field2; this.field3 = field3; // 初始化其他字段 } // Getter 和 Setter 方法 public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } // 其他 Getter 和 Setter 方法 } ``` 在 Java 代码里,将 `Object[]` 转换为 `YourCustomObject`: ```java List<YourCustomObject> customObjectList = new ArrayList<>(); for (Object[] array : paramList) { YourCustomObject customObject = new YourCustomObject((String) array[0], (String) array[1], (BigDecimal) array[2], ...); customObjectList.add(customObject); } yourMapper.yourMethod(customObjectList); ``` 在 Mapper XML 文件或者注解 SQL 中,使用自定义对象的属性: ```xml <select id="yourMethod" resultType="YourResultType"> SELECT * FROM your_table WHERE column1 = #{field1} AND column2 = #{field2} AND ... </select> ``` ### 3. 使用 Map 把 `Object[]` 数组转换为 `Map`,这样 MyBatis 就能使用 `Map` 的键值对。 ```java List<Map<String, Object>> mapList = new ArrayList<>(); for (Object[] array : paramList) { Map<String, Object> map = new HashMap<>(); map.put("field1", array[0]); map.put("field2", array[1]); map.put("field3", array[2]); // 其他键值对 mapList.add(map); } yourMapper.yourMethod(mapList); ``` 在 Mapper XML 文件或者注解 SQL 中,使用 `Map` 的键: ```xml <select id="yourMethod" resultType="YourResultType"> SELECT * FROM your_table WHERE column1 = #{field1} AND column2 = #{field2} AND ... </select> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值