public class DocumentUtil {

private static final Logger log = LoggerFactory.getLogger(DocumentUtil.class);

/**
 * 对象转换成document
 * @param obj 对象
 * @param clazz 对象所属类
 * @return document
 */
public static Document buildDocument(Object obj, Class clazz) {
    Document document = new Document();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        Object value = null;
        try {
            if (field.getType().isAssignableFrom(List.class) ||
                    field.getType().isAssignableFrom(Collection.class) ||
                    field.getType().isAssignableFrom(Map.class)) {
                value = JSON.toJSONString(field.get(obj));
            } else {
                value = field.get(obj);
            }
        } catch (IllegalAccessException e) {
            log.error(e);
        }

        if (value != null) {
            document.append(field.getName(), value);
        }
    }
    return document;
}

/**
 * document转换对象
 * @param document document
 * @param clazz 转换对象类
 * @param <T> 泛型对象
 * @return 对象
 */
public static <T> T convertToObj(Document document, Class<T> clazz) {
    Field[] fields = clazz.getDeclaredFields();
    T t = null;
    try {
        t = clazz.newInstance();
    } catch (Exception e) {
        log.error(e);
    }
    for (Field field : fields) {
        try {
            if (field.getType().isAssignableFrom(Integer.class)) {
                field.setAccessible(true);
                field.setInt(t, document.getInteger(field.getName()));
            } else if (field.getType().isAssignableFrom(Float.class)) {
                field.setAccessible(true);
                field.setFloat(t, document.getDouble(field.getName()).floatValue());
            } else if (field.getType().isAssignableFrom(Double.class)) {
                field.setAccessible(true);
                field.setDouble(t, document.getDouble(field.getName()));
            } else if (field.getType().isAssignableFrom(Long.class)) {
                field.setAccessible(true);
                field.setLong(t, document.getLong(field.getName()));
            } else if (field.getType().isAssignableFrom(String.class)) {
                field.setAccessible(true);
                field.set(t, document.getString(field.getName()));
            } else if (field.getType().isAssignableFrom(List.class) ||
                    field.getType().isAssignableFrom(Collection.class)) {
                field.setAccessible(true);
                field.set(t, JSON.parseArray(document.getString(field.getName())));
            } else if (field.getType().isAssignableFrom(Map.class)) {
                field.setAccessible(true);
                field.set(t, JSON.parseObject(document.getString(field.getName())));
            } else {
                field.setAccessible(true);
                field.set(t, JSON.parseObject(document.getString(field.getName()), field.getType()));
            }
        } catch (Exception e) {
            log.error(e);
        }
    }

    return t;
}

}

@Component
public class MongodbCustomClient {
    private String mongoAddress = SystemConfig.getProperty("mongo.address", "192.168.202.91:27017");

    private int mongoConnectionsPerHost = 100;

    private int mongoThreadsAllowedToBlockForConnectionMultiplier = 5;

    private int mongoConnectionTimeout = 60 * 1000;

    private int mongoMaxWaitTime = 120 * 1000;

    private boolean mongoSocketKeepAlive = true;

    private int mongoSocketTimeout = 0;

    private String auth = SystemConfig.getProperty("mongo.auth", "");

    private String dbName = SystemConfig.getProperty("mongo.dbname", "featureLib");

    private MongoClient mongoClient;


    @PostConstruct
    public void init() {
        MongoClientOptions options = MongoClientOptions.builder()
                .threadsAllowedToBlockForConnectionMultiplier(mongoThreadsAllowedToBlockForConnectionMultiplier)
                .connectTimeout(mongoConnectionTimeout)
                .connectionsPerHost(mongoConnectionsPerHost)
                .maxWaitTime(mongoMaxWaitTime)
                .socketKeepAlive(mongoSocketKeepAlive)
                .writeConcern(WriteConcern.SAFE)
                .socketTimeout(mongoSocketTimeout).build();

        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        String[] auths = auth.split(",");
        for (String a : auths) {
            if (a.equals("")) continue;
            String[] up = a.split(":");
            credentials.add(MongoCredential.createCredential(up[0], dbName, up[1].toCharArray()));
        }

        String[] servers = mongoAddress.split(",");
        List<ServerAddress> addresses = new ArrayList<ServerAddress>();
        for (String server : servers) {
            String[] hp = server.split(":");
            addresses.add(new ServerAddress(hp[0], Integer.valueOf(hp[1])));
        }

        if (credentials.size() > 0) {
            mongoClient = new MongoClient(addresses, credentials, options);
        } else {
            mongoClient = new MongoClient(addresses, options);
        }
    }

    public MongoDatabase getDataBase() {
        return mongoClient.getDatabase(dbName);
    }

    public MongoCollection<Document> getCollection(String collection) {
        return getDataBase().getCollection(collection);
    }

}

首先恭喜自己拿到深圳京东的实习offer,是一个好的开端,但也有了更高的目标。从寒假到现在,经历了分手和不计其数的refuse,腾讯、阿里、携程等等等等,暴瘦15斤。还好,最后有幸被jd看中,然后递交了入职申请,准备5.11号的入职,我的春招也就告一段落了。下面还是分享总结一下这次在春招大战中学到感受到的一些东西。

阅读全文 »

环境jdk8 + npm5.6 + elasticsearch-6.2.3 + elastichead
搭建过程这里不赘述了
master节点下的配置文件:

http.cors.enabled: true
http.cors.allow-origin: "*"

cluster.name: hoaresky
node.name: master
node.master: true

network.host: 127.0.0.1

slave的配置文件:

cluster.name: hoaresky
node.name: slave1

network.host: 127.0.0.1
http.port: 8200

discovery.zen.ping.unicast.hosts: ['127.0.0.1']

elasticsearch-head插件的配置文件是根目录下的:Gruntfile.js



阅读全文 »

一、单个参数:

public List<XXBean> getXXBeanList(String xxCode);  

<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">

  select t.* from tableName t where t.id= #{id}  

</select>  

其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字,

select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。

二、多参数:

public List<XXXBean> getXXXBeanList(String xxId, String xxCode);  

<select id="getXXXBeanList" resultType="XXBean">

  select t.* from tableName where id = #{0} and name = #{1}  

</select>  

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
复制代码

三、Map封装多参数:

public List<XXXBean> getXXXBeanList(HashMap map);  

<select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">

  select 字段... from XXX where id=#{xxId} code = #{xxCode}  

</select>  

其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。

四、List封装in:

public List<XXXBean> getXXXBeanList(List<String> list);  

<select id="getXXXBeanList" resultType="XXBean">
  select 字段... from XXX where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
    #{item}  
  </foreach>  
</select>  

foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')

五、多参数传递之注解方式示:

例子:

public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId);

xml配置这样写:

<select id="getAddrInfo"  resultMap="com.xxx.xxx.AddrInfo">
       SELECT * FROM addr__info 
    where addr_id=#{addrId} and corp_id=#{corpId}
</select>

以前在mybatis select语句中要带parameterType的,现在可以不要这样写。

六、selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法:

将参数放入Map,再取出Map中的List遍历。如下:

List<String> list_3 = new ArrayList<String>();
Map<String, Object> map2 = new HashMap<String, Object>();

list.add("1");
list.add("2");
map2.put("list", list); //网址id

map2.put("siteTag", "0");//网址类型

 

public List<SysWeb> getSysInfo(Map<String, Object> map2) {
  return getSqlSession().selectList("sysweb.getSysInfo", map2);
}
 


<select id="getSysInfo" parameterType="java.util.Map" resultType="SysWeb">
  select t.sysSiteId, t.siteName, t1.mzNum as siteTagNum, t1.mzName as siteTag, t.url, t.iconPath
   from TD_WEB_SYSSITE t
   left join TD_MZ_MZDY t1 on t1.mzNum = t.siteTag and t1.mzType = 10
   WHERE t.siteTag = #{siteTag } 
   and t.sysSiteId not in 
   <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
       #{item}
   </foreach>
 </select>