JPA introduction
JPA (Java Persistence API)
Integrate JPA
Jap rely on
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Copy the code
Of course, you need to add spring-boot-starter-JDBC and mysql-connector-Java database dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Copy the code
The core concept
The core interface of JPA is Repository. The Repository interface has two generic parameters
, where T is the entity object we need to operate on, and ID is the unique primary key field of our database entity object. In JPA, Interface CrudRepository inherits the Repository interface to provide us with sophisticated CRUD capabilities.
The next code is CrudRepository
@NoRepositoryBean
public interface CrudRepository<T.ID> extends Repository<T.ID> {
<S extends T> S save(S var1);
<S extends T> Iterable<S> saveAll(Iterable<S> var1);
Optional<T> findById(ID var1);
boolean existsById(ID var1);
Iterable<T> findAll(a);
Iterable<T> findAllById(Iterable<ID> var1);
long count(a);
void deleteById(ID var1);
void delete(T var1);
void deleteAll(Iterable<? extends T> var1);
void deleteAll(a);
}
Copy the code
On the basis of interface CrudRepository, JPA provides PagingAndSortingRepository interface are used to implement sorting and paging.
@NoRepositoryBean
public interface PagingAndSortingRepository<T.ID> extends CrudRepository<T.ID> {
Iterable<T> findAll(Sort var1);
Page<T> findAll(Pageable var1);
}
Copy the code
Query data where page = 1 and pageSize = 20
repository.findAll(PageRequest.of(1.20));
Copy the code
Jpa provides us with JpaRepository, an interface we can rely on directly when using Jpa for database operations.
@NoRepositoryBean
public interface JpaRepository<T.ID> extends PagingAndSortingRepository<T.ID>, QueryByExampleExecutor<T> {
List<T> findAll(a);
List<T> findAll(Sort var1);
List<T> findAllById(Iterable<ID> var1);
<S extends T> List<S> saveAll(Iterable<S> var1);
void flush(a);
<S extends T> S saveAndFlush(S var1);
void deleteInBatch(Iterable<T> var1);
void deleteAllInBatch(a);
T getOne(ID var1);
<S extends T> List<S> findAll(Example<S> var1);
<S extends T> List<S> findAll(Example<S> var1, Sort var2);
}
Copy the code
The actual operation
Define the Entity
Create s_user table
CREATE TABLE `s_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) NOT NULL COMMENT 'Username'.`password` varchar(50) NOT NULL COMMENT 'password'.`real_name` varchar(30) NOT NULL COMMENT 'User real name'.`mnemonic_code` varchar(30) DEFAULT NULL COMMENT 'mnemonic code'.`sex` varchar(30) DEFAULT NULL COMMENT 'Sex 0 male 1 female'.`created_by` int(11) DEFAULT NULL COMMENT 'Founder'.`created_time` datetime(6) DEFAULT NULL COMMENT 'Creation time'.`last_modified_by` int(11) DEFAULT NULL COMMENT 'Update person'.`last_modified_time` datetime(6) DEFAULT NULL COMMENT 'Update Time',
PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy the code
Entity
@Data
@Entity
@Table(name = "s_user")
public class SUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "user_name")
private String userName;
@Column(name = "pass_word")
private String passWord;
@Column(name = "real_name")
private String realName;
@Column(name = "mnemonic_code")
private String mnemonicCode;
@Column(name = "sex")
private String sex;
@Column(name = "created_by")
private Integer createdBy;
@Column(name = "created_time",updatable = false)
private Date createdTime;
@Column(name = "last_modified_by")
private Integer lastModifiedBy;
@Column(name = "last_modified_time")
private Date lastModifiedTime;
}
Copy the code
Commonly used annotations in Entity
@Id
Indicates that this field is the database primary key
@GeneratedValue
Indicates the primary key value generation policy
@Cloumn
Field notes
name
The field nameunique
Whether or not the onlynullable
Whether is emptylength
The length of thecolumnDefinition
Column definitions such as TEXT
Repository
@Repository
public interface UserRepository extends JpaRepository<SUser.Integer> {}Copy the code