为了账号安全,请及时绑定邮箱和手机立即绑定

史上最全,最详idea搭建springdata+mongoDB+maven+springmvc

标签:
Java MongoDB

作为IT届的小弟,本篇作为本人的第一篇手记,还希望各位大牛多多指点,以下均为个人学习所得,如有错误,敬请指正。本着服务IT小白的原则,该手记比较详细。由于最近使用postgre开发大型项目,发现了关系型数据库的弊端及查询效率之慢,苦心钻研之下,对nosql的mongoDB从无知到有了初步了解。


项目环境:win10+IntelliJ IDEA2016+maven3.3.9+MongoDB 3.2+JDK1.7+spring4.1.7


推荐网站(适合学习各种知识的基础):http://www.runoob.com/
mongo安装请参考http://www.runoob.com/mongodb/mongodb-window-install.html


由于最近osChina的maven仓库挂掉,推荐大家使用阿里的镜像,速度快的飞起
maven配置:<localRepository>F:\.m2\repository</localRepository>

<mirrors>
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
  </mirrors>

这里不实用idea自带maven插件,改用3.3.9
图片描述


项目结构:图片描述
这里dao与mongoDao分别为mongoDB的两种查询方式:
dao为JPA的查询方式(请参考springdataJPA)
mongoDao使用mongoTemplate,类似于关系型数据库使用的jdbcTemplate


不罗嗦,上代码
先看配置文件
spring-context.xm为最基本的spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描service包嗲所有使用注解的类型-->
    <context:component-scan base-package="com.lida.mongo"/>

    <!-- 导入mongodb的配置文件 -->
    <import resource="spring-mongo.xml" />
    <!-- 开启注解 -->
    <context:annotation-config />
</beans>

spring-web.xml为springmvc的基本配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!--配置springmvc-->
    <!--1.开启springmvc注解模式-->
    <!--简化配置:
        (1)主动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
        (2)提供一系列功能:数据绑定,数字和日期的format @NumberFormt @DataTimeFormat,xml json默认的读写支持-->
    <mvc:annotation-driven/>
    <!--servlet-mapping-->
    <!--2静态资源默认的servlet配置,(1)允许对静态资源的处理:js,gif    (2)允许使用“/”做整体映射-->
    <!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL-->
    <mvc:default-servlet-handler/>
    <!--3:配置jsp 显示viewResolver-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 4自动扫描且只扫描@Controller -->
    <context:component-scan base-package="com.lida.mongo.controller" />

    <!-- 定义无需Controller的url<->view直接映射 -->
    <mvc:view-controller path="/" view-name="redirect:/goMongo/list"/>

</beans>

spring-mongo.xml为mongo配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/data/mongo
            http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

    <!-- 加载mongodb的属性配置文件 -->
    <context:property-placeholder location="classpath:mongo.properties" />
    <!-- spring连接mongodb数据库的配置 -->
    <mongo:mongo-client replica-set="${mongo.hostport}" id="mongo">
        <mongo:client-options connections-per-host="${mongo.connectionsPerHost}"
                       threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                       connect-timeout="${mongo.connectTimeout}"
                       max-wait-time="${mongo.maxWaitTime}"
                       socket-timeout="${mongo.socketTimeout}"/>
    </mongo:mongo-client>
    <!-- mongo的工厂,通过它来取得mongo实例,dbname为mongodb的数据库名,没有的话会自动创建 -->
    <mongo:db-factory  id="mongoDbFactory" dbname="mongoLida" mongo-ref="mongo" />

    <!-- 只要使用这个调用相应的方法操作 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>
    <!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
    <mongo:repositories base-package="com.lida.mongo" />
</beans>

mongo.properties

#mongoDB连接配置
mongo.hostport=127.0.0.1:27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#连接超时时间
mongo.connectTimeout=1000
#等待时间
mongo.maxWaitTime=1500
#Socket超时时间
mongo.socketTimeout=1500

pom.xml

<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>com.liad</groupId>
  <artifactId>mongo</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mongo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!--使用junit4,注解的方式测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!--日志-->
    <!--日志 slf4j,log4j,logback,common-logging-->
    <!--slf4j是规范/接口-->
    <!--log4j,logback,common-logging是日志实现 本项目使用slf4j + logback -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.12</version>
    </dependency>
    <!--实现slf4j并整合-->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.1</version>
    </dependency>
    <!--数据库相关-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.22</version>
      <!--maven工作范围   驱动在真正工作的时候使用,故生命周期改为runtime-->
      <scope>runtime</scope>
    </dependency>

    <!--servlet web相关-->
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.5.4</version>
    </dependency>

    <!--spring-->
    <!--spring核心-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <!--spring dao-->
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.8.0.RELEASE</version>
      </dependency>
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <!--spring web-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <!--spring test-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.10</version>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-framework-bom</artifactId>
        <version>${spring.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.6.9</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <finalName>mongo</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

两个实体类:


/**
 * Created by DuLida on 2016/10/20.
 */
public class Address {

    private String city;
    private String street;
    private int num;

    public Address() {
    }

    public Address(String city, String street, int num) {
        this.city = city;
        this.street = street;
        this.num = num;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", street='" + street + '\'' +
                ", num=" + num +
                '}';
    }
}

/**
 * Created by DuLida on 2016/10/20.
 */
@Document(collection="person")
public class Person implements Serializable {

    @Id
    private ObjectId id;
    private String name;
    private int age;
    private Address address;

    public Person() {
    }

    public Person( String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public ObjectId getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}

JPA的dao,注意这里只要继承MongoRepository不用写注解spring就能认识这是个Repository,MongoRepository提供了基本的增删改查,不用实现便可直接调用,例如testMongo的personDao.save(persons);

public interface PersonDao extends MongoRepository<Person, ObjectId> {

    @Query(value = "{'age' : {'$gte' : ?0, '$lte' : ?1}, 'name':?2 }",fields="{ 'name' : 1, 'age' : 1}")
    List<Person> findByAge(int age1, int age2, String name);

}

mongoTemplate的dao

/**
 * Created by DuLida on 2016/10/21.
 */
public interface PersonMongoDao {
     List<Person> findAll();
     void insertPerson(Person user);
     void removePerson(String userName);
     void updatePerson();
     List<Person> findForRequery(String userName);
}
@Repository("personMongoImpl")
public class PersonMongoImpl implements PersonMongoDao {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public List<Person> findAll() {
        return mongoTemplate.findAll(Person.class,"person");
    }

    @Override
    public void insertPerson(Person person) {
        mongoTemplate.insert(person,"person");
    }

    @Override
    public void removePerson(String userName) {
        mongoTemplate.remove(Query.query(Criteria.where("name").is(userName)),"person");
    }

    @Override
    public void updatePerson() {
        mongoTemplate.updateMulti(Query.query(Criteria.where("age").gt(3).lte(5)), Update.update("age",3),"person");
    }

    @Override
    public List<Person> findForRequery(String userName) {
        return mongoTemplate.find(Query.query(Criteria.where("name").is(userName)),Person.class);
    }
}

JPA查询的测试类:

/**
 * Created by DuLida on 2016/10/20.
 */
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件
@ContextConfiguration({"classpath:spring/spring-context.xml","classpath:spring/spring-mongo.xml"})
public class PersonDaoTest {

    @Resource
    private PersonDao personDao;

    /*先往数据库中插入10个person*/
    @Test
    public void testMongo() {
        List<Person> persons = new ArrayList<Person>();
        for (int i = 0; i < 10; i++) {
           persons.add(new Person("name"+i,i,new Address("石家庄","裕华路",i)));
        }
        personDao.save(persons);
    }

    @Test
    public void findMongo() {
        System.out.println(personDao.findByAge(2,8,"name6"));

    }

}

mongoTemplate查询的测试类

/**
 * Created by DuLida on 2016/10/21.
 */
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件
@ContextConfiguration({"classpath:spring/spring-context.xml","classpath:spring/spring-mongo.xml"})
public class MongoTemplateTest {

    @Resource
    private PersonMongoImpl personMongo;

    @Test
    public void testMongoTemplate() {

        //personMongo.insertPerson(new Person("wukong",24,new Address("石家庄","鑫达路",20)));
        //personMongo.removePerson("name3");
        //personMongo.updatePerson();
        //System.out.println(personMongo.findAll());
        System.out.println(personMongo.findForRequery("wukong"));
    }
}

注意测试前请先通过testMongo()向数据库中插入数据。
项目源码Git地址,仅供学习使用https://github.com/dreamSmile/mongo.git
参考资料http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/

点击查看更多内容
18人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
26
获赞与收藏
340

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消