mybatis 自定义转换器 ListTypeHandler

mybatis 自定义转换器 ListTypeHandler
实现存库 list->string

读库 string->list
package com.jeesite.modules.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.lang.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
<!--more-->
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * @author liuxin
 * @version 1.0
 * @date 2022/8/23 12:25
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes({List.class})
public abstract class ListTypeHandler<T> extends BaseTypeHandler<List<T>> {

  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, List<T> parameter, JdbcType jdbcType)
      throws SQLException {
    String content = ObjectUtils.isEmpty(parameter) ? null : JSON.toJSONString(parameter);
    ps.setString(i, content);
  }

  @Override
  public List<T> getNullableResult(ResultSet rs, String columnName) throws SQLException {
    return this.getListByJsonArrayString(rs.getString(columnName));
  }

  @Override
  public List<T> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    return this.getListByJsonArrayString(rs.getString(columnIndex));
  }

  @Override
  public List<T> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    return this.getListByJsonArrayString(cs.getString(columnIndex));
  }

  private List<T> getListByJsonArrayString(String content) {
    return StringUtils.isBlank(content)
        ? new ArrayList<>()
        : JSON.parseObject(content, this.specificType());
  }

  /**
   * 具体类型,由子类提供
   *
   * @return 具体类型
   */
  protected abstract TypeReference<List<T>> specificType();
}
 Java 泛型 如果在 ListTypeHandler 类中直接提供 TypeReference<List<T>> 这种类型,那就等效于TypeReference<List<Object>> 这种类型,后续 fastjson 在转换时无法确定具体的 Java 类型,转换后的类型最终就会是 List<JSONObject> ;同理,如果使用 Jackson 作为 JSON 转换工具,不确定具体类型时,最总会被转换为LinkedHashMap 类型,都需要再使用 TypeReference 来转换一次。

自定义TypeHandler的使用笔记 可通过自定义的TypeHandler实现某个属性在插入数据库以及查询时的自动转换。
package com.jeesite.modules.handler;

/**
 * @author liuxin
 * @version 1.0
 * @date 2022/8/23 12:33
 */

import com.alibaba.fastjson.TypeReference;
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsListGetResponse;

import java.util.List;

public class SkuListItemSpecDetailsTypeHandler
        extends ListTypeHandler<PddGoodsListGetResponse.GoodsListGetResponseGoodsListItemSkuListItemSpecDetailsItem> {
    @Override
    protected TypeReference<List<PddGoodsListGetResponse.GoodsListGetResponseGoodsListItemSkuListItemSpecDetailsItem>> specificType() {
        return new TypeReference<List<PddGoodsListGetResponse.GoodsListGetResponseGoodsListItemSkuListItemSpecDetailsItem>>() {
        };
    }
}

实体类上直接用注解:

 typeHandler = ListTypeHandler.class,
 javaType = PddGoodsListGetResponse.GoodsListGetResponseGoodsListItemSkuListItemSpecDetailsItem.class

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jeesite.modules.pdd.dao.PddGoodsSkuListDao">

    <resultMap type="com.jeesite.modules.pdd.entity.PddGoodsSkuList" id="PddGoodsSkuListMap">
        <!--column需要和数据库字段名对应,当实体上面设置了属性名则和属性名对应-->
        <result column="specDetails" property="specDetails"
                javaType="java.util.List" typeHandler="com.jeesite.modules.handler.SkuListItemSpecDetailsObjectTypeHandler"/>
    </resultMap>
    <select id="findList" resultMap="PddGoodsSkuListMap">
        SELECT ${sqlMap.column.toSql()}
        FROM ${sqlMap.table.toSql()}
        <where>
            ${sqlMap.where.toSql()}
        </where>
        ORDER BY ${sqlMap.order.toSql()}
    </select>
</mapper>

java内部类并实例化问题

@Data
public class MailOperationContent {
    private String theme;
    private List<String> toMail;
    private List<String> ccMail;
    private List<String> bccMail;
    private List<String> fromMail;
    private BTMailSecretlevelEnum secretLevel;
    private Long mailSize;
    private Integer attachmentNum;
    private List<MailAttachmentInfo> mailAttachmentInfos = new ArrayList<>();

    @Data
    public class MailAttachmentInfo {
        private String attachmentName;
        private String attachmentSecretLevel;
    }

但是我想实例化 MailAttachmentInfo 这个类
采用:

  mailAttachmentInfo = new MailOperationContent.MailAttachmentInfo();

但是编译报错

这个时候就会出现编译错误:"xxx.OuterClass" is not an enclosing class

解决办法:若要创建内部类的实例,需要有外部类的实例才行,或者是将内部类设置为静态的。
方法一:

   mailAttachmentInfo = new MailOperationContent().new MailAttachmentInfo();

方法二:将内部类设置为静态的

 @Data
    public static class MailAttachmentInfo {
        private String attachmentName;
        private String attachmentSecretLevel;
    }

java日常總結

对于数据库类型为varchar 类型 但是实体类为Long类 字段映射时会出现精度问题。可以使用 @JsonSerialize(using = ToStringSerializer.class) 把反射转为string字段

对于实体类为list类型 但是想要保存到数据库,并且查出来的数据为list可以使用 @TableField(typeHandler = AESEncryptHandler.class) 进行映射处理

selenium-java实现1688 cookies登录 和自动滑动验证码

pom.xml

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>3.141.59</version>
</dependency>

java代码

package com.jeesite.test;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

import java.io.*;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;

/**
 * @author liuxin
 * @version 1.0
 * @date 2022/6/9 11:05
 */
public class web1688 {

    public static void main(String[] args) {
        String osName = System.getProperties().getProperty("os.name");
        if (osName.equalsIgnoreCase("Linux")) {
            System.out.println("running in Linux");
            System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver_linux64/chromedriver");//chromederiver存放位置
        } else {
            System.out.println("don't running in Linux");
            System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver_win32/chromedriver.exe");//chromederiver存放位置
        }
        ChromeDriver webDriver = null;
        try {
            ChromeOptions chromeOptions = new ChromeOptions(); //设置 chrome 的无头模式
            // chromeOptions.setHeadless(Boolean.TRUE);
            chromeOptions.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
            chromeOptions.setExperimentalOption("useAutomationExtension", false);
            chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
            chromeOptions.addArguments("--no-sandbox");//禁用沙箱
            chromeOptions.addArguments("--disable-dev-shm-usage");//禁用开发者shm
            //   chromeOptions.addArguments("--headless"); //无头浏览器,这样不会打开浏览器窗口
            // 启动一个 chrome 实例
            webDriver = new ChromeDriver(chromeOptions); //访问网址
            //  login(webDriver, "15******", "l******");
            // writerCookie(webDriver);
            String url = "https://detail.1688.com/offer/**********.html";
            webDriver.get(url);
            setCookies(webDriver);
            System.out.println("登录cookies:" + webDriver.manage().getCookies());
            slider(webDriver);
            Thread.sleep(3000);
            String body = webDriver.findElement(By.cssSelector("body")).getText();
            System.out.println("body:" + body);
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //退出chrome   taskkill /F /im chromedriver.exe
            if (webDriver != null) {
                webDriver.close();
                webDriver.quit();
            }
        }
    }

     /**
     * 出现滑块实现自动滑动验证
     * @param webDriver
     * @throws InterruptedException
     */
    public static void slider(ChromeDriver webDriver) throws InterruptedException {
        // ((JavascriptExecutor) webDriver).executeScript(js);
        Thread.sleep(3000);
        // webDriver.switchTo().frame(0); //need to switch to this frame before clicking the slider
        WebElement slider = webDriver.findElement(By.xpath("//*[@id=\"nc_1_n1z\"]"));
        if (slider != null && slider.isDisplayed()) {
            Actions move = new Actions(webDriver);
            Action action = (Action) move.dragAndDropBy(slider, 258, 0).build();
            action.perform();
        }
    }


    /**
     * 生成cookies
     *
     * @param webDriver
     * @throws IOException
     */
    public static void writerCookie(ChromeDriver webDriver) throws IOException {
        //成功登陆后增加如下代码
        File cookieFile = new File("D:\\cookie.txt");
        cookieFile.delete();
        cookieFile.createNewFile();
        FileWriter fileWriter = new FileWriter(cookieFile);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        for (Cookie cookie : webDriver.manage().getCookies()) {
            bufferedWriter.write((cookie.getName() + ";" +
                    cookie.getValue()));
            bufferedWriter.newLine();
        }

//        for (Cookie cookie : webDriver.manage().getCookies()) {
//            bufferedWriter.write((cookie.getName() + ";" +
//                    cookie.getValue() + ";" +
//                    cookie.getDomain() + ";" +
//                    cookie.getPath() + ";" +
//                    cookie.getExpiry() + ";" +
//                    cookie.isSecure()));
//            bufferedWriter.newLine();
//        }
        bufferedWriter.flush();
        bufferedWriter.close();
        fileWriter.close();
    }

    /**
     * 设置cookies
     *
     * @param webDriver
     * @throws IOException
     */
    public static void setCookies(ChromeDriver webDriver) {
        webDriver.manage().deleteAllCookies();
        BufferedReader bufferedReader;
        try {
            File cookieFile = new File("D:\\cookie.txt");
            FileReader fileReader = new FileReader(cookieFile);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                StringTokenizer stringTokenizer = new StringTokenizer(line, ";");
                while (stringTokenizer.hasMoreTokens()) {
                    String name = stringTokenizer.nextToken();
                    String value = stringTokenizer.nextToken();
                    Cookie cookie = new Cookie(name, value);
                    webDriver.manage().addCookie(cookie);
                }
            }
            webDriver.navigate().refresh();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
     
     /**
     * 登录1688方法
     *
     * @param webDriver
     * @param username
     * @param password
     * @throws InterruptedException
     */
    public static void login(ChromeDriver webDriver, String username, String password) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        String baseUrl = "https://login.1688.com/";
        webDriver.get(baseUrl + "/member/signin.htm?spm=0.0.0.0.ijAHe8&Done=https%3A%2F%2Fs.1688.com%2Fcompany%2Fcompany_search.htm%3Fkeywords%3D%25B7%25FE%25D7%25B0%26button_click%3Dtop%26n%3Dy%26sortType%3Dpop%26pageSize%3D30%26offset%3D3%26beginPage%3D1");

        //  webDriver.switchTo().frame(0);
        webDriver.findElement(By.cssSelector(".password-login-tab-item")).click();
        //休息5秒
        Thread.sleep(5000);
        webDriver.findElement(By.id("fm-login-id")).clear();
        webDriver.findElement(By.id("fm-login-id")).sendKeys(username);
        webDriver.findElement(By.id("fm-login-password")).clear();
        webDriver.findElement(By.id("fm-login-password")).sendKeys(password);
        webDriver.findElement(By.cssSelector(".password-login")).click();
        //休息5秒
        Thread.sleep(5000);
    }
}

使用教程

首先需要下载 webDriver驱动 和你的谷歌浏览器匹配
先放开 用你的1688账户登录一次获取到登录cookies

 login(webDriver, "18******", "l******");
 writerCookie(webDriver);

然后
注释掉上面的方法,就可以实现自动登录和出现滑块自动验证的功能