Spring-boot访问MongoDB
2015-04-01 14:39
363 查看
1、访问配置信息
2、POJO类
3、Repository接口
4、Application
package hello; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.authentication.UserCredentials; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; import com.mongodb.Mongo; import com.mongodb.MongoClient; @Configuration public class MongoConfig extends AbstractMongoConfiguration { @Bean public Mongo mongo() throws Exception { return new MongoClient(); } @Bean public MongoTemplate mongoTemplate() throws Exception { UserCredentials user = new UserCredentials("scott", "tiger"); return new MongoTemplate(mongo(), "test1", user); } @Override protected String getDatabaseName() { // TODO Auto-generated method stub return "test1"; } }
2、POJO类
package hello; import org.springframework.data.annotation.Id; public class Customer { @Id private String id; private String firstName; private String lastName; public Customer() {} public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format( "Customer[id=%s, firstName='%s', lastName='%s']", id, firstName, lastName); } }
3、Repository接口
package hello; import java.util.List; import org.springframework.data.mongodb.repository.MongoRepository; public interface CustomerRepository extends MongoRepository<Customer, String> { public Customer findByFirstName(String firstName); public List<Customer> findByLastName(String lastName); }
4、Application
package hello; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Import; @SpringBootApplication @AutoConfigureBefore @Import(MongoConfig.class) public class Application implements CommandLineRunner { @Autowired private CustomerRepository repository; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { repository.deleteAll(); // save a couple of customers repository.save(new Customer("Alice", "Smith")); repository.save(new Customer("Bob", "Smith")); // fetch all customers System.out.println("Customers found with findAll():"); System.out.println("-------------------------------"); for (Customer customer : repository.findAll()) { System.out.println(customer); } System.out.println(); // fetch an individual customer System.out.println("Customer found with findByFirstName('Alice'):"); System.out.println("--------------------------------"); System.out.println(repository.findByFirstName("Alice")); System.out.println("Customers found with findByLastName('Smith'):"); System.out.println("--------------------------------"); for (Customer customer : repository.findByLastName("Smith")) { System.out.println(customer); } } }
相关文章推荐
- Spring Data MongoDB入门-访问mongodb
- Spring Boot Mongodb
- springboot-aop访问拦截
- SpringBoot中实现拦截器级别的URl访问过快拦截,并利用JPA实现IP黑名单的功能。
- Spring Boot中使用JdbcTemplate访问数据库
- Creating a REST API with Spring Boot and MongoDB
- Spring Boot 配置JdbcTemplate访问数据库
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- Spring Boot 微框架学习(利用Spring Boot编写一个访问数据库的helloword)
- springboot副本集mongoDB
- Spring Boot Mongodb
- Java 访问 MongoDB (使用Spring-Data-MongoDB)
- 禁用springboot中对mongodb的autoConfiguration
- Spring Boot实现跨域访问实现代码
- 第十二章 springboot + mongodb(复杂查询)
- spring boot demo(spring jdbc访问数据)
- spring boot 连接mongodb
- 快速构建Spring boot项目(功能是检测访问页面的设备)
- Spring Boot与Spring Security整合后post数据不了,403拒绝访问
- springboot中整合activiti流程设计器时遇到访问项目需要输入tomcat用户名和密码的问题