在hibernate运用sql查询

在hibernate运用sql查询,第1张

给你一个我写的例子:

public

List<Statistics>

statistics(){

String

sql="select

"+

"sum(case

when

(ttype_code=3)

then

1

else

0

end),"+

"sum(case

when

(ttype_code=5)

then

1

else

0

end),"+

"sum(case

when

(ttype_code=8)

then

1

else

0

end),"+

"sum(case

when

(ttype_code=0)

then

1

else

0

end),"+

"sum(case

when

(ttype_code=6)

then

1

else

0

end),"+

"sum(case

when

(ttype_code=9)

then

1

else

0

end),"+

"count()"+

"from

cs_cust_complaint_data

t

where

tfrom_flag='2'";

List<Statistics>

stal

=

new

ArrayList<Statistics>();

SQLQuery

query

=

getSession()createSQLQuery(sql);

Object[]

obj

=

(Object[])queryuniqueResult();

BigDecimal

b0

=

(BigDecimal)obj[0];

BigDecimal

b1

=

(BigDecimal)obj[1];

BigDecimal

b2

=

(BigDecimal)obj[2];

BigDecimal

b3

=

(BigDecimal)obj[3];

BigDecimal

b4

=

(BigDecimal)obj[4];

BigDecimal

b5

=

(BigDecimal)obj[5];

BigDecimal

b6

=

(BigDecimal)obj[6];

Double

zs

=

b6doubleValue();

DecimalFormat

df=new

DecimalFormat("#00");

staladd(new

Statistics("卷烟营销",

b0longValue(),DoubleparseDouble(dfformat(b0doubleValue()/zs100))));

staladd(new

Statistics("专卖法规",

b1longValue(),DoubleparseDouble(dfformat(b1doubleValue()/zs100))));

staladd(new

Statistics("烟叶生产",

b2longValue(),DoubleparseDouble(dfformat(b2doubleValue()/zs100))));

staladd(new

Statistics("政风行风",

b3longValue(),DoubleparseDouble(dfformat(b3doubleValue()/zs100))));

staladd(new

Statistics("人事劳资",

b4longValue(),DoubleparseDouble(dfformat(b4doubleValue()/zs100))));

staladd(new

Statistics("其他咨询",

b5longValue(),DoubleparseDouble(dfformat(b5doubleValue()/zs100))));

return

stal;

}

BigDecimal的setScale(int newScale,int roundingMode)方法

其中roundingMode 表示要应用的舍入模式。

可选一下的值:

ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_UNNECESSARY

例如:保留两位小数并四舍五入

BigDecimal bd1 = new BigDecimal(121231230023422634);

BigDecimal bd2 = bd1setScale(2,BigDecimalROUND_HALF_UP);

Systemoutprintln("bd2--------" + bd2);

输出结果是:bd2--------1212312300234226

circle 圆?

radian 还是 radius 半径?

用圆形类 一个参数,求 矩形周长面积? 还是求圆形周长面积?

如果是求圆形面积周长,可以参考下边:

import javamathBigDecimal;

//圆形类

public class Circle {

//半径

private double radius = 0;

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

thisradius = radius;

}

//获取面积 = π r r; 使用Math中的bigdecimal类进行小数数字计算,结果取值 两位小数,四舍五入

public double getArea(){

BigDecimal r = new BigDecimal(radius);

r = rmultiply(r)multiply(new BigDecimal(MathPI))setScale(2,BigDecimalROUND_HALF_UP);

return rdoubleValue();

}

//获取圆形周长 = π d = 2πr;用Math中的bigdecimal类进行小数数字计算,结果取值 两位小数,四舍五入

public double getCircumference(){

BigDecimal d = new BigDecimal(radius 2);

d = dmultiply(new BigDecimal(MathPI))setScale(2,BigDecimalROUND_HALF_UP);

return ddoubleValue();

}

}

首先楼上那两位对于普通的使用场景来说基本是正确的。但是在一些需要精确结果的场景中(比如货币金额计算),double就会有问题了,比如:

(int)102399999999999999=1024;

再比如:

double n1=003;

double n2=002;

double n3=n1-n2;

Systemoutprintln(n3);//0009999999999999998

所以如果需要精确安全的 *** 作的话,需要用到这个类:javamathBigDecimal

调用它的divideAndRemainder方法来进行计算,该计算可以理解为:进行一次保留余数的除法运算。结果会返回一个BigDecimal类型且长度为2的数组,0下标的元素是该计算的商,1下标的元素是该结果的余数。

BigDecimal n4=new BigDecimal("102399999999999999");

BigDecimal [] result=n4divideAndRemainder(BigDecimalvalueOf(1));

Systemoutprintln(result[1]toString());//099999999999999

注意,构造BigDecimal对象的时候我使用的是字符串String类型的“102399999999999999

”,如果直接写102399999999999999的话会被认为是double类型的数,然后double类型的该值为10240,所以给字符串类型的值可以保证BigDecimal对象接收到的数就准确无误是我们心里想的数。

JAVA中Resultset是一个类 而不是一个方法。记住啊。

结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有 *** 纵数据的功能,可能完成对数据的更新等。

结果集读取数据的方法主要是getXXX(),它的参数可以是整型,表示第几列(是从1开始的),还可以是列名。返回的是对应的XXX类型的值。如果对应那列时空值,XXX是对象的话返回XXX型的空值,如果XXX是数字类型,如Float等则返回0,boolean返回false。使用getString()可以返回所有的列的值,不过返回的都是字符串类型的。XXX可以代表的类型有:基本的数据类型如整型(int),布尔型(Boolean),浮点型(Float,Double)等,比特型(byte),还包括一些特殊的类型,如:日期类型(javasqlDate),时间类型(javasqlTime),时间戳类型(javasqlTimestamp),大数型(BigDecimal和BigInteger等)等。还可以使用getArray(int colindex/String columnname),通过这个方法获得当前行中,colindex所在列的元素组成的对象的数组。使用getAsciiStream(

int colindex/String colname)可以获得该列对应的当前行的ascii流。也就是说所有的getXXX方法都是对当前行进行 *** 作。

结果集从其使用的特点上可以分为四类,这四类的结果集的所具备的特点都是和Statement语句的创建有关,因为结果集是通过Statement语句执行后产生的,所以可以说,结果集具备何种特点,完全决定于Statement,当然我是说下面要将的四个特点,在Statement创建时包括三种类型。首先是无参数类型的,他对应的就是下面要介绍的基本的ResultSet对应的Statement。下面的代码中用到的Connection并没有对其初始化,变量conn代表的就是Connection对应的对象。SqlStr代表的是响应的SQL语句。

1、 最基本的ResultSet。

之所以说是最基本的ResultSet是因为,这个ResultSet他起到的作用就是完成了查询结果的存储功能,而且只能读去一次,不能够来回的滚动读取。这种结果集的创建方式如下:

Statement st = connCreateStatement

ResultSet rs = StatementexcuteQuery(sqlStr);

由于这种结果集不支持,滚动的读去功能所以,如果获得这样一个结果集,只能使用它里面的next()方法,逐个的读去数据。

2 可滚动的ResultSet类型。

这个类型支持前后滚动取得纪录next()、previous(),回到第一行first(),同时还支持要去的ResultSet中的第几行absolute(int n),以及移动到相对当前行的第几行relative(int n),要实现这样的ResultSet在创建Statement时用如下的方法。

Statement st = conncreateStatement(int resultSetType, int resultSetConcurrency)

ResultSet rs = stexecuteQuery(sqlStr)

其中两个参数的意义是:

resultSetType是设置ResultSet对象的类型可滚动,或者是不可滚动。取值如下:

ResultSetTYPE_FORWARD_ONLY只能向前滚动

ResultSetTYPE_SCROLL_INSENSITIVE和ResultTYPE_SCROLL_SENSITIVE这两个方法都能够实现任意的前后滚动,使用各种移动的ResultSet指针的方法。二者的区别在于前者对于修改不敏感,而后者对于修改敏感。

resultSetConcurency是设置ResultSet对象能够修改的,取值如下:

ResultSetCONCUR_READ_ONLY 设置为只读类型的参数。

ResultSetCONCUR_UPDATABLE 设置为可修改类型的参数。

所以如果只是想要可以滚动的类型的Result只要把Statement如下赋值就行了。

Statement st = conncreateStatement(ResultTYPE_SCROLL_INSENITIVE,

ResultSetCONCUR_READ_ONLY);

ResultSet rs = stexcuteQuery(sqlStr);

用这个Statement执行的查询语句得到的就是可滚动的ResultSet。

3、 可更新的ResultSet

这样的ResultSet对象可以完成对数据库中表的修改,但是我知道ResultSet只是相当于数据库中表的视图,所以并不时所有的ResultSet只要设置了可更新就能够完成更新的,能够完成更新的ResultSet的SQL语句必须要具备如下的属性:

a、只引用了单个表。

b、不含有join或者group by子句。

c、那些列中要包含主关键字。

具有上述条件的,可更新的ResultSet可以完成对数据的修改,可更新的结果集的创建方法是:

Statement st = createstatement(ResultTYPE_SCROLL_INSENSITIVE,ResultCONCUR_UPDATABLE)

4、 可保持的ResultSet

正常情况下如果使用Statement执行完一个查询,又去执行另一个查询时这时候第一个查询的结果集就会被关闭,也就是说,所有的Statement的查询对应的结果集是一个,如果调用Connection的commit()方法也会关闭结果集。可保持性就是指当ResultSet的结果被提交时,是被关闭还是不被关闭。JDBC20和10提供的都是提交后ResultSet就会被关闭。不过在JDBC30中,我们可以设置ResultSet是否关闭。要完成这样的ResultSet的对象的创建,要使用的Statement的创建要具有三个参数,这个Statement的创建方式也就是,我所说的Statement的第三种创建方式。如下:

Statement st=createStatement(int resultsetscrollable,int resultsetupdateable,int resultsetSetHoldability)

ResultSet rs = stexcuteQuery(sqlStr);

前两个参数和两个参数的createStatement方法中的参数是完全相同的,这里只介绍第三个参数:

resultSetHoldability表示在结果集提交后结果集是否打开,取值有两个:

ResultSetHOLD_CURSORS_OVER_COMMIT:表示修改提交时,不关闭数据库。

ResultSetCLOSE_CURSORS_AT_COMMIT:表示修改提交时ResultSet关闭。

package entity;

import javamathBigDecimal;

import comkettascommonSystemIn;

public class MortgageLoan {

//利率

private double providentFund_number;

private double business_number;

public double getProvidentFund_number() {

return providentFund_number;

}

public void setProvidentFund_number(double providentFund_number) {

thisprovidentFund_number = providentFund_number;

}

public double getBusiness_number() {

return business_number;

}

public void setBusiness_number(double business_number) {

thisbusiness_number = business_number;

}

//首付,有默认值

private double providentFund_firstPay=03;

private double business_firstPay=02;

private double mix_firstPay=03;

public double getMix_firstPay() {

return mix_firstPay;

}

public void setMix_firstPay(double mix_firstPay) {

thismix_firstPay = mix_firstPay;

}

public double getProvidentFund_firstPay() {

return providentFund_firstPay;

}

public void setProvidentFund_firstPay(double providentFund_firstPay) {

thisprovidentFund_firstPay = providentFund_firstPay;

}

public double getBusiness_firstPay() {

return business_firstPay;

}

public void setBusiness_firstPay(double business_firstPay) {

thisbusiness_firstPay = business_firstPay;

}

//费用,比如:保险费:房款总额的2% 贷款税:贷款总额的005% 贷款公证费:贷款总额的03%

private double insurance=002;

private double taxLoans=00005;

private double notaryFees=0003;

private double cost=insurance+taxLoans+notaryFees;

public double getInsurance() {

return insurance;

}

public void setInsurance(double insurance) {

thisinsurance = insurance;

}

public double getTaxLoans() {

return taxLoans;

}

public void setTaxLoans(double taxLoans) {

thistaxLoans = taxLoans;

}

public double getNotaryFees() {

return notaryFees;

}

public void setNotaryFees(double notaryFees) {

thisnotaryFees = notaryFees;

}

// private double

/

购房按揭计算

@param total,style,month,maxMoney

@return BigDecimal[]

first value is The first payment

second value is Monthly payment

/

public BigDecimal[] getMoney(int total,int style,int month,int maxMoney){

if(total<0||style<0||month<0||maxMoney<0){

Systemoutprintln("输入的参数错误!");

return null;

}

//需要改进

if(month>60){

thissetProvidentFund_number(00405);

thissetBusiness_number(00504);

}else{

thissetProvidentFund_number(0036);

thissetBusiness_number(00477);

}

BigDecimal[] money=new BigDecimal[2];

switch(style){

case 1:money=thisprovidentFund(total, month);break;

case 2:money=thisbusiness(total, month);break;

case 3:money=thismix(total, month, maxMoney);break;

default:Systemoutprintln("您选择的按揭方式错误");

}

return money;

}

//公积金贷款方式

private BigDecimal[] providentFund(int total,int month){

BigDecimal[] money=new BigDecimal[2];

money[0]=new BigDecimal(total(providentFund_firstPay+cost));

BigDecimal restMoney=new BigDecimal(total(1-providentFund_firstPay));

restMoney=restMoneyadd(new BigDecimal(total(1-providentFund_firstPay)providentFund_numbermonth));

money[1]=restMoneydivide(new BigDecimal(month),2,BigDecimalROUND_HALF_UP);

return money;

}

//商业性贷款方式

private BigDecimal[] business(int total,int month){

BigDecimal[] money=new BigDecimal[2];

money[0]=new BigDecimal(total(business_firstPay+cost));

BigDecimal restMoney=new BigDecimal(total(1-business_firstPay));

restMoney=restMoneyadd(new BigDecimal(total(1-business_firstPay)business_numbermonth));

money[1]=restMoneydivide(new BigDecimal(month),2,BigDecimalROUND_HALF_UP);

return money;

}

//混合型贷款方式

private BigDecimal[] mix(int total,int month,int maxMoney){

BigDecimal[] money=new BigDecimal[2];

money[0]=new BigDecimal(total(mix_firstPay+cost));

BigDecimal restMoney=new BigDecimal(total(1-mix_firstPay));

restMoney=restMoneyadd(new BigDecimal(maxMoneyprovidentFund_numbermonth

+(total(1-mix_firstPay)-maxMoney)business_numbermonth));

money[1]=restMoneydivide(new BigDecimal(month),2,BigDecimalROUND_HALF_UP);

return money;

}

public static void main(String[] args){

MortgageLoan mm=new MortgageLoan();

Systemoutprintln("请输入贷款的总金额 :");

Integer total=SystemInreadInt();

Systemoutprintln("请输入贷款的方式:");

Systemoutprintln("1:公积金贷款");

Systemoutprintln("2:商业性贷款");

Systemoutprintln("3:混合型贷款");

Integer style=SystemInreadInt();

Systemoutprintln("请输入贷款的期限:");

Integer month=SystemInreadInt();

Integer maxMoney=0;

if(style==3){

Systemoutprintln("请输入最大公积金贷款限额:");

maxMoney=SystemInreadInt();

}

BigDecimal[] bd=mmgetMoney(total, style, month, maxMoney);

Systemoutprintln("您需要首付 : "+bd[0]);

Systemoutprintln("您需要月付 : "+bd[1]);

}

}

comkettascommonSystemIn;这个类你可能没有,你就修改下主函数,直接手动输入数字就可以了。

SQL 分组关键字 Group by 在汇总,求和,等函数中可以出现多字段假如定义一个表 A,表中有字段 a nvarchar 类型,b 字段int 类型select a,sum(b) as b from Agroup by a上列SQL就是对一个字段分组,但是取多个字段数据

以上就是关于在hibernate运用sql查询全部的内容,包括:在hibernate运用sql查询、BigDecimal bd1 = new BigDecimal(12123123002342.2634); BigDecimal bd2 = bd1.setScale(2);、java按以下要求编写程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9816266.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存