展示一下我的项目目录结构
1.引入jar包 下面展示我的pom.xml 文件 主要引入:
spring-boot-starter-data-mongodb
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sdnware.odl</groupId>
<artifactId>sdnware-odl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sdnware-odl</name>
<description>sdnware-OpendayLight Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> -->
<!-- spring-boot-starter-data-mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.添加配置 application.yml 如果不连接mysql数据库可以去掉配置信息
spring:
datasource:
url: jdbc:mysql://192.168.**.138:3306/****?characterEncoding=UTF-8
username: ****
password: ****
driver-class-name: com.mysql.jdbc.Driver
jpa:
database: MYSQL
show-sql: true
hibernate:
ddl-auto: update #validate
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
data:
mongodb:
#数据库名
database: ****
#地址
host: 192.168.**.130
#端口号
port: 30000
3.新建一个实体类 Student
package com.example.momgodb.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import org.springframework.data.annotation.Id;
/**
*
*/
@AllArgsConstructor
@Data
@ToString
public class Student {
@Id
private Long id;
private String name;
private Integer age;
private String home;
}
4. 新建一个接口继承 MongoRepository
package com.example.momgodb.dao;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.example.momgodb.model.Student;
public interface StudentRepository extends MongoRepository<Student, Long> {
Student findByName(String name);
}
5. 测试
package com.example.momgodb;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.momgodb.dao.StudentRepository;
import com.example.momgodb.model.Student;
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableAsync
@EnableJpaAuditing
public class MomgodbApplicationTests {
@Autowired
private StudentRepository studentRepository;
@Test
public void contextLoads() {
}
/**
* 第一次单元测试
*
* @throws Exception
*/
@Test
public void insertStudent() throws Exception {
studentRepository.save(new Student(1L, "小明", 30,"1小区"));
studentRepository.save(new Student(2L, "小红", 40,"2小区"));
studentRepository.save(new Student(3L, "小王", 50,"3小区"));
}
}
6.打印测试结果,查看mongo数据库
7.剩下的就可以根据JPA的语法进行操作了。