准备
本人写的spring data是通过maven子父工程管理
parent项目的 : pom.xml
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-data</artifactId>
<groupId>com.ronnie</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-jpa</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
propertise
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://ip/jpa?characterEncoding=utf-8
username: root
password: pass
jpa:
hibernate:
ddl-auto: update
show-sql: true
#不加这句则默认为myisam引擎
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
注意注释 !!!
默认为myisam引擎 boot2.x 的特性
一、 实体类
1.Member
package com.ronnie.data.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
/**
* @Description:
* @Author: rongyu
* @CreateDate: 2018/8/31$ 10:06$
* @Remark:
*/
@Data
@Table
@Entity
public class Member implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
@JsonIgnore
private String password;
}
MemberOrder
如果用order请修改映射表名 order为数据库关键字 生成DDL 语法错误
package com.ronnie.data.entity;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.util.Date;
/**
* @Description:
* @Author: rongyu
* @CreateDate: 2018/9/1$ 15:33$
* @Remark:
*/
@Data
@Table
@Entity
public class MemberOrder {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String goodsName;
private Integer number;
@JoinColumn(name = "member_id")
@ManyToOne
private Member member;
@CreationTimestamp
private Date creationTime;
}
Dao
继承JpaRepository 就可以直接使用接口里面的方法
package com.ronnie.data.repository;
import com.ronnie.data.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
* @Description:
* @Author: rongyu
* @CreateDate: 2018/8/31$ 10:12$
* @Remark:
*/
public interface MemberDao extends JpaRepository<Member,Long>{
}
查询
package com.ronnie.data;
import com.ronnie.data.entity.Member;
import com.ronnie.data.entity.MemberOrder;
import com.ronnie.data.repository.MemberDao;
import com.ronnie.data.repository.MemberOrderDao;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
MemberDao memberDao;
@Autowired
MemberOrderDao orderDao;
@Test
public void addTestData() throws InterruptedException {
// Member member = memberDao.getOne(1L); bad way
Optional<Member> optionalMember = memberDao.findById(1L);
Member member = optionalMember.get();
for (int i = 0; i < 12; i++) {
MemberOrder order = new MemberOrder();
order.setGoodsName("coco");
order.setMember(member);
order.setNumber(10);
Thread.sleep(1000);
orderDao.save(order);
}
}
@Test
public void queryDemo() {
// add condition
MemberOrder order = new MemberOrder();
Member member = new Member();
member.setUsername("root");
order.setMember(member);
Example<MemberOrder> example = Example.of(order);
// add order by
Sort.Order order1 = new Sort.Order(Sort.Direction.DESC, "id");
Sort.Order order2 = new Sort.Order(Sort.Direction.ASC, "number");
Sort sort = Sort.by(order1, order2);
// add pageable
PageRequest pageRequest = PageRequest.of(0, 10, sort);
// findAll with page
Page<MemberOrder> result = orderDao.findAll(example, pageRequest);
log.info("result={}", result);
}
}
jpaJpaRepository 接口之中所有方法,接受的都是分页查询过程中的对象,直接调用即可
github https://github.com/ronnierry/spring-boot-data/tree/master/spring-data-jpa
QQ群 : 869658872