2、Spring Data JPA CRUD实例 主结构: springdata→pom.xml :
4.0.0
com.tuling.springdata
springdata
pom
1.0-SNAPSHOT
01-jpa-hibernate
02-springdata-jpa
03-springdata-jpa
04-springdata-jpa
8
8
org.springframework.data
spring-data-bom
2021.1.0
import
pom
02-springdata-jpa→pom.xml :
springdata
com.tuling.springdata
1.0-SNAPSHOT
4.0.0
02-springdata-jpa
8
8
4.4.0
1.1.3
org.springframework.data
spring-data-jpa
junit
junit
4.13
test
org.hibernate
hibernate-entitymanager
5.4.32.Final
mysql
mysql-connector-java
5.1.22
com.alibaba
druid
1.2.8
org.springframework
spring-test
5.3.10
test
com.querydsl
querydsl-jpa
${querydsl.version}
com.mysema.maven
apt-maven-plugin
${apt.version}
com.querydsl
querydsl-apt
${querydsl.version}
generate-sources
process
target/generated-sources/queries
com.querydsl.apt.jpa.JPAAnnotationProcessor
true
pojo→Customer实体类:
package com.tuling.pojo;
import javax.persistence.*;
@Entity // 作为hibernate 实体类
@Table(name = "tb_customer") // 映射的表明
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long custId; //客户的主键
@Column(name = "cust_name")
private String custName;//客户名称
@Column(name="cust_address")
private String custAddress;//客户地址
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custAddress='" + custAddress + '\'' +
"}\n";
}
}
spring.xml 文件
CustomerRepository接口
package com.tuling.repositories;
import com.tuling.pojo.Customer;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;//这个接口可以实现分页和排序
import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
//CustomerRepository接口继承这个接口PagingAndSortingRepository,有分页排序等功能
// 这个相应测试类可见:SpringDataJpaPagingAndSortTest
public interface CustomerRepository extends PagingAndSortingRepository{
// 增删查改
// 查询
@Query("FROM Customer where custName=:custName ")
List findCustomerByCustName(@Param("custName") String custName);
// 修改
@Transactional
@Modifying // 通知springdatajpa 是增删改的 *** 作
@Query("UPDATE Customer c set c.custName=:custName where c.custId=:id")
int updateCustomer(@Param("custName") String custName,@Param("id")Long id);
@Transactional
@Modifying // 通知springdatajpa 是增删改的 *** 作
@Query("DELETE FROM Customer c where c.custId=?1")
int deleteCustomer(Long id);
// 新增 JPQL
@Transactional
@Modifying // 通知springdatajpa 是增删改的 *** 作
@Query("INSERT INTO Customer (custName) SELECT c.custName FROM Customer c where c.custId=?1")
int insertCustomerBySelect(Long id);
@Query(value="select * FROM tb_customer where cust_name=:custName "
,nativeQuery = true)
List findCustomerByCustNameBySql(@Param("custName") String custName);
}
SpringdataJpaTest测试
package com.tuling;
import com.tuling.config.SpringDataJPAConfig;
import com.tuling.pojo.Customer;
import com.tuling.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.Optional;
// 基于junit4 spring单元测试
@ContextConfiguration("/spring.xml") //spring.xml配置文件的方式,二选一
//@ContextConfiguration(classes = SpringDataJPAConfig.class) //配置类的方式,,二选一
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringdataJpaTest {
// jdk动态代理的实例
@Autowired
CustomerRepository repository;
//查询
@Test
public void testR(){
Optional byId = repository.findById(1L);//Optional这个是JDK8的新特性,用来防止空指针的
System.out.println(byId.orElse(null));
System.out.println("========");
System.out.println(byId.get());
}
//增
@Test
public void testC(){
Customer customer = new Customer();
customer.setCustName("赵");
System.out.println(repository.save(customer));
}
//更新、删除
@Test
public void testD(){
Customer customer = new Customer();
customer.setCustId(7L);
customer.setCustName("赵六");
//更新插入
repository.save(customer);
//删除
// repository.delete(customer);
}
//查询多个
@Test
public void testFindAll(){
Iterable allById = repository.findAllById(Arrays.asList(1L, 4L, 5L));
System.out.println(allById);
}
// 用来插入和修改 有主键就是修改 没有就是新增
// 获得插入后自增id, 获得返回值 S save(S entity);
// 通过集合保存多个实体 Iterable saveAll(Iterable entities);
// 通过主键查询实体 Optional findById(ID id);
// 通过主键查询是否存在 返回boolean 11 boolean existsById(ID id);
// 查询所有 Iterable findAll();
// 通过集合的主键 查询多个实体,, 返回集合 Iterable findAllById(Iterable ids);
// 查询总数量 long count();
// 根据id进行删除 void deleteById(ID id);
// 根据实体进行删除 void delete(T entity);
// 删除多个 void deleteAllById(Iterable extends ID> ids);
// 删除多个传入集合实体 void deleteAll(Iterable extends T> entities);
// 删除所有 void deleteAll();
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)