Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeHandler是否支持自定义的select查询呢? 测试下来只有tk自带的select比如 selectAll能够利用typeHandler机制 #903

Open
newton1919 opened this issue Feb 2, 2024 · 1 comment

Comments

@newton1919
Copy link

newton1919 commented Feb 2, 2024

<select id="selectList" resultType="com.yxy.vo.PersonVO"
          parameterType="com.yxy.dto.PersonListDto">
    SELECT
    t1.*,
    t2.name as tenantName
    FROM
    person t1, tenant_extend t2
    WHERE
    t1.status = 0
    and t1.tenant_id = t2.id
    <if test="search != null and search != ''">
      and t1.name like concat('%',#{search},'%')
    </if>
    and t1.tenant_id in
    <foreach item="item" index="index" collection="scopedTenantIds"
             open="(" separator=" , " close=")">
      '${item}'
    </foreach>
  </select>

以上是一个自定义的select查询,返回结果是一个自定义的 PersonVO类,PersonVO类定义如下:

@Data
public class PersonVO extends Person {
  private String tenantName;//所属租户名称
}

继承自model类Person,定义如下:

package com.yxy.model;


import com.yxy.config.SensitiveInformationTypeHandler;
import lombok.Data;
import org.apache.ibatis.type.JdbcType;
import tk.mybatis.mapper.annotation.ColumnType;

import java.util.Date;
import java.math.BigDecimal;

import javax.persistence.*;


@Data
@Entity
@Table(name = "person")
public class Person {

  //主键
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  private Long id;

  //租客id
  @Column(name = "tenant_id")
  private Long tenantId;

  //姓名
  @Column(name = "name")
  private String name;

  //性别
  @Column(name = "gender")
  private String gender;

  //手机号
  @Column(name = "mobile")
  @ColumnType(typeHandler = SensitiveInformationTypeHandler.class, jdbcType = JdbcType.VARCHAR)
  private String mobile;

  //身份证号
  @Column(name = "id_card_no")
  @ColumnType(typeHandler = SensitiveInformationTypeHandler.class, jdbcType = JdbcType.VARCHAR)
  private String idCardNo;

  //图片路径
  @Column(name = "img_url")
  private String imgUrl;

  //是否领导,0不是  1是
  @Column(name = "is_leader")
  private Integer isLeader;

  //年龄
  @Column(name = "age")
  private Integer age;

  //0 有效  1 逻辑删除
  @Column(name = "status")
  private Long status;

  //描述(保留字段)
  @Column(name = "misc")
  private String misc;

  //创建者id
  @Column(name = "create_user_id")
  private Long createUserId;

  //创建人姓名
  @Column(name = "create_user_name")
  private String createUserName;

  //创建时间
  @Column(name = "create_date")
  private Date createDate;

  //修改人id
  @Column(name = "update_user_id")
  private Long updateUserId;

  //修改人姓名
  @Column(name = "update_user_name")
  private String updateUserName;

  //更新时间
  @Column(name = "update_date")
  private Date updateDate;


}

大家可以看到, 手机号和身份证号字段 使用了typeHandler做特殊处理,即保存时加密,查询时解密, 目前发现调用tkmybatis自带的selectAll()方法可以查询出解密后的手机号和身份证号,但是使用 自定义的selectList方法却只查询到加密的手机号,并没有自动解密,
我该怎么办?麻烦提点下,谢谢

@abel533
Copy link
Owner

abel533 commented Feb 4, 2024

自己写的resultType="com.yxy.vo.PersonVO"也用resultMap,在里面指定typehandler。。

如果能定义 Mobile 和 IdCard 对象,还可以针对这俩对象写typehandler,配置为全局处理也能避免每个地方都手动配置。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants