SpringBoot Data Jpa 使用環境搭建(一)

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", "[email protected]", "asddsfd",date));
     userRepository.save(new User("dfasdfasdf",14, "123456", "[email protected]", "dfasdf",date));
     userRepository.save(new User("csdfsadfc3",15, "123456", "[email protected]", "fdfdfdf",date));

     User user1 = userRepository.findByUserName("dfas");
     User user2 = userRepository.findByEmail("[email protected]");
     System.out.println(user1);
     System.out.println(user2);
 }

總結

今天才開始使用 jpa,遇到一個坑就是Dao層的接口自定義Sql總是報錯,原來@Query裏面的命令默認是Hql,如果要切換成Sql要使用 nativeQuery = true
本博客記錄了SpringBoot Data Jpa 的簡單使用,接下來應該着力學習一些 jpa 的高級用法~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章