Hibernate设置映射BigDecimal类型的小数位数

版权声明: https://blog.csdn.net/caryyin/article/details/39476231

需要在@Column注解中,定义两个变量:precision,scale。

precision:小数精度(整数位+小数位)

scale:小数位数

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "my_big_decimal")
public class MyBigDecimal {
	int id;
	BigDecimal xiaoShu;

	@Id
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(precision = 23, scale = 6)
	public BigDecimal getXiaoShu() {
		return xiaoShu;
	}

	public void setXiaoShu(BigDecimal xiaoShu) {
		this.xiaoShu = xiaoShu;
	}
}

生成的数据表:

CREATE TABLE `my_big_decimal` (
  `id` int(11) NOT NULL,
  `xiaoShu` decimal(23,6) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


展开阅读全文

没有更多推荐了,返回首页