SpringDataJPA是Spring基于ORM框架,JPA规范的基础上封装的一套JPA应用框架。
1.首先创建一个maven环境,并导入坐标
pom.xml文件的导入
5.2.7.RELEASE
5.4.10.Final
1.6.6
1.2.12
0.9.1.2
5.1.6
junit
junit
4.12
test
org.aspectj
aspectjweaver
1.6.8
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-core
${spring.version}
org.hibernate
hibernate-core
${hibernate.version}
org.hibernate
hibernate-entitymanager
${hibernate.version}
org.hibernate
hibernate-validator
5.2.1.Final
c3p0
c3p0
${c3p0.version}
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
mysql
mysql-connector-java
${mysql.version}
org.springframework.data
spring-data-jpa
1.9.0.RELEASE
org.springframework
spring-test
5.2.7.RELEASE
javax.el
javax.el-api
2.2.4
org.glassfish.web
javax.el
2.2.4
然后是在resource文件夹下创建一个applicationContext.xml
然后在对应的com.w.domain下面创建实体类
package com.w.domain;
import javax.persistence.*;
@Entity
@Table(name="cst_customer")
public class Customer {
//主键
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="cust_id")
private Long cId;
//客户名称
@Column(name="cust_name")
private String cName;
//客户来源
@Column(name="cust_source")
private String cSource;
//所属行业
@Column(name="cust_industry")
private String cIndustry;
//等级
@Column(name="cust_level")
private String cLevel;
//地址
@Column(name="cust_address")
private String cAddr;
//电话
@Column(name="cust_phone")
private String cPhone;
public Long getcId() {
return cId;
}
public void setcId(Long cId) {
this.cId = cId;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public String getcSource() {
return cSource;
}
public void setcSource(String cSource) {
this.cSource = cSource;
}
public String getcIndustry() {
return cIndustry;
}
public void setcIndustry(String cIndustry) {
this.cIndustry = cIndustry;
}
public String getcLevel() {
return cLevel;
}
public void setcLevel(String cLevel) {
this.cLevel = cLevel;
}
public String getcAddr() {
return cAddr;
}
public void setcAddr(String cAddr) {
this.cAddr = cAddr;
}
public String getcPhone() {
return cPhone;
}
public void setcPhone(String cPhone) {
this.cPhone = cPhone;
}
@Override
public String toString() {
return "Customer{" +
"cId=" + cId +
", cName='" + cName + '\'' +
", cSource='" + cSource + '\'' +
", cIndustry='" + cIndustry + '\'' +
", cLevel='" + cLevel + '\'' +
", cAddr='" + cAddr + '\'' +
", cPhone='" + cPhone + '\'' +
'}';
}
}
以及实体类对应的sql,在我的上一章节搭建JPA项目中有
接着就是创建对应的Dao接口,需要继承
JpaRepository, JpaSpecificationExecutor 两个接口
package com.w.dao;
import com.w.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface CustomDao extends JpaRepository, JpaSpecificationExecutor {
@Query(value = "from Customer where cId = ?2 and cName = ?1",nativeQuery = false)
Customer getCustomerByNameAndId(String name,long id);
@Query("update Customer set cName=?2 where cId=?1")
@Modifying
void updateCustomerById(long id,String name);
Customer findByCNameLikeAndCAddr(String cName, String cAddr);
Customer findByCName(String cName);
}
接着编写测试类
package com.w.test;
import com.w.dao.CustomDao;
import com.w.domain.Customer;
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.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestCRUD {
@Autowired
private CustomDao customDao;
@Test
public void testFindAll() {
List customers = customDao.findAll();
System.out.println(customers);
}
@Test
public void testFindOne() {
Customer customer = customDao.findOne(1L);
System.out.println(customer);
}
@Test
public void testSave() {
Customer customer =new Customer();
customer.setcName("张三小疯子");
customer.setcAddr("上海");
customDao.save(customer);
}
@Test
public void testDelete() {
customDao.delete(4L);
}
@Test
public void testCount(){
System.out.println(customDao.count());
}
@Test
public void testExist(){
//查看是否存在该对象
System.out.println(customDao.exists(4L));
}
}
这里面都是一些springData里面有的方法
如果使用JPQL语句进行查询
则还需要添加对应的方法在dao接口中,我已经在里面定义完了,可以照着操作
package com.w.test;
import com.w.dao.CustomDao;
import com.w.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestJPQL {
@Autowired
private CustomDao customDao;
@Test
public void testFindByNameAndId(){
System.out.println(customDao.getCustomerByNameAndId("张三",1L));
}
@Test
@Transactional
@Rollback(value = false)
public void testUpdate(){
customDao.updateCustomerById(2,"小赵");
}
}
最后是根据命名规范进行查找
package com.w.test;
import com.w.dao.CustomDao;
import com.w.domain.Customer;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestOthers {
@Autowired
private CustomDao customDao;
@Test
public void test1(){
Customer customer = customDao.findByCNameLikeAndCAddr("%张%","上海");
//Customer customer = customDao.findByCName("%张%");
System.out.println(customer);
}
}