Type handler für ArrayList in myBatis

Ich bin Versucht zu schreiben-type-handler für ArrayList, aber das ist mir Fehler kann einer mir helfen.

Will ich speichern ArrayList als VARCHAR in der DB abrufen und es als ArrayList.

package com.someweb.typehandlers;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
//@MappedTypes(java.util.ArrayList.class)

//@MappedJdbcTypes(JdbcType.VARCHAR)
public class StringArrayListTypeHandler extends BaseTypeHandler<ArrayList<String>> 
{
@Override
public void setNonNullParameter(PreparedStatement ps, int i, ArrayList<String> parameter, JdbcType jdbcType)
        throws SQLException {
    //TODO Auto-generated method stub
    StringBuilder str=new StringBuilder(parameter.toString());
    ps.setString(i,str.substring(1,str.length()-1));

}

@Override
public ArrayList<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
    //TODO Auto-generated method stub

    String str=rs.getString(columnName);

    ArrayList<String> roles=new ArrayList<String>();
    String[] rolesarray=str.split(",");
    for(String s:rolesarray)
    roles.add(s);

    return roles;
}

@Override
public ArrayList<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    //TODO Auto-generated method stub
    String str=rs.getString(columnIndex);

    ArrayList<String> roles=new ArrayList<String>();
    String[] rolesarray=str.split(",");
    for(String s:rolesarray)
    roles.add(s);

    return roles;   }

@Override
public ArrayList<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    //TODO Auto-generated method stub
    String str=cs.getString(columnIndex);

    ArrayList<String> roles=new ArrayList<String>();
    String[] rolesarray=str.split(",");
    for(String s:rolesarray)
    roles.add(s);

    return roles;   }

}

Ich bin neu in myBatis . So jemand mir helfen, ich bin nicht in der Lage, um herauszufinden, wie dies erreicht werden kann

Mein dto-Klasse sieht wie folgt aus

package com.someweb.dto;

import java.security.Principal;
import java.sql.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class UserDTO implements Principal {

private int id;
private String username;

private String name;
private String password;
private String token;
private String email;
private boolean isAuthenticated;
private boolean is_active;

private List<String> role;
private String phone;
public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public boolean isAuthenticated() {
    return isAuthenticated;
}

public void setAuthenticated(boolean isAuthenticated) {
    this.isAuthenticated = isAuthenticated;
}

public List<String> getRole() {
    return role;
}



public void setRole(List<String> role) {
    this.role = role;
}
public void setRole(String role) {
     this.role.add(role);
}
 public void addRole(String role)
 {
     if(role==null) this.role=new ArrayList<String>();
     this.role.add(role);

 }

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public boolean isIs_active() {
    return is_active;
}

public void setIs_active(boolean is_active) {
    this.is_active = is_active;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}
}

meine mapper-Datei der code ist, wie dies

    <resultMap id="userResultMap" type="com.someweb.dto.UserDTO">
  <id property="id" column="id" />
  <result property="username" column="username"/>
  <result property="password" column="password"/>
  <result property="email" column="email"/>
 <result property="phone" column="phone"/>
 <result property="is_active" column="is_active"/>
  <collection property="role" ofType="java.lang.String" >
        <result column="role" />
    </collection>

</resultMap>
<insert id="insertUser" useGeneratedKeys="true"
    keyProperty="id">
  insert into tblusers(username,password,email,phone,role,is_active) 
  values(#{username},#{password},#{email},#{phone},#{role,typeHandler=com.someweb.typehandlers.StringArrayListTypeHandler},#{is_active})
</insert>

nun die Einfügung ausführt . Aber beim abrufen der Daten DTO das gesamte Feld Rolle ist geladen wurden, als einzelnen string in ArrayList .

Und wenn Sie den typeHandler Attribut, erhalte ich die Fehler

Cause: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use.
hinzufügen ausnahmeereignis in deiner Frage
Achtung: wie ich bereits in deiner anderen Frage, wenn Sie kommentieren Sie die Art handler @MappedJdbcTypes(JdbcType.VARCHAR) es wird standardmäßig verwendet, um die anzeigen-Spalte vom Typ Varchar, wird für jede Spalte einer Abfrage mit der app, das ist unwahrscheinlich, was Sie wollen.
Würde ich ersetzen ArrayList mit List (best-practice-sowieso). Versuchen Sie, entfernen Sie Kommentare und Referenz in typeHandler Abschnitt über Mybatis config, anstatt, wie ich bemerkt habe, seltsame Verhaltensweisen, die mit diesen Anmerkungen.
Es ist nicht klar, was Sie Fragen. Sie müssen mindestens die Angabe genauer, welche Fehlermeldung Sie bekommen und dem Beispiel-code, verwendet oder verwenden soll, die Prozedur.

InformationsquelleAutor harsha kumar Reddy | 2017-03-01

Schreibe einen Kommentar