springboot随笔之四:springboot整合spring-data-jpa(mogodb)

一、在mongodb中准备相应数据

1、创建数据库并创建对应可以访问的对应库的用户信息

2、创建对应collection,并插入相应的文档数据

二、结合前面的实例,项目中配置包依赖及数据源

1、jpa包依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
         <!-- mongodb依赖 --> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

2、配置数据源

# mongodb 2.4以上版本支持uri配置(此处未设置用户名密码,mongodb默认不设置用户名密码)
spring.data.mongodb.uri=mongodb://admin:123456@localhost/test
#spring.data.mongodb.database=test
#spring.data.mongodb.host=127.0.0.1
#spring.data.mongodb.port=27017
#spring.data.mongodb.repositories.enabled=true
#spring.data.mongodb.username=admin
#spring.data.mongodb.password=123456

三、编写数据库表对应的实体类

package com.expample.springboot04.domain;


//与mongo collection对应的Entity类

/**
* ORM对象关系映射,即java 实体类和mongodb collection的映射关系:
* 类成员变量 -> collection字段,类名 -> collection名
**/

import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import javax.persistence.Entity;
import javax.persistence.Id;

//注解实体类,springboot自动装载为bean
@Entity
@Document("student")
public class Student {

 // 指定主键字段,注意import是javax持久化的Id而非springboot annotation的Id
 // @Field("id")指定该主键名称为id,如果不添加此项注解的话,
 // 程序会认为private int id对应的是mongodb studdent集合中的objectid字段(该字段是mongodb中的默认主键)
 @Id
 @Field("id")
 private int id;
 private String name;
 private int age;

 // 默认构造方法
 public Student(){
 }
 
 // 有参构造方法
 public Student(int id, String name, int age){
     this.id = id;
     this.name = name;
     this.age = age;
 }

 public int getId() {
     return id;
 }

 public void setId(int 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;
 }
}

四、编写持久层

package com.expample.springboot04.dao;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.expample.springboot04.domain.Student;

@Repository("studentDao")
public interface IStudentDao extends MongoRepository<Student, Integer> {

}

其中MongoRepository<Student, Integer>
Student表示数据库中表对应的实体类
Integer表示数据中表对应主键类型

Spring容器会根据@Repository("studentDao")创建对应的对象studentDao

五、编写业务层

package com.expample.springboot04.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.expample.springboot04.domain.Student;

@Service
public interface IStudentService {

	List<Student> findAllStudents();
}
package com.expample.springboot04.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.expample.springboot04.dao.IStudentDao;
import com.expample.springboot04.domain.Student;
import com.expample.springboot04.service.IStudentService;

@Service("studentService")
public class StudentServiceImpl implements IStudentService {

	@Autowired
	private IStudentDao studentDao;
	@Override
	public List<Student> findAllStudents() {
		// TODO Auto-generated method stub
		return studentDao.findAll();
	}

}

六、完善控制器

package com.expample.springboot04.control;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.expample.springboot04.domain.Student;
import com.expample.springboot04.service.IStudentService;

@RestController
@RequestMapping("/exampleControl")
public class ExampleControl {
	@Autowired
	private IStudentService studentService;
	@RequestMapping("/testExample")
	public String testExample() {
        return "testExample";
	}
	
	@RequestMapping("/showStudents")
	public List<Student> showStudents() {
		return studentService.findAllStudents();
	}

}

七、运行springboot启动类,浏览器测试http://localhost:8000/test/exampleControl/showStudents

 

点击跳转到源码下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值