通过spring tools suite新建一个spring project。带maven的即可
pom.xml文件配置
001 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
003 |
< modelVersion >4.0.0</ modelVersion > |
004 |
< groupId >org.springframework.samples</ groupId > |
005 |
< artifactId >springbootmongodb</ artifactId > |
006 |
< version >0.0.1-SNAPSHOT</ version > |
007 |
< name >springbootmongodb</ name > |
011 |
< java.version >1.7</ java.version > java版本号,根据自己情况来 |
012 |
< project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > |
013 |
< project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding > |
015 |
< spring-framework.version >4.2.3.RELEASE</ spring-framework.version > 这里版本要高 |
016 |
< org.aspectj-version >1.6.10</ org.aspectj-version > |
018 |
< hibernate.version >4.2.1.Final</ hibernate.version > //默认生成了,没啥用 |
020 |
< logback.version >1.0.13</ logback.version > |
021 |
< slf4j.version >1.7.5</ slf4j.version > |
023 |
< junit.version >4.11</ junit.version > |
027 |
< groupId >org.springframework.boot</ groupId > |
028 |
< artifactId >spring-boot-starter-parent</ artifactId > |
029 |
< version >1.3.3.RELEASE</ version > |
035 |
< groupId >org.springframework</ groupId > |
036 |
< artifactId >spring-context</ artifactId > |
037 |
< version >${spring-framework.version}</ version > |
040 |
< groupId >org.springframework</ groupId > |
041 |
< artifactId >spring-tx</ artifactId > |
042 |
< version >${spring-framework.version}</ version > |
047 |
< groupId >org.slf4j</ groupId > |
048 |
< artifactId >slf4j-api</ artifactId > |
049 |
< version >${slf4j.version}</ version > |
050 |
< scope >compile</ scope > |
053 |
< groupId >ch.qos.logback</ groupId > |
054 |
< artifactId >logback-classic</ artifactId > |
055 |
< version >${logback.version}</ version > |
056 |
< scope >runtime</ scope > |
061 |
< groupId >org.hibernate</ groupId > |
062 |
< artifactId >hibernate-entitymanager</ artifactId > |
063 |
< version >${hibernate.version}</ version > |
068 |
< groupId >org.springframework.boot</ groupId > |
069 |
< artifactId >spring-boot-starter-web</ artifactId > |
072 |
< groupId >org.springframework.boot</ groupId > |
073 |
< artifactId >spring-boot-starter-tomcat</ artifactId > |
079 |
< groupId >org.springframework.boot</ groupId > |
080 |
< artifactId >spring-boot-starter-actuator</ artifactId > |
084 |
< groupId >org.springframework.boot</ groupId > |
085 |
< artifactId >spring-boot-starter-tomcat</ artifactId > |
086 |
< scope >provided</ scope > |
093 |
< groupId >org.springframework.boot</ groupId > |
094 |
< artifactId >spring-boot-starter-data-mongodb</ artifactId > |
099 |
< groupId >org.aspectj</ groupId > |
100 |
< artifactId >aspectjrt</ artifactId > |
101 |
< version >${org.aspectj-version}</ version > |
107 |
< groupId >org.springframework</ groupId > |
108 |
< artifactId >spring-test</ artifactId > |
109 |
< version >${spring-framework.version}</ version > |
113 |
< groupId >junit</ groupId > |
114 |
< artifactId >junit</ artifactId > |
115 |
< version >${junit.version}</ version > |
新建实体类 SysUser
package entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class SysUser {
//mongodb需要指定ID
@Id
private long seqid;
private String name;
private String password;
private long roleId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public long getRoleId() {
return roleId;
}
public void setRoleId(long roleId) {
this.roleId = roleId;
}
public long getSeqid() {
return seqid;
}
public void setSeqid(long seqid) {
this.seqid = seqid;
}
}
建立接口SysUserRepository extends MongoRepository
package mapper;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import entity.SysUser;
@Component
public interface SysUserRepository extends MongoRepository {
SysUser findByName(String name); //自定义的方法
}
建立service类,写业务逻辑方法
SysUserService
package service;
import java.util.List;
import mapper.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import entity.SysUser;
@Service
@EnableMongoRepositories(basePackages="mapper" ) //扫描指定目录下面的MongoRepositories接口
public class SysUserService{
@Autowired
private SysUserRepository sysUserRepository;
@RequestMapping("save")
public String save(){
SysUser sysUser = new SysUser();
sysUser.setName("张三");
sysUser.setPassword("123456");
sysUser.setSeqid(1L);
sysUserRepository.save(sysUser);
sysUser = new SysUser();
sysUser.setName("李四");
sysUser.setPassword("123456");
sysUser.setSeqid(2L);
sysUserRepository.save(sysUser);
return "ok";
}
@RequestMapping("find")
public List find(){
return sysUserRepository.findAll();
}
@RequestMapping("findByName")
public SysUser findByName(){
return sysUserRepository.findByName("张三");
}
}
最后是控制器
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.SysUserService;
import entity.SysUser;
/**
* Created by wenchao.ren on 2014/4/26.
*/
@SpringBootApplication
//启注解事务管理
@Controller
@Import (SysUserService.class )
public class SimpleController {
@Autowired
private SysUserService sysUserService;
@RequestMapping(value ="/hello", method = RequestMethod.GET)
@ResponseBody
public String hello(){
SysUser sysUser = new SysUser();
sysUserService.save();
SysUser sysUser2= sysUserService.findByName();
return sysUser2.getName();
}
public static void main(String[] args) {
SpringApplication.run(SimpleController.class, args);
}
}
还有applicationContext.properties
# mongodb note:mongo3.x will not use host and port,only use uri
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://localhost:27017/springbootdb
server.port=8011