java 数据绑定的几种方式及相关注意事项-持续更新

spring mvc 中会遇到各种数据绑定,有些不常用的,但是千万不要觉得不可以,没有什么是不可以的,只要能够想到,就可以。

数据绑定方式:

1.

注意:

当数据为包装类型的数字型时,如果Long hospitalId为空或-1,则Long departmentAllId= -1,如果Lang departmentAllId为空或-1 则Lang hospitalId = -1,既a和b不能同时存在。

如果不传hospitalId,那么在null和数字之间的判断极可能出现空指针,因为如果null!=null 后 使用==和数字判断时,hospitalId会做类型转换,从而导致空指针。

    if(hospitalId!=null && hospitalId!=-1){
            departmentAllId = -1;
        } else {
            departmentId = -1;
        }

2. 对象多个属性为集合数据

  json: 

//String json = "{'signedSummaryId':1,'tilte':'bbbbb','archiveDate':'1999-2-2','createdDoctor':1001,'createdDoctorName':'ccccc','main_suit':[{'content':'1111111','pic':'1111111','voice':'1111111','archiveTypeName':'主诉','archiveType':1}],'body_check':[{'content':'2222222','pic':'2222222','voice':'222222','archiveTypeName':'查体','archiveType':2}],'diagnosis':[{'content':'3333','pic':'3333','voice':'333','archiveTypeName':'诊断','archiveType':3}],'recipel':[{'content':'444','pic':'44','voice':'44444','archiveTypeName':'处方','archiveType':4},{'content':'444aaa','pic':'44aaa','voice':'44444aaa','archiveTypeName':'处方','archiveType':4}],'report':[{'content':'555','pic':'55555','voice':'5555','archiveTypeName':'报告单','archiveType':5},{'content':'55aaa','pic':'555aaa','voice':'5555aaa','archiveTypeName':'报告单','archiveType':5}],'discharge_abstract':[{'content':'7777','pic':'7777','voice':'77777','archiveTypeName':'出院小结','archiveType':7}],'proof_of_diagnosis':[{'content':'8888','pic':'888','voice':'8888','archiveTypeName':'诊疗证明','archiveType':8}],'up_referral':[{'content':'9999','pic':'999999','voice':'99999','archiveTypeName':'上转','archiveType':9}],'down_referral':[{'content':'1010110101','pic':'1010101010','voice':'10100110','archiveTypeName':'下转','archiveType':10}],'follow_Up':[{'content':'666','pic':'3666','voice':'3666','archiveTypeName':'随诊','archiveType':6}]}";

"{
    'signedSummaryId': 1,
    'tilte': 'bbbbb',
    'archiveDate': '1999-2-2',
    'createdDoctor': 1001,
    'createdDoctorName': 'ccccc',
    'main_suit': [
        {
            'content': '1111111',
            'pic': '1111111',
            'voice': '1111111',
            'archiveTypeName': '主诉',
            'archiveType': 1
        }
    ],
    'body_check': [
        {
            'content': '2222222',
            'pic': '2222222',
            'voice': '222222',
            'archiveTypeName': '查体',
            'archiveType': 2
        }
    ],
    'diagnosis': [
        {
            'content': '3333',
            'pic': '3333',
            'voice': '333',
            'archiveTypeName': '诊断',
            'archiveType': 3
        }
    ],
    'recipel': [
        {
            'content': '444',
            'pic': '44',
            'voice': '44444',
            'archiveTypeName': '处方',
            'archiveType': 4
        },
        {
            'content': '444aaa',
            'pic': '44aaa',
            'voice': '44444aaa',
            'archiveTypeName': '处方',
            'archiveType': 4
        }
    ],
    'report': [
        {
            'content': '555',
            'pic': '55555',
            'voice': '5555',
            'archiveTypeName': '报告单',
            'archiveType': 5
        },
        {
            'content': '55aaa',
            'pic': '555aaa',
            'voice': '5555aaa',
            'archiveTypeName': '报告单',
            'archiveType': 5
        }
    ],
    'discharge_abstract': [
        {
            'content': '7777',
            'pic': '7777',
            'voice': '77777',
            'archiveTypeName': '出院小结',
            'archiveType': 7
        }
    ],
    'proof_of_diagnosis': [
        {
            'content': '8888',
            'pic': '888',
            'voice': '8888',
            'archiveTypeName': '诊疗证明',
            'archiveType': 8
        }
    ],
    'up_referral': [
        {
            'content': '9999',
            'pic': '999999',
            'voice': '99999',
            'archiveTypeName': '上转',
            'archiveType': 9
        }
    ],
    'down_referral': [
        {
            'content': '1010110101',
            'pic': '1010101010',
            'voice': '10100110',
            'archiveTypeName': '下转',
            'archiveType': 10
        }
    ],
    'follow_Up': [
        {
            'content': '666',
            'pic': '3666',
            'voice': '3666',
            'archiveTypeName': '随诊',
            'archiveType': 6
        }
    ]
}"

com.google.gson.Gson

Gson gson = new Gson();
ArchiveAddModel archiveAddModel = gson.fromJson(json, ArchiveAddModel.class);

 

posted @ 2016-08-10 19:11  yangfei969  阅读(651)  评论(0编辑  收藏  举报