Spring Data JPA的简单使用

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


Spring Data JPA的简单使用

  • ​​1、新建Spring Boot项目,引入对应的依赖​​
  • ​​2、根据数据库信息,添加yaml配置信息​​
  • ​​3、编写实体类,同时设置好数据库表对应的字段名字​​
  • ​​4、编写mapper​​
  • ​​5、编写测试类​​



​​Spring Data JPA官方地址​​

1、新建Spring Boot项目,引入对应的依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>



2、根据数据库信息,添加yaml配置信息

spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/jpa?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
driver-class-name: com.mysql.jdbc.Driver

jpa:
show-sql: true
hibernate:
ddl-auto: updatez



3、编写实体类,同时设置好数据库表对应的字段名字

@Table
@Entity(name = "user")
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@ToString
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Integer userId;

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

@Column(name = "class_name")
private String className;

@Column(name = "age")
private Integer age;

}



4、编写mapper

public interface UserMapper extends JpaRepository<User, Integer> {

List<User> findUsersByName(String name);

List<User> findByAgeAndName(Integer age, String name);
}



5、编写测试类

@SpringBootTest
class SpringbootjpaApplicationTests {

@Autowired
private UserMapper userMapper;

/**
* 使用jpa自带的方法,向数据库中添加数据
*/
@Test
void contextLoads() {

userMapper.save(buildUser("mobian", "初一", 13));
userMapper.save(buildUser("pan", "高一", 22));
userMapper.save(buildUser("wang", "初二", 15));
}

private static User buildUser(String name, String className, Integer age) {
User user = new User();
user.setClassName(className);
user.setName(name);
user.setAge(age);
return user;
}

/**
* 使用jpa自带的方法,查询所有的数据,再分页后返回
*/

@Test
void findAll() {
System.out.println(userMapper.findAll(PageRequest.of(1, 2)));
}

/**
* 使用jpa自带的方法,根据主键id查询数据库数据
*/
@Test
void findAllById() {
System.out.println(userMapper.findById(1));
}

/**
* 使用mapper中自定义的方法,不需要写sql,直接根据方法命名规则完成sql逻辑
*/
@Test
void findAllByName() {
System.out.println(userMapper.findUsersByName("mobian"));
}

/**
* 使用mapper中自定义的方法,不需要写sql,直接根据方法命名规则完成sql逻辑
*/
@Test
void findAllByAge() {
System.out.println(userMapper.findByAgeAndName(22, "pan"));
}
}



附JPA支持的方法换sql的表格:

Keyword

Sample

JPQL snippet

​Distinct​

​findDistinctByLastnameAndFirstname​

​select distinct … where x.lastname = ?1 and x.firstname = ?2​

​And​

​findByLastnameAndFirstname​

​… where x.lastname = ?1 and x.firstname = ?2​

​Or​

​findByLastnameOrFirstname​

​… where x.lastname = ?1 or x.firstname = ?2​

​Is​​​, ​​Equals​

​findByFirstname​​​,​​findByFirstnameIs​​​,​​findByFirstnameEquals​

​… where x.firstname = ?1​

​Between​

​findByStartDateBetween​

​… where x.startDate between ?1 and ?2​

​LessThan​

​findByAgeLessThan​

​… where x.age < ?1​

​LessThanEqual​

​findByAgeLessThanEqual​

​… where x.age <= ?1​

​GreaterThan​

​findByAgeGreaterThan​

​… where x.age > ?1​

​GreaterThanEqual​

​findByAgeGreaterThanEqual​

​… where x.age >= ?1​

​After​

​findByStartDateAfter​

​… where x.startDate > ?1​

​Before​

​findByStartDateBefore​

​… where x.startDate < ?1​

​IsNull​​​, ​​Null​

​findByAge(Is)Null​

​… where x.age is null​

​IsNotNull​​​, ​​NotNull​

​findByAge(Is)NotNull​

​… where x.age not null​

​Like​

​findByFirstnameLike​

​… where x.firstname like ?1​

​NotLike​

​findByFirstnameNotLike​

​… where x.firstname not like ?1​

​StartingWith​

​findByFirstnameStartingWith​

​… where x.firstname like ?1​​​ (parameter bound with appended ​​%​​)

​EndingWith​

​findByFirstnameEndingWith​

​… where x.firstname like ?1​​​ (parameter bound with prepended ​​%​​)

​Containing​

​findByFirstnameContaining​

​… where x.firstname like ?1​​​ (parameter bound wrapped in ​​%​​)

​OrderBy​

​findByAgeOrderByLastnameDesc​

​… where x.age = ?1 order by x.lastname desc​

​Not​

​findByLastnameNot​

​… where x.lastname <> ?1​

​In​

​findByAgeIn(Collection<Age> ages)​

​… where x.age in ?1​

​NotIn​

​findByAgeNotIn(Collection<Age> ages)​

​… where x.age not in ?1​

​True​

​findByActiveTrue()​

​… where x.active = true​

​False​

​findByActiveFalse()​

​… where x.active = false​

​IgnoreCase​

​findByFirstnameIgnoreCase​

​… where UPPER(x.firstname) = UPPER(?1)​


阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring