Spring Boot从入门到精通(九)整合Spring Data JPA应用框架

JPA

JPAJava Persistence APISunJavaJDK 5.0XML

JPAJava EEJava SEORMJPAHibernateTopLinkJDOORM使JPASpringEJB3.0

Spring Data JPA

Spring Data JPASpringORMJPAJPA使Hibernate JPA使访使Spring Data JPA

使Spring BootSpring Data JPAJava

Spring Data JPA

1Maven

1Spring Boothttp://start.spring.io/

2

Spring BootSpring BootSpring BootSpring BootJava

MavenSpring Data JPA

 

3Generate-Crtl+使IntelliJ IDEAFile->Project Structure -> + MavenOK

4MaveninstallMaven

2pom.xml

MySQLMaven

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>

  

spring-boot-starter-parenthjar使maven helper

3
application.properties

#数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/dba
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect

#端口号
server.port=8080

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.jdbc.batch_size=50
logging.level.org.hibernate.type.descriptor.sql=trace

  

4BootUserBootUserMySQL

package com.yoodb.study.demo05.entity;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="boot_user")
public class BootUser implements Serializable {
    @Id
    //strategy=GenerationType.IDENTITY 自增长
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id")
    private String id;

    @Column(name="user_name")
    private String name;

    @Column(name="detail")
    private String detail;

    public String getId() {
        return id;
    }

    public void setId(String id) {
  this.id=id;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetDetail(){
returndetail;
}

publicvoidsetDetail(Stringdetail){
this.detail=detail;
}
}

  

5RepositoryUserRepository

package com.yoodb.study.demo05.repository;

import com.yoodb.study.demo05.entity.BootUser;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<BootUser, Integer> {

}

  

6UsersDaoImplTest

package com.yoodb.study.demo05;

import com.yoodb.study.demo05.repository.UserRepository;
import com.yoodb.study.demo05.entity.BootUser;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;

import javax.transaction.Transactional;

@SpringBootTest
public class UsersDaoImplTest  {
    @Autowired
    private UserRepository userRepository;

    /**
     * 添加用户
     */
 @Test
@Transactional//对于事务提交方式默认回滚
@Rollback(false)//取消自动回滚
publicvoidtestInsertUsers(){
BootUserusers=newBootUser();
users.setName("关注微信公众号“Java精选”");
users.setDetail("SpringBoot从入门到精通系列文章持续更新中,带你从入门到精通,玩转SpringBoot框架。");
this.userRepository.save(users);
}

}

  

7IntelliJ IDEA

Error running 'UsersDaoImplTest.testInsertUsers': Failed to resolve org.junit.platform:junit-platform-launcher:1.6.0

IntelliJ使IDEAMaven settings.xmlMaven CentralMavenpom.xml

<dependency>
	<groupId>org.junit.platform</groupId>
	<artifactId>junit-platform-launcher</artifactId>
	<scope>test</scope>
</dependency>

来源:素文宅博客

转自:https://blog.yoodb.com/yoodb/article/detail/1574

Spring BootSpring Data JPAspringboot-study-demo05https://github.com/yoodb/springboot

Spring bootSpring Data JPA便

posted @ 2020-03-16 00:33  yoodb  阅读(707)  评论(0编辑  收藏  举报