1、引入依赖(pom.xml)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2、数据库连接配置(application.yml)
spring:
datasource:
url: jdbc:mysql://localhost/test
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
#根据实体类自动创建表,如果创建过就不再重新创建
ddl-auto: update
show-sql: true
3、实体类
package com.example.demospringbootjpa.bean;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;
@Entity
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false, unique = true)
private String userName;
@Column
private Integer age;
@Column(nullable = false)
private String passWord;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = true, unique = true)
private String nickName;
@Column(nullable = false)
private Date regTime;
public User() {
}
public User(String userName, Integer age, String passWord, String email, String nickName, Date regTime) {
this.userName = userName;
this.age = age;
this.passWord = passWord;
this.email = email;
this.nickName = nickName;
this.regTime = regTime;
}
}
4、Dao层接口
package com.example.demospringbootjpa.repository;
import com.example.demospringbootjpa.bean.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface UserRepository extends JpaRepository<User, Long> {
//1、Query里面的查询不是Sql,是Hql,from 后面是Java实体名,查询条件字段如userName也是Java实体对象的属性!!!
@Query("select u from User u where u.userName = :userName")
User findByUserName(String userName);
//2、nativeQuery = true 使用原生的sql
@Query(value = "select * from user where email like concat('%',:email,'%') ",nativeQuery = true)
User findByEmail(String email);
}
5、测试查询
@Test
public void contextLoads() {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
userRepository.save(new User("dfas",12, "123456", "abc@163.com", "asddsfd",date));
userRepository.save(new User("dfasdfasdf",14, "123456", "dfdf@163.com", "dfasdf",date));
userRepository.save(new User("csdfsadfc3",15, "123456", "13265@163.com", "fdfdfdf",date));
User user1 = userRepository.findByUserName("dfas");
User user2 = userRepository.findByEmail("dfdf@163.com");
System.out.println(user1);
System.out.println(user2);
}
总结
今天才开始使用 jpa,遇到一个坑就是Dao层的接口自定义Sql总是报错,原来@Query里面的命令默认是Hql,如果要切换成Sql要使用 nativeQuery = true
。
本博客记录了SpringBoot Data Jpa 的简单使用,接下来应该着力学习一些 jpa 的高级用法~