[JAVA] [How to install Spring Data Jpa]

① Add a tag to pom.xml.

<!-Settings to enable DataJpa in Spring Boot-> org.springframework.boot spring-boot-starter-data-jpa

② Create an Entity class.

Add [must] @Entity. This indicates that it is an Entity class. Add [must] @Table.name is the name of the DB table. If omitted, it will be mapped to a table with a capitalized class name. [must] Add @Id to the column with PRIMARY KEY. It will not work without @Id. If not, add PRIMARY KEY to DB or Add @GeneratedValue (strategy = GenerationType.IDENTITY). With this, you can generate a PRIMARY KEY. If DB is a composite key, use @ javax.persistence.EmbeddedId

Add [must] @Column and specify the column name to be mapped. If omitted, the property name will be mapped to the column with the capitalized name.

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;

// Entity class = DB table @Entity @Table(name = "player") public class Player {

@Id
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "nationality")
private String nationality;

// Getter Setter below

③ Create a repository. Make it with an interface instead of the usual class.

[must] Add the following after the class name.

				⬇		︎
	extends JpaRepository<Player, Integer>

import org.springframework.data.jpa.repository.JpaRepository; import jp.co.rakus.football.entity.Player;

//JpaRepositoryを継承する事で、fineOne、findAll、save、deleteの4メソッドが使用 // Generics specifies the entity's class name and primary key type. public interface PlayerRepository_DataJpa extends JpaRepository<Player, Integer> {

}

④ Now that it is ready, use it in services and controllers.

@Service @EnableTransactionManagement public class PlayerService {

@Autowired
PlayerRepository_DataJpa playerRepository_DataJpa;
	
public List<Player> findAll() {
	return playerRepository_DataJpa.findAll();
}

public void saveUserInfo(User user) {
	userRepositoryDataJpa.save(user);
}

}

Recommended Posts

[How to install Spring Data Jpa]
How to use Spring Data JDBC
How to install Docker
How to install docker-machine
How to install MySQL
How to install ngrok
How to define multiple orm.xml in Spring4, JPA2.1
[Rails] How to install devise
[spring] Let's use Spring Data JPA
How to install Boots Faces
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
How to install JDK8-10 (Mac)
I tried to get started with Spring Data JPA
How to use Lombok in Spring
[Rails] How to install Font Awesome
How to unit test Spring AOP
How to install JMeter for Mac
How to install ImageMagick on Windows 10
How to install ruby through rbenv
How to set Spring Boot + PostgreSQL
Super rough! How to install Dagger2
How to install Bootstrap in Ruby
How to install MariaDB 10.4 on CentOS 8
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
How to install WildFly on Ubuntu 18.04
How to use ModelMapper (Spring boot)
Spring Data JPA SQL log output
How to install Swiper in Rails
How to get date data in Ruby
OR search with Spring Data Jpa Specification
How to install Play Framework 2.6 for Mac
[Java] How to add data to List (add, addAll)
How to include Spring Tool in Eclipse 4.6.3?
Exists using Specification in Spring Data JPA
How to install Eclipse (Photon) on Mac
How to install production Metabase on Ubuntu
[IOS] How to get data from DynamoDB
How to install beta php8.0 on CentOS8
To write Response data directly in Spring
How to install the legacy version [Java]
How to delete data with foreign key
How to install kafkacat on Amazon Linux2
[Spring MVC] How to pass path variables
How to write Spring AOP pointcut specifier
How to overwrite Firebase data in Swift
How to split Spring Boot message file
[Rails] How to handle data using enum
How to deploy
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to install network drivers on standalone Ubuntu
Understand how to share Spring DB connection (DB transaction)
How to use built-in h2db with spring boot
Spring Data JPA save select-insert is only insert
How to make Spring Boot Docker Image smaller
How to install Titan2D (v4.2.0) in virtual environment
How to use Spring Boot session attributes (@SessionAttributes)
How to install NVIDIA driver on Ubuntu 18.04 (Note)
Spring Data REST HAL Browser to check Spring REST operation
How to add a classpath in Spring Boot