首页 > 代码库 > mongoDb 2

mongoDb 2

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>selenium</groupId>    <artifactId>selenium</artifactId>    <packaging>war</packaging>    <version>0.0.1-SNAPSHOT</version>    <name>sunfan Maven Webapp</name>    <properties>        <spring-version>4.0.5.RELEASE</spring-version>        <spring-integration-version>4.0.0.RELEASE</spring-integration-version>        <spring.mongo.version>1.3.3.RELEASE</spring.mongo.version>        <javamail.version>1.4.5</javamail.version>    </properties>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-classic</artifactId>            <version>1.1.2</version>        </dependency>        <!-- Spring Framework - Core -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring-version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring-version}</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring-version}</version>        </dependency>        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjweaver</artifactId>            <version>1.8.4</version>        </dependency>        <!-- pnr -->        <dependency>            <groupId>com.travelsky</groupId>            <artifactId>ibeclient</artifactId>            <version>1.1</version>        </dependency>        <dependency>            <groupId>org.apache</groupId>            <artifactId>log4j</artifactId>            <version>1.2.15</version>        </dependency>        <dependency>            <groupId>com.travelsky</groupId>            <artifactId>unibizibe</artifactId>            <version>0.0.1</version>        </dependency>        <dependency>            <groupId>com.travelsky</groupId>            <artifactId>uniframework</artifactId>            <version>0.0.1</version>        </dependency>        <!-- pnr -->        <!-- Spring Framework - Integration -->        <dependency>            <groupId>org.springframework.integration</groupId>            <artifactId>spring-integration-core</artifactId>            <version>${spring-integration-version}</version>        </dependency>        <dependency>            <groupId>org.springframework.integration</groupId>            <artifactId>spring-integration-ws</artifactId>            <version>${spring-integration-version}</version>        </dependency>        <dependency>            <groupId>org.springframework.integration</groupId>            <artifactId>spring-integration-mongodb</artifactId>            <version>${spring-integration-version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>4.0.6.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-mongodb</artifactId>            <version>1.5.4.RELEASE</version>        </dependency>        <dependency>            <groupId>org.oracle</groupId>            <artifactId>ojdbc6</artifactId>            <version>6</version>        </dependency>        <!-- jdbc driver -->        <dependency>            <groupId>javax</groupId>            <artifactId>javaee-api</artifactId>            <version>7.0</version>        </dependency>        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-core-asl</artifactId>            <version>1.9.13</version>        </dependency>        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.12</version>        </dependency>    <dependency>        <groupId>org.seleniumhq.selenium</groupId>        <artifactId>selenium-java</artifactId>        <version>2.44.0</version>    </dependency>        <dependency>            <groupId>javax.mail</groupId>            <artifactId>mail</artifactId>            <version>${javamail.version}</version>        </dependency>        <dependency>            <groupId>commons-lang</groupId>            <artifactId>commons-lang</artifactId>            <version>2.5</version>        </dependency>    </dependencies>    <build>        <finalName>selenium</finalName>        <plugins>            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.1</version>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>            <plugin>                <groupId>org.mortbay.jetty</groupId>                <artifactId>jetty-maven-plugin</artifactId>                <version>8.1.10.v20130312</version>                <configuration>                    <webAppConfig>                        <contextPath>/${project.artifactId}</contextPath>                    </webAppConfig>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-war-plugin</artifactId>                <version>2.4</version>                <configuration>                    <failOnMissingWebXml>false</failOnMissingWebXml>                </configuration>            </plugin>        </plugins>    </build></project>

 

 

 MongoTemplateConfig 

package com.travelsky.autotest.config.spring;import java.net.UnknownHostException;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.authentication.UserCredentials;import org.springframework.data.mongodb.core.MongoTemplate;import com.mongodb.Mongo;import com.mongodb.MongoClient;import com.travelsky.autotest.dao.datasource.builder.mongo.AutoTest_DefaultDB;import com.travelsky.autotest.dao.entity.DataSource;@Configurationpublic class MongoTemplateConfig {        @Bean public MongoTemplate autoTestClient() {        DataSource ds = new AutoTest_DefaultDB().build(new DataSource());        Mongo mongo = null;        try {            mongo = new MongoClient(ds.getIp(),ds.getPort());        } catch (UnknownHostException e) {            throw new RuntimeException(e);        }        UserCredentials userCredentials = new UserCredentials(ds.getUsername(), ds.getPassword());        return new MongoTemplate(mongo, ds.getDatabaseName(), userCredentials);    }    }

 

MongoDao

package com.travelsky.autotest.dao.table;import java.net.UnknownHostException;import java.util.List;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;public abstract class MongoDao<E> {        abstract MongoTemplate getMongoTemplate() throws UnknownHostException;            /**     * 插入测试用例     *      * @param o obj     * @throws UnknownHostException     */    public void insert(Object o) throws UnknownHostException {        getMongoTemplate().insert(o, getCollections());    }    /**     * 读取测试数据     *      * @throws UnknownHostException      */    @SuppressWarnings("unchecked")    public List<E> queryAll() throws UnknownHostException {        return getMongoTemplate().findAll(getClazz());    }            /**     * 读取数据按criteria规则     * @param criteria 规则     * @return     * @throws UnknownHostException     */    @SuppressWarnings("unchecked")    public List<E> query(Criteria criteria) throws UnknownHostException {        return getMongoTemplate().find(new Query(criteria), getClazz());    }        /**     * 条件查询,相当于 where key = value     * @param key     * @param value     * @return     * @throws UnknownHostException     */    public List<E> query(String key,String value) throws UnknownHostException {        return query(Criteria.where(key).is(value));    }        /**     * 注意这个方法只会覆盖所有匹配到的数据     * @param criteria 查询条件     * @param updateKey 定位更新的键     * @param updateValue 更新的内容     * @throws UnknownHostException     */    public void queryAndUpdate(Criteria criteria,String updateKey, Object updateValue) throws UnknownHostException {         getMongoTemplate().updateMulti(new Query(criteria), Update.update(updateKey, updateValue),  getClazz());    }        /**     * 注意这个方法只会覆盖所有匹配到的数据     * @param key 定位查询的主键     * @param value 定位查询的内容     * @param updateKey 定位更新的KEY     * @param updateValue 更新的内容     * @throws UnknownHostException     */    public void queryAndUpdate(String key,String value,String updateKey, Object updateValue) throws UnknownHostException {         getMongoTemplate().updateMulti(new Query(Criteria.where(key).is(value)), Update.update(updateKey, updateValue),  getClazz());    }        /**     * 删除匹配到的数据       * @param key 定位删除的键     * @param value 定位查询的内容     * @throws UnknownHostException     */    public void removeAll(String key,String value) throws UnknownHostException {         getMongoTemplate().remove(new Query(Criteria.where(key).is(value)),  getClazz());    }        /**     * 定位删除的内容     * @param criteria 定位删除的内容     * @throws UnknownHostException     */    public void removeAll(Criteria criteria) throws UnknownHostException {         getMongoTemplate().remove(new Query(criteria),  getClazz());    }    abstract String getCollections();    @SuppressWarnings("rawtypes")    abstract Class getClazz();    }

TestCaseDao

package com.travelsky.autotest.dao.table;import java.net.UnknownHostException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.stereotype.Component;import com.travelsky.autotest.dao.entity.TestCase;@Componentpublic class TestCaseDao<E> extends MongoDao<E>{    @Autowired @Qualifier("autoTestClient")MongoTemplate autoTestClient;    @Override    String getCollections() {        return "testCase";    }    @SuppressWarnings("rawtypes")    @Override    Class getClazz() {        return TestCase.class;    }    @Override    MongoTemplate getMongoTemplate() throws UnknownHostException {        return autoTestClient;    }    }

 

AutoTest_DefaultDB

package com.travelsky.autotest.dao.datasource.builder.mongo;import com.travelsky.autotest.dao.config.DataBaseConfig;import com.travelsky.autotest.dao.datasource.builder.Builder;import com.travelsky.autotest.dao.entity.DataSource;public class AutoTest_DefaultDB implements Builder{        @Override    public DataSource build(DataSource dataSource) {        dataSource.setIp(DataBaseConfig.MONGO_DB_IP);        dataSource.setPort(DataBaseConfig.MONGO_DB_PORT);        dataSource.setDatabaseName(DataBaseConfig.MONGO_DB_DATABASENAME);        dataSource.setUsername(DataBaseConfig.MONGO_DB_USERNAME);        dataSource.setPassword(DataBaseConfig.MONGO_DB_PASSWORD);        return dataSource;    }    }

 

mongoDb 2