0%

在MongoDB的客户端可以使用如下命令:
db.copyDatabase(“abc”, “abc-tmp”, “localhost”)
数据库文件较大的话会需要一段时间。

1,复制数据库,使用copyDatabase命令完成复制数据库,
格式:copyDatabase(fromdb,todb,fromhost[,username,password])
fromdb:源数据库名称
todb:目标数据库名称
fromhost:源数据库地址,本地和远程都可以
username:远程数据库用户名
password:远程数据密码
例子:将本地db2库复制本地,并重命名db1
> db.copyDatabase(“db2”,”db1”,”localhost”)

阅读全文 »

在MongoDB数据库的Java程序开发过程中,使用了官方支持的POJO映射Morphia
https://github.com/mongodb/morphia/wiki

其作用,简而言之,就是完成Java Bean对象与MongoDB数据库中一条记录的映射,大家在Entity中看到的Profile就是这样一个POJO,
Profile中嵌套了一些子类,比较容易理解,Morphia的所有映射都可以通过注解来完成,非常简单易用,可以查看上面wiki文档中的说明。
另外,Morphia提供了一个基本的DAO类,org.mongodb.morphia.dao.BasicDAO<T,K>
详细信息可以查看链接https://rawgit.com/wiki/mongodb/morphia/javadoc/0.108/apidocs/index.html

该类支持基本的CRUD操作,查询也支持各种查询条件,不过在返回结果这边可定制性较弱,通常都是返回整个的POJO类的List,属于粗粒度吧。
BasicDAO从根本上都是对mongo-java-driver中一些api的封装,如果需要更全面的功能的话可以选用底层driver,当然这样就是直接跟MongoDB中的BSON打交道了,
也就用不上POJO了。

Java中有不少处理JSON格式数据的类库,在JSON的官网 http://www.json.org/ 下面对应的各个语言编解码库,Java就有20多种可以使用的类库。

当然,其中最常使用的是json-lib,也是资格比较老的一个库,网上介绍也很多,http://json-lib.sourceforge.net/,但是已经停止更新很久了,
最新的版本还是基于JDK1.5的,最后更新日期是2010年的12月。在Struts2中返回JSON格式的数据时,默认使用的就是json-lib。
但是总体上来说,现在开发程序还是不建议使用json-lib,毕竟有点太古老了。
推荐使用的是号称性能最快的JSON处理器Jackson,社区非常活跃,也一直在更新,功能也很强大,https://github.com/FasterXML/jackson

可以发现在许多其他Java开源项目中对JSON的处理都在使用Jackson,例如Spring。

有人专门对比了这两个类库的性能,
http://wangym.iteye.com/blog/738933

阅读全文 »

最近在进行一个项目,需要设计一个Server,为移动端的client提供REST风格的服务,RESTful API是现在互联网应用程序中使用比较多、也比较成熟的一种设计理念,以HTTP协议为基础,在项目之前,只是简单听说过REST这种理念,并没有很深入的接触,在api的设计过程中,需要遵循一些原则,但是要注意的是RESTful只是一种风格,并不是标准、协议或者强制性的要求。因此,在设计过程中,我也参考了一些文章和资料,学习如何去设计一套合理规范的API。

在初步的设计中,根据之前的工作经验,采用了比较传统的Spring和Hibernate,根据应用的需求,也采用了许多相关技术,例如Spring-data-jpa和Hibernate Validator等,后面会根据一些具体内容进行总结。

下面地址是一个java轻量级restful框架,里面有不少关于RESTful的介绍和文章链接,推荐阅读。

https://github.com/Dreampie/resty

http://geode.incubator.apache.org/

前段时间听说了分布式内存数据库Apache geode开源的消息,本来对该数据库还不甚了解,但是发现它和12306有着不少关系,不仅被采用而且经受住了重重考验,所以觉得还是有必要关注一下,先留下一篇文章吧,以后来完善。

mybatis中如果传入类型为Long,与其他一些基本类型例如int,String的处理是不一样的,参数需要统一使用#{_parameter},而不论你传入参数的名称是什么。

例如:

<select id="getUser" parameterType="java.lang.Long" resultType="com.test.User">
    SELECT * from user
    <if test = "accountId!=null">
        WHERE accountId = #{_parameter}
    </if>  
</select>

 

In the HyperText Transfer Protocol (HTTP), idempotence and safety are the major attributes that separate HTTP verbs.

Of the major HTTP verbs, GET, PUT, and DELETE are idempotent (if implemented according to the standard), but POST is not.[9] These verbs represent very abstract operations in computer science: GET retrieves a resource; PUT stores content at a resource; and DELETE eliminates a resource. As in the example above, reading data usually has no side effects, so it is idempotent (in fact nullipotent). Storing a given set of content is usually idempotent, as the final value stored remains the same after each execution. And deleting something is generally idempotent, as the end result is always the absence of the thing deleted.

参考

PUT vs POST in REST

Idempotence wiki

我的psn奖杯卡

我的psn id是lmshlms,港服,欢迎大家加好友。

初用Gradle,也学习了不少东西,相比于之前的maven(虽然用的不多),但还是感受到了Gradle的许多特点和强大之处。除了常规的添加一些依赖,也尝试着自己写一些脚本。下面的跟tomcat相关的用例,主要用于部署测试环境,也借鉴了他人的一些思路,分享出来。

// deploy to local tomcat server

// All extra properties must be defined through the "ext" namespace.
ext.tomcatHome = System.getenv()["CATALINA_HOME"]
ext.tomcatBin = tomcatHome + '/bin'
ext.tomcatStart = tomcatBin + '/startup'
ext.tomcatStop = tomcatBin + '/shutdown'
ext.tomcatWebapps = tomcatHome + '/webapps'

ant.condition(property: "os", value: "windows") { os(family: "windows") }
ant.condition(property: "os", value: "unix"   ) { os(family: "unix")    }

task checkTomcat << {
    if (tomcatHome == null)
        throw new RuntimeException("Could not get TOMCAT home, please set CATALINA_HOME env virable first!")
    switch(ant.properties.os){
        case 'windows':
            println 'Running on windows.'
            tomcatStart += '.bat'
            tomcatStop += '.bat'
            break
        case 'unix':
            println 'Running on unix.'
            tomcatStart += '.sh'
            tomcatStop += '.sh'
            break
    }
    println "Using CATALINA_HOME: ${tomcatHome}"
    println "Using Tomcat start cmd: ${tomcatStart}"
    println "Using Tomcat stop cmd: ${tomcatStop}"
}

task deployLocal << {
    println "copy war from ${buildDir}/libs into ${tomcatWebapps}"
    copy{
        from "${buildDir}/libs"
        into "${tomcatWebapps}"
        include '*.war'
    }
    //println "start tomcat !"
    //startTomcat.execute()
}

deployLocal.dependsOn checkTomcat

task startTomcat << {
    exec {
        executable tomcatStart
    }
    println 'Start Tomcat server.'
    //store the output instead of printing to the console:
    standardOutput = new ByteArrayOutputStream()

    //extension method stopTomcat.output() can be used to obtain the output:
    ext.output = {
        return standardOutput.toString()
    }
    println 'Done.'
}

startTomcat.dependsOn checkTomcat

task stopTomcat << {
    exec {
        executable tomcatStop
    }
    println 'Shutting down Tomcat server.'

    //store the output instead of printing to the console:
    standardOutput = new ByteArrayOutputStream()
    //extension method stopTomcat.output() can be used to obtain the output:
    ext.output = {
        return standardOutput.toString()
    }
    println 'Done.'
}

stopTomcat.dependsOn checkTomcat