Commit 5f63bc0f by 小柒2012

初交

parents
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>spring-boot-mongodb</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/webapp"/>
<classpathentry kind="src" path="target/m2e-wtp/web-resources"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="spring-boot-mongodb">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
<property name="context-root" value="spring-boot-mongodb"/>
<property name="java-output-path" value="/spring-boot-mongodb/target/classes"/>
</wb-module>
</project-modules>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="jst.web" version="2.3"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.7"/>
</faceted-project>
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
\ No newline at end of file
Window
\ No newline at end of file
disabled=06target
eclipse.preferences.version=1
This diff is collapsed. Click to expand it.
作者: 小柒2012
出处: https://blog.52itstyle.com
<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.itstyle.mongodb</groupId>
<artifactId>spring-boot-mongodb</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mongodb</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<!-- spring-boot-starter-parent包含了大量配置好的依赖管理,在自己项目添加这些依赖的时候不需要写<version>版本号 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<!-- 配置依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包项目 mvn clean package -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<!-- mvn spring-boot:run 热部署启动 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.itstyle.mongodb;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.itstyle.mongodb"})
public class Application {
private static final Logger logger = Logger.getLogger(Application.class);
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
logger.info("项目启动 ");
}
}
\ No newline at end of file
package com.itstyle.mongodb.model;
import java.io.Serializable;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection="users")
@CompoundIndexes({
@CompoundIndex(name = "age_idx", def = "{'name': 1, 'age': -1}")
})
public class Users implements Serializable{
private static final long serialVersionUID = 1L;
@Indexed
private String uid;
private String name;
private int age;
@Transient
private String address;
public Users(String uid, String name, int age) {
super();
this.uid = uid;
this.name = name;
this.age = age;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return String.format(
"Customer[name='%s', age='%s']",
name, age);
}
}
package com.itstyle.mongodb.service;
import java.util.List;
import com.itstyle.mongodb.model.Users;
/**
* mongodb 案例
* 创建者 小柒
* 创建时间 2017年7月19日
*
*/
public interface IUserService {
public void saveUser(Users users);
public Users findUserByName(String name);
public void removeUser(String name);
public void updateUser(String name, String key, String value);
public List<Users> listUser();
}
package com.itstyle.mongodb.service;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.itstyle.mongodb.model.Users;
public interface UserRepository extends MongoRepository<Users, Integer> {
public Users findUserByName(String name);
public void removeUser(String name);
public void updateUser(String name, String key, String value);
}
package com.itstyle.mongodb.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import com.itstyle.mongodb.model.Users;
import com.itstyle.mongodb.service.IUserService;
@Component("userService")
public class UserRepositoryImpl implements IUserService {
@Autowired
MongoOperations mongoTemplate;
public void saveUser(Users users) {
mongoTemplate.save(users);
}
public Users findUserByName(String name) {
return mongoTemplate.findOne(
new Query(Criteria.where("name").is(name)), Users.class);
}
public void removeUser(String name) {
mongoTemplate.remove(new Query(Criteria.where("name").is(name)),
Users.class);
}
public void updateUser(String name, String key, String value) {
mongoTemplate.updateFirst(new Query(Criteria.where("name").is(name)),
Update.update(key, value), Users.class);
}
public List<Users> listUser() {
return mongoTemplate.findAll(Users.class);
}
}
# \u9879\u76eecontextPath\uff0c\u4e00\u822c\u5728\u6b63\u5f0f\u53d1\u5e03\u7248\u672c\u4e2d
server.context-path=/mongodb
# \u670d\u52a1\u7aef\u53e3
server.port=8080
# session\u6700\u5927\u8d85\u65f6\u65f6\u95f4(\u5206\u949f)\uff0c\u9ed8\u8ba4\u4e3a30
server.session-timeout=60
# \u8be5\u670d\u52a1\u7ed1\u5b9aIP\u5730\u5740\uff0c\u542f\u52a8\u670d\u52a1\u5668\u65f6\u5982\u672c\u673a\u4e0d\u662f\u8be5IP\u5730\u5740\u5219\u629b\u51fa\u5f02\u5e38\u542f\u52a8\u5931\u8d25\uff0c\u53ea\u6709\u7279\u6b8a\u9700\u6c42\u7684\u60c5\u51b5\u4e0b\u624d\u914d\u7f6e
# server.address=192.168.16.11
# tomcat\u6700\u5927\u7ebf\u7a0b\u6570\uff0c\u9ed8\u8ba4\u4e3a200
server.tomcat.max-threads=800
# tomcat\u7684URI\u7f16\u7801
server.tomcat.uri-encoding=UTF-8
#mongo2.x\u652f\u6301\u4ee5\u4e0a\u4e24\u79cd\u914d\u7f6e\u65b9\u5f0f mongo3.x\u4ec5\u652f\u6301uri\u65b9\u5f0f
#mongodb note:mongo3.x will not use host and port,only use uri
#spring.data.mongodb.host=47.94.232.109
#spring.data.mongodb.port=27017
#spring.data.mongodb.database=itstyle
#\u6ca1\u6709\u8bbe\u7f6e\u5bc6\u7801
#spring.data.mongodb.uri=mongodb://47.94.232.109:27017/itstyle
#\u8bbe\u7f6e\u4e86\u5bc6\u7801
spring.data.mongodb.uri=mongodb://itstyle:itstyle@47.94.232.109:27017/itstyle
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
package com.itstyle.mongodb.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.itstyle.mongodb.service.IUserService;
import com.itstyle.mongodb.service.UserRepository;
@SpringBootApplication
@ComponentScan(basePackages={"com.itstyle.mongodb"})
public class SpringbootMongodbApplication implements CommandLineRunner {
@Autowired
private IUserService userService;
@Autowired
private UserRepository userRepository;
public static void main(String[] args) {
SpringApplication.run(SpringbootMongodbApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
try {
userRepository.removeUser("小明");
/*Users users = new Users("1", "小明", 10);
users.setAddress("青岛市");
userService.saveUser(users);
List<Users> list = userService.listUser();
System.out.println("一共这么多人:"+list.size());*/
} catch (Exception e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment