首先是依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
springboot自动生成MySQL的表格
配置文件需要注意一下
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:///biyesheji #三条斜杠代表本地3306端口下的xxxx数据库 我这里就是本地3306端口下的biyesheji数据库
spring.datasource.username=账号
spring.datasource.password=密码
spring.data.mongodb.port=27017
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.database=数据库是哪个
spring.jpa.show-sql=true #显示sql语句
spring.jpa.hibernate.ddl-auto=update #自动生成(更新)表格 这个不配置的话不能自动生成
- 写一个java类
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
@Entity
@Data //自动生成get set toString
@AllArgsConstructor //自动生成全部参数的构造方法
@EntityListeners(AuditingEntityListener.class) //这个要加 ,是配合@CreateDate 和 @LastModifiedDate 使用的注解
@Table(name = "test")
public class Mysql_Entity_Test {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)//因为连接的是mysql数据库,推荐使用这个自动生成策略
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "createTime")
@CreatedDate //加这个注解,到时候可以自动生成时间
private Date createTime; //Long Date LocalDateTime 都可以
@Column(name = "lastModifyTime")
@LastModifiedDate //加这个注解,到时候可以自动生成时间
private Date lastModifyTime; //Long Date LocalDateTime 都可以
public Mysql_Entity_Test() {
}
}
- 顺手写上 Repository
import com.example.domain.Mysql_Entity_Test;
import org.springframework.data.jpa.repository.JpaRepository;
public interface Mysql_Entity_TestRepository extends JpaRepository<Mysql_Entity_Test,Integer> {
}
- 主类加上注解
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
@SpringBootApplication
@EnableJpaAuditing //加入这个配合 mysql 使用
@EnableMongoAuditing //加入这个 配合 mongodb 使用
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 测试
import com.example.domain.Mysql_Entity_Test;
import com.example.repository.Mysql_Entity_TestRepository;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
class Mysql_Entity_Test_test {
@Autowired
private Mysql_Entity_TestRepository testRepository;
@Test
public void test1(){
Mysql_Entity_Test test=new Mysql_Entity_Test();
test.setName("lisi");
testRepository.save(test);
//只需要设置一下 不是自动生成的字段值就可以
}
}
- 结果
springboot自动生成MongoDB的表格
- 写一个java类
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import javax.persistence.EntityListeners;
import javax.persistence.Id;
import java.time.LocalDateTime;
import java.util.Date;
@Document("mdt")
@Data
@EntityListeners(AuditingEntityListener.class)
public class Mongodb_Domain_test {
@Id
private String Object;
@Field("name")
private String name;
@CreatedDate
@Field("create_time")
private LocalDateTime createTime; // 创建时间
@LastModifiedDate
@Field("update_time")
private LocalDateTime updateTime; // 更新时间
public Mongodb_Domain_test() {
}
}
- 顺手写Repository
import com.example.domain.Mongodb_Domain_test;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface Mongodb_Domain_testRepository extends MongoRepository<Mongodb_Domain_test,String> {
}
- 测试
import com.example.domain.Mongodb_Domain_test;
import com.example.domain.Mysql_Entity_Test;
import com.example.repository.Mongodb_Domain_testRepository;
import com.example.repository.Mysql_Entity_TestRepository;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
class Mongodb_Domain_Test_test {
@Autowired
private Mongodb_Domain_testRepository repository;
@Test
public void test1(){
Mongodb_Domain_test test=new Mongodb_Domain_test();
test.setName("zhangsan1");
repository.insert(test);
}
}
- 结果
总结
总结来说 第一:配置文件 一行
第二: @CreateDate 和 @ LastModifyData
第三: @EntityListeners(AuditingEntityListener.class)
第四: 主类上要加入配套的注解 mysql加@xxxx
mongo加@xxxx