javaifnotnull
① java ibatis isPropertyAvailable跟isEmpty的使用怎么解决
isPropertyAvailable和isNotEmpty
这个两个属性非常有用
isPropertyAvailable:入参有这个属性
isNotEmpty:入参的这个属性不为空
入参一般是一个封装了数据的DTO
如果希望一个属性无论为何值都符合条件则使用isPropertyAvailable
如果希望一个属性只是不为空的时候才符合条件就用isNotEmpty
例子如下:
<update id="partner.update.back">
update partner
<dynamic prepend="set">
<isPropertyAvailable property="domain_url" prepend=",">domain_url=#domain_url#</isPropertyAvailable>
<isNotNull property="user_name"><isNotEmpty prepend="," property="user_name">user_name=#user_name#</isNotEmpty></isNotNull>
</dynamic>
where partner_id=#partner_id#
</update>
ibatis版本2.3,最新的myBatis没有时间研究,应该大同小异
② java notnull 怎么实现的
not null指判断某个对象非空
Object obj = new Object();
if(obj != null){
System.out.println("obj is not null");
}
③ java对象为空的判断
/**
*判断对象或对象数组中每一个对象是否为空:对象为null,字符序列长度为0,集合类、Map为empty
*
*@paramobj
*@return
*/
(Objectobj){
if(obj==null)
returntrue;
if(objinstanceofCharSequence)
return((CharSequence)obj).length()==0;
if(objinstanceofCollection)
return((Collection)obj).isEmpty();
if(objinstanceofMap)
return((Map)obj).isEmpty();
if(objinstanceofObject[]){
Object[]object=(Object[])obj;
if(object.length==0){
returntrue;
}
booleanempty=true;
for(inti=0;i<object.length;i++){
if(!isNullOrEmpty(object[i])){
empty=false;
break;
}
}
returnempty;
}
returnfalse;
}
应用场景:
读取excel文件,转化为一个二维数组:Object[][]arrays
但是excel中有空行,所以需要过滤Object[][]arrays中的空的一维数组:
Java代码
/***
*过滤数组中的空元素
*
*
*@paramarrays
*@return
*/
publicstaticObject[][]filterEmpty(Object[][]arrays){
intsumNotNull=0;
/***
*统计非空元素的总个数
*/
for(inti=0;i<arrays.length;i++){
Objectobject=arrays[i];
if(!ValueWidget.isNullOrEmpty(object)
&&!SystemUtil.isNullOrEmpty((Object[])object)){//判断元素是否为空
sumNotNull=sumNotNull+1;
}
}
Object[][]filtedObjs=newObject[sumNotNull][];
intindex=0;
for(inti=0;i<arrays.length;i++){
Object[]object_tmp=arrays[i];
if(!ValueWidget.isNullOrEmpty(object_tmp)
&&!SystemUtil.isNullOrEmpty((Object[])object_tmp)){//判断元素是否为空
filtedObjs[index++]=object_tmp;
}
}
returnfiltedObjs;
}
判断对象的所有成员变量是否为空
Java代码
/***
*Determinewhethertheobject'sfieldsareempty
*
*@paramobj
*@paramisExcludeZero:true:数值类型的值为0,则当做为空;----false:数值类型的值为0,则不为空
*
*@return
*@throwsSecurityException
*@
*@throwsNoSuchFieldException
*@throwsIllegalAccessException
*/
(Objectobj,booleanisExcludeZero)
throwsSecurityException,IllegalArgumentException,
NoSuchFieldException,IllegalAccessException{
if(ValueWidget.isNullOrEmpty(obj)){//对象本身就为null
returntrue;
}
List<Field>fieldList=ReflectHWUtils.getAllFieldList(obj.getClass());
booleanisNull=true;
for(inti=0;i<fieldList.size();i++){
Fieldf=fieldList.get(i);
ObjectpropertyValue=null;
try{
propertyValue=getObjectValue(obj,f);
}catch(NoSuchFieldExceptione){
e.printStackTrace();
}
if(!ValueWidget.isNullOrEmpty(propertyValue)){//字段不为空
if(){//是数字
if(!((Integer)propertyValue==0&&isExcludeZero)){
isNull=false;
break;
}
}elseif(propertyValueinstanceofDouble){//是数字
if(!((Double)propertyValue==0&&isExcludeZero)){
isNull=false;
break;
}
}elseif(propertyValueinstanceofFloat){//是数字
if(!((Float)propertyValue==0&&isExcludeZero)){
isNull=false;
break;
}
}elseif(propertyValueinstanceofShort){//是数字
if(!((Short)propertyValue==0&&isExcludeZero)){
isNull=false;
break;
}
}else{
isNull=false;
break;
}
}
}
returnisNull;
}
测试:
Java代码
@Test
publicvoidtest_isNullObject()throwsSecurityException,
IllegalArgumentException,NoSuchFieldException,
IllegalAccessException{
Person2p=newPerson2();
Assert.assertEquals(true,ReflectHWUtils.isNullObject(p,true));
Assert.assertEquals(false,ReflectHWUtils.isNullObject(p,false));
p.setAddress("beijing");
Assert.assertEquals(false,ReflectHWUtils.isNullObject(p,true));
Assert.assertEquals(false,ReflectHWUtils.isNullObject(p,false));
p.setAddress(null);
p.setId(0);
Assert.assertEquals(true,ReflectHWUtils.isNullObject(p,true));
Assert.assertEquals(false,ReflectHWUtils.isNullObject(p,false));
}
Person2源代码(省略getter,setter方法):
Java代码
importjava.sql.Timestamp;
publicclassPerson2{
privateintid;
privateintage;
privatedoubleweight;
privateStringpersonName;
privateTimestampbirthdate;
publicStringidentitify;
protectedStringaddress;
Stringphone;
}
④ java中if从句
strGender=JOptionPane.showInputDialog(null,"Enter your gender:m/f"); 这句的strGender改为gender
或把if ((gender=='f')&&(age>21)&&(height>=170)&&(weight<80)) 这句的gender改为strGender
⑤ 如何在查询语句中把空值(null),输出为0
MYSQL可用:
select cource.c_id,cource.c_name,cource.c_num,ifnull(student.count_c_id,'lattice') from cource
left join
(select c_id,count(s_id) as count_c_id from cource_student group by c_id) as student
on cource.c_id=student.c_id;
在遇到多张表查询时,很可能查一个关联数值时,并没有这条关联记录,所以查询到的结果是null,通常需要把这个结果处理成0或者其他。这时候就用isNULL(字段,0)。
(5)javaifnotnull扩展阅读
SQL NULL 值
NULL 值是遗漏的未知数据。默认地,表的列可以存放 NULL 值。
NULL 值的处理方式与其他值不同。
NULL 用作未知的或不适用的值的占位符。
注释:无法比较 NULL 和 0;它们是不等价的。
sql之null、空字符串、0的区别:
1、'' 表示空字符串,判断'' 用 ='' 或 <>'' ,
2、null表示空值,数值未知,没有两个相等的空值,判断用 is null 或 is not null
例如:tran_heating_id_!=5 想筛选出所有tran_heating_id_不是5的客户信息,但是这样并不能筛出tran_heating_id_为null的客户信息
(因为null是值不确定的情况),需要用is null筛选。
3、0表示值为‘0’。
⑥ java中怎样判断多个text文本不为空
判断咯 假如 t 是一个TextField
if( t.getText().equals("")==true)
就是这样比较 懂了? getText()方法返回的是String类 String类的equals()方法是比较值 而你如果直接用t.getText()==""的话就会比较它们在内存中的位置
⑦ Groovy Tip 3 如何在if条件语句中判断对象为空
Groovy Tip 3 如何在if条件语句中判断对象为空
在Java语言编程中,对对象的非空判断是一个永恒的话题。例如,经常需要对一个字符串进行如下的判断:
if(str!=null&&!str.equals(""))
{
......
}
输入这样的语句的确使人生厌,而且有时候还会忘掉输入“!str.equals("")”语句中的“!”导致代码出现逻辑错误。
而敏捷的Groovy语言开发就不需要担心这样的问题。同样的判断语句,只需要输入下面的代码:
def str = null
if(str)
{
println"str is not null"
}
else
{
println'str is null'
}
这个语句段的执行结果为:
str is null
可以看出,if(str)判断语句,当str为null的时候,不执行。可能要问,当str = ''的时候会怎样?
def str = ''
if(str)
{
println"str is not null"
}
else
{
println'str is null'
}
执行结果还是:
str is null
这样,可以把开头的那段Java代码改写成如下的代码了:
if(str)
{
......
}
这样就简洁多了。
除了字符串对象,那其他对象的非空判断?来看下面的例子:
def map = ['key1':'value1']
if(map)
{
println'map is not null'
}
else
{
println'map is null'
}
map.remove('key1')
if(map)
{
println'this time,map is not null'
}
else
{
println'this time,map is null'
}
执行结果为:
map is not null
this time,map is null
同样,来看看List对象:
def list = []
if(list)
{
println'list is not null'
}
else
{
println'list is null'
}
list<<'a'
if(list)
{
println'here, list is not null'
}
else
{
println'here, list is null too'
}
输出结果为:
list is null
here, list is not null
如果是Domain对象?
class Empl
{
String name
}
执行下面的语句:
Empl em = new Empl()
if(em)
{
println'em is not null'
}
else
{
println'em is null'
}
结果为:
em is not null
可以看出,对于Domain对象,只要该对象不是null,则if(em)条件为true。
⑧ if语句要怎么写 不是null 才不会报错
如果Text是字符串类型
可以用org.apache.commons.lang3.StringUtils包下的isEmpty方法判断是否为空
StringUtils.isNotEmpty(Text)
StringUtils.isEmpty(Text)
如果Text是对象
StringUtils.isEmpty(Objectstr)这个方法也能搞定。
如果Text是集合
使用org.apache.commons.collections.CollectionUtils下的
CollectionUtils.isEmpty(null):true
CollectionUtils.isEmpty(newArrayList()):true
CollectionUtils.isEmpty({a,b}):false
⑨ assert.notnull有什么用
一、用法:
1、首先:booleanExpression 参数是一个bool表达式。
2、当程序运行到该语句的时候,程序会检查booleanExpression 这个表达式是真还是假。
3、如果条件符合,程序继续运行下面的代码;
4、如果是不符合,那么持续运行会被卡在这里。不往下面走,并有程序弹出错误对话框,指示是由于booleanExpression 这个条件符合导致的报错。
二、作用:
1、ASSERT(booleanExpression )语句一般用来检查一些必须符合的条件,如果不符合条件,则不让程序继续运行下去。
2、assert是编译过程中判断语句是否成功(合法)的函数。
3、也是一种断言语句,主要用来调试程序。
⑩ java反射机制判断对象所有属性是否全部为空
private boolean checkObjFieldIsNotNull(Object obj){
try {
for (Field f : obj.getClass().getDeclaredFields()) {
f.setAccessible(true);
if (f.get(obj) != null) {
return true;
}
}
}catch (IllegalAccessException e){
}
return false;
}