SpringBoot專案使用Jpa向資料庫寫入資料,自動添加當前時間
阿新 • • 發佈:2018-12-15
ids for this class must be manually assigned before calling save(): com.example.entity.UserInfo
pom檔案引入相關jar包,使用mysql資料庫,阿里的連線池,和jackjson
<?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>org.monitot.server.api</groupId> <artifactId>NewIdeaTest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>NewIdeaTest</name> <description>monitor function</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.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-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置檔案中配置mysql,連結資訊與jpa設定
配置檔案詳情
server.port=9188 spring.datasource.url=jdbc:mysql://192.168.88.12:3306/Test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.platform=mysql spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.jpa.database=MYSQL spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true spring.jackson.serialization.indent_output=true spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
實體類標註@EntityListeners(AuditingEntityListener.class),
在屬性上使用@CreateDate註解,這樣,jpa插入資料庫時,會自動插入當前時間,
使用@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss")註解,在存取資料時會按照你指定的格式存取。
另外,在啟動類上加入註解@EnableJpaAuditing
package com.example.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; @Data @Entity @EntityListeners(AuditingEntityListener.class) @Table(name="USERINFO") public class UserInfo implements Serializable{ /** * */ private static final long serialVersionUID = -3145519548971854247L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="ID") private Integer ID; @Column(name="NAME") private String name; @Column(name="CREATE_TIME") @CreatedDate @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss") private Date createTime; }
啟動類
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;
//禁止連資料庫
//@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@SpringBootApplication
@EnableJpaAuditing
@EnableScheduling
public class NewIdeaTestApplication {
public static void main(String[] args) {
SpringApplication.run(NewIdeaTestApplication.class, args);
}
}
這樣就可以了,可以寫介面測試一下
package com.example.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.entity.UserInfo;
public interface UserInfoRepository extends JpaRepository<UserInfo, Integer>{
}
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.example.Repository.UserInfoRepository;
import com.example.entity.UserInfo;
@Service
public class UserInfoService {
@Autowired
private UserInfoRepository userInfoRepository;
public UserInfo saveInfo() {
UserInfo userInfo=new UserInfo();
userInfo.setName("yin");
return userInfoRepository.saveAndFlush(userInfo);
}
@Scheduled(cron=" 1/5 * * * * * ")
public void test() {
System.err.println(this.saveInfo());;
}
}
使用定時任務,輸出結果如下:自動插入了時間
資料庫插入的結果:時間格式與實體類指定的格式一致
原創文章,轉載請註明出處