根据身份证获取信息的方法,基本都在这里了

这是一个Java工具类,用于根据身份证号码获取个人信息,包括校验身份证的合法性、获取性别、出生年月和年龄。此外,虽然未在代码中实现,但提到了可以根据身份证号获取籍贯的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.hzrc.acl.authentication.security.util;

import java.util.Calendar;

/**
 * 根据身份证号获取相关信息
 */
public class getInfoByIdentityCodeUtil {

    /**
     * 校验身份证的正确性
     * @param identityCard
     * @return
     */
    private static boolean getValidIdentityCard(String identityCard){

        if(identityCard == null || identityCard.length() == 0) return false;

        if(identityCard.length() != 18) return false;

        char[] identities = identityCard.toCharArray();
        int code = 0;
        int[] number = new int[]{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
        char[] numberArray = new char[]{'1','0','X','9','8','7','6','5','4','3','2'};
        for(int i =0 ; i < 17; i++)
        {
            if(identities[i] < '0' || identities[i] > '9')
            {
                return false;
            }
            code += (identities[i] -'0') * number[i];
        }
        code = code % 11;

        if(numberArray[code] != identities[17])
        {
            return false;
        }
        return true;
    }


    /**
     * 根据身份证号获取性别
     * @param identityCode
     * @return 0位置 1男生 2女生
     */
    public static Integer getGender(String identityCode) {

        if(identityCode == null || identityCode.length() == 0) return 0;

        int gender = 0;
        if(identityCode.length() == 18){

            //如果身份证号18位,取身份证号倒数第二位
            char c = identityCode.charAt(identityCode.length() - 2);
            gender = Integer.parseInt(String.valueOf(c));

        }else{

            //如果身份证号15位,取身份证号最后一位
            char c = identityCode.charAt(identityCode.length() - 1);
            gender = Integer.parseInt(String.valueOf(c));

        }

        if(gender % 2 == 1)   return 1;

        return 2;

    }


    /**
     * 根据身份证号获取出生年月
     * @param identityCode
     * @return
     */
    public static String getBirthday(String identityCode) {

        String birthday = null;

        if(identityCode == null || identityCode.length() == 0) return birthday;

        if (identityCode.length() == 15) {
            birthday = "19" + identityCode.substring(6, 8) + "-" + identityCode.substring(8, 10) + "-"
                    + identityCode.substring(10, 12);
        } else if (identityCode.length() == 18) {
            birthday = identityCode.substring(6, 10) + "-" + identityCode.substring(10, 12) + "-"
                    + identityCode.substring(12, 14);
        }

        return birthday;

    }

    /**
     * 根据身份证号获取年龄
     * @param identityCode
     * @return
     */
    public static Integer getAge(String identityCode) {

        int age = 0;

        if(identityCode == null || identityCode.length() == 0) return age;

        int year = Calendar.getInstance().get(Calendar.YEAR);

        if (identityCode.length() == 15) {
            age = (year - Integer.parseInt("19" + identityCode.substring(6, 8)));
        } else if (identityCode.length() == 18) {

            age = (year - Integer.parseInt(identityCode.substring(6, 10)));
        }

        return age;
    }


    /**
     * 根据身份证号获取籍贯
     * @param identityCode
     * @return
     */
    public static String getNaticePlace(String identityCode) {

        if(identityCode == null || identityCode.length() == 0) return null;

        int nativePlaceCode=Integer.parseInt(identityCode.substring(0, 6));

        return NativePlace.getNativePlace(nativePlaceCode);

    }


    public static void main(String[] args) {
        

    }





}



获取籍贯,因为所有地区信息太多了,就不放在这里了,需要的可以评论,私发给你。



知是行之始,行是知之成。

<think>我们正在处理一个需求:通过输入身份证号,在地图上显示对应的基本信息根据引用,我们有以下线索:1.引用[1]展示了一个登录方法的签名,使用`Map<String,String>`接收数据,并且有`HttpServletRequest`参数。2.引用[2]强调了在MyBatis中使用`<resultMap>`的重要性,而不是直接使用`resultType`,这样可以解耦数据库字段和对象字段。3.引用[3]描述了一个公租房管理系统的背景,这可能是我们当前系统的上下文。结合需求,我们需要:-用户输入身份证号(作为查询条件)-根据身份证号查询用户的基本信息(可能包括地址信息,用于地图显示)-将查到的地址信息在地图上显示(可能需要集成地图服务,如百度地图、高德地图)步骤:1.前端:提供一个输入框让用户输入身份证号,并提交。2.后端:接收身份证号,查询数据库,获取用户的基本信息(包括地址等)。3.将地址信息传递给前端,前端调用地图API显示地图并标记位置。但是,需要注意的是,身份证号本身包含地址信息(前6位为地区代码),但我们可能需要完整的地址(如省市区)来定位,或者用户信息中存储了详细的地址字符串。因此,我们可能有两种方式:a)通过身份证号前6位,查询地区代码对应的地址(但这样只能定位到区县级,不够精确,且用户当前实际居住地址可能不同)b)在用户信息中存储详细地址(如具体到门牌号),这样我们就可以通过详细地址进行地理编码,得到经纬度。考虑到系统需求(公租房管理系统),第二种方式更可能,因为公租房有具体的房源地址。因此,假设我们在数据库的用户信息表(或者公租房租赁信息表)中存储了用户的详细住址。后端实现(Java)步骤:1.接收请求参数(身份证号)-可以使用一个Map接收,如引用[1]所示,但建议使用DTO对象接收,更清晰。-或者使用`@RequestParam`注解。2.根据身份证号查询用户信息-使用MyBatis,按照引用[2]的建议,定义好`<resultMap>`,不要使用resultType。-编写Mapper接口和XML文件,执行查询。3.查询结果中应包含地址信息(字符串)4.将地址信息传递给前端前端实现(Vue)步骤:1.发送请求到后端,传递身份证号2.接收到地址信息3.使用地图API(例如百度地图)进行地理编码,将地址转换成经纬度4.创建地图实例,添加标记点由于问题聚焦于Java部分,我们详细写Java后端代码:假设我们有一个实体类`Resident`(住户):```javapublicclassResident{privateStringidCard;//身份证号privateStringname;//姓名privateStringaddress;//详细地址//其他字段...//gettersandsetters}```Mapper接口:```javapublicinterfaceResidentMapper{ResidentfindByIdCard(StringidCard);}```MapperXML(注意使用resultMap):```xml<resultMapid="ResidentMap"type="com.example.publicrentalhousing.domain.Resident"><idcolumn="id"property="id"/><!--假设表中有主键id--><resultcolumn="id_card"property="idCard"/><resultcolumn="name"property="name"/><resultcolumn="address"property="address"/><!--其他字段映射--></resultMap><selectid="findByIdCard"resultMap="ResidentMap">SELECT*FROMresidentWHEREid_card=#{idCard}</select>```服务层:```java@ServicepublicclassResidentService{@AutowiredprivateResidentMapperresidentMapper;publicResidentgetResidentByIdCard(StringidCard){returnresidentMapper.findByIdCard(idCard);}}```控制层:```java@RestController@RequestMapping("/resident")publicclassResidentController{@AutowiredprivateResidentServiceresidentService;@GetMapping("/info")publicResponseEntity<?>getResidentInfo(@RequestParam("idCard")StringidCard){Residentresident=residentService.getResidentByIdCard(idCard);if(resident!=null){returnResponseEntity.ok(resident);}else{returnResponseEntity.notFound().build();}}}```这样,前端可以通过访问`/resident/info?idCard=身份证号`来获取用户信息,其中包括地址字段。接下来,前端需要将地址传递给地图API。例如,使用百度地图API,你可以:1.在HTML中引入百度地图API2.在Vue组件中,当获取到用户信息后,使用地址字符串进行地理编码(Geocoding),然后在地图上标注。注意:为了安全,身份证号在传输过程中应加密(如HTTPS),并且前端显示时应注意隐私保护(通常部分隐藏)。但是,需求要求“在地图上显示”,所以前端代码需要集成地图服务。由于问题重点在Java,这里只简要说明前端部分:```vue<template><div><inputv-model="idCard"placeholder="请输入身份证号"><button@click="search">查询</button><divid="map-container"></div></div></template><script>exportdefault{data(){return{idCard:'',map:null}},mounted(){//初始化地图this.map=newBMap.Map("map-container");},methods:{search(){this.$axios.get(`/resident/info?idCard=${this.idCard}`).then(response=>{constresident=response.data;constaddress=resident.address;//使用百度地图地理编码constgeoc=newBMap.Geocoder();geoc.getPoint(address,point=>{if(point){this.map.centerAndZoom(point,15);this.map.clearOverlays();newBMap.Marker(point).addTo(this.map);}else{alert('无法解析地址');}});}).catch(error=>{console.error(error);});}}}</script>```注意:上述前端代码仅为示例,实际中需要申请百度地图API密钥,并处理错误情况。另外,地理编码服务可能会因为地址不准确而失败,因此建议在数据库中存储的地址尽量规范。如果用户信息中没有存储详细地址,那么就需要根据身份证号解析出区域(前6位),然后显示该区域的地图(比如显示该区域的行政区划图),但这不能满足显示“具体位置”的需求。因此,本方案采用存储详细地址的方式。总结步骤:1.Java后端接收身份证号,查询数据库获得详细地址(通过MyBatis的resultMap映射结果)2.返回包含地址的JSON数据3.前端使用地址调用地图API进行地理编码,然后显示地图和标记相关问题:§§相关问题§§1.如何保障用户身份证号传输和存储的安全性?2.如果地址解析失败,有什么备选方案?3.在Java后端查询时,身份证号是否需要脱敏处理?如果需要,如何实现?4.如何优化大量地址解析时的性能问题(如缓存机制)?</think> ### Java程序实现身份证号查询与地图显示基本信息 #### 解决方案概述 要在Java中实现通过身份证号在地图上显示基本信息的功能,需要整合以下技术模块: 1. **身份证号验证与解析** - 验证合法性并提取行政区划信息 2. **用户信息查询** - 从数据库获取详细信息 3. **地图集成** - 调用地图API显示位置 4. **前端展示** - Vue.js实现用户界面 具体实现步骤: --- ### 一、身份证号处理模块 ```java import java.util.regex.*; import java.text.SimpleDateFormat; public class IDCardUtil { // 验证身份证号合法性 public static boolean isValidIdCard(String idCard) { String regex = "^[1-9]\\d{5}(19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[0-9X]$"; return Pattern.matches(regex, idCard); } // 提取行政区划代码(前6位) public static String getAreaCode(String idCard) { return isValidIdCard(idCard)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值