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注解
网友评论