1.pom.xml新增 spring-boot-starter-data-jpa 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 資料庫連線池採用阿里druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>com.googlecode.log4jdbc</groupId>
<artifactId>log4jdbc</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
複製程式碼
2.application.yml配置
spring:
datasource:
driver-class-name: net.sf.log4jdbc.DriverSpy
url: jdbc:log4jdbc:mysql://localhost:3306/datebasename?createDatabaseIfNotExist=true&autoReconnect=true&useUnicode=true&characterEncoding=utf-8
username: root
password: root
initialSize: 5
minIdle: 5
maxActive: 2000
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: true
testOnReturn: true
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,slf4j
connectionProperties=druid.stat.mergeSql: true;druid.stat.slowSqlMillis=5000
type: com.alibaba.druid.pool.DruidDataSource
useGlobalDataSourceStat: true
jpa:
database: mysql
#配置自動建表:updata:沒有表新建,有表更新操作,控制檯顯示建表語句
hibernate:
ddl-auto: update
show-sql: true
複製程式碼
3.BaseEntity類和實體類
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* @Description: TODO(base-entity)
* @Author: 愛飄de小子 15:54
* @Date: 2018年08月03日 15點54分
*/
@Data
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = -4505117821220216969L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id", nullable = false, columnDefinition = "BIGINT UNSIGNED")
private Long id;
@Column(name = "create_time", columnDefinition = "datetime COMMENT '建立時間'")
private Date createTime;
@Column(name = "update_time", columnDefinition = "datetime COMMENT '修改時間'")
private Date updateTime;
/**
* 資料插入前的操作
*/
@PrePersist
public void setInsertBefore() {
this.createTime = new Date();
this.updateTime = new Date();
}
/**
* 資料修改前的操作
*/
@PreUpdate
public void setUpdateBefore() {
this.updateTime = new Date();
}
}
複製程式碼
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import java.io.Serializable;
/**
* 普通實體類
* @Author: 愛飄de小子 10:43
* @Date: 2018年09月10日 10點43分
*/
@Data
@Entity
//列orderNo新增索引
@Table(name="NoticeHisResult",indexes = {@Index(columnList = "orderNo")})
public class Notice extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1620243840780887725L;
@Column(name = "orderNo",nullable = false,columnDefinition = "varchar(50) COMMENT '訂單號'")
private String orderNo;
@Column(name = "notice_status", columnDefinition = "int(10) COMMENT '通知狀態: 1-成功 0-失敗'")
private Integer noticeStatus;
@Column(name = "notice_desc", columnDefinition = "varchar(500) COMMENT '描述'")
private String noticeDesc;
@Column(name = "resultText", columnDefinition = "text COMMENT '返回結果'")
private String resultText;
}
複製程式碼
注: 使用了lombok的@Data註解自動生成get和set方法
4.持久層介面
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
* @Author: 愛飄de小子 10:48
* @Date: 2018年09月10日 10點48分
*/
@Repository
public interface NoticeRepository extends PagingAndSortingRepository<Notice, Long>, JpaSpecificationExecutor<Notice> {
/**
* hql
*/
@Query(value = " from Notice n where n.orderNo = :orderNo ")
NoticeHisResult findByorderNo(@Param("orderNo") String orderNo);
/**
* sql
*/
@Query(value = " select * from notice n where n.orderNo = :orderNo ",nativeQuery = true)
NoticeHisResult findByorderNo(@Param("orderNo") String orderNo);
}
複製程式碼
注: 1.繼承PagingAndSortingRepository 實現分頁和排序 和基礎增刪改查 2.繼承JpaSpecificationExecutor 操作複雜sql或者動態sql(上篇文件有具體詳解)
5.關於jpa需要注意的地方
1.原生sql更新操作:
/**
* 原生SQL實現更新方法介面
*/
@Modifying
@Query(value = "update notice set notice_desc = :name where id = :id ", nativeQuery = true)
void updateOne(String noticeDesc,int id);
複製程式碼
呼叫updateOne介面的方法上需要加上 @Transactional註解