Spring Data JPA (Oracle)基础使用

Spring Data JPA基础使用

学习了 尚硅谷的jpa在线视频教程, 结合其他资料后动手实践的成果。

 

1.环境搭建
Maven环境,在pom.xml中加入相关hibernate,spring,jdbc,slf4j-log4j12,json-lib,testng的jar包

2.Spring配置文件说明
配置文件放置位置,在classpath设定的目录下建个resources文件,放spring-config.xml(这个名字可以自己取)。
文件内容:
配置service自动扫描的包
配置数据源oracle
配置JPA的entityManagerFactory
配置事物管理器
配置支持注解的事物
配置springData

 










 

 





 



org.hibernate.cf.ImprovedNamingStrategy

org.hibernate.dialect.OracleDialect
true
true
update




   
  






 

3.使用示例
一个最简单的应用包含:
在数据库建表
数据表实例(根据数据库中的表定义)
接口定义( Repository ,CrudRepository ,JPA Repository,…)
应用调用
(看示例说明)

3.1数据库表实例

package com.springjpa.orclSpringJpa;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name="Jpa_Person")//需实例化的数据库表名称:Jpa_Person,将其与实体类JpaPerson映射关联
@Entity
public class JpaPerson {
@GeneratedValue(strategy=GenerationType.TABLE)
@Id
private Integer id;//id,name,age为表中的字段
private String name;
private Integer age;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
return "JpaPerson [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}

 

3.2.1普通接口定义

package com.springjpa.orclSpringJpa;

import org.springframework.data.repository.JpaRepository;

public interface IJpaPersonRepository extends JpaRepository {
//不需加任何内容,就可以通过IJpaPersonRepository调用默认的一些查询方法了
}

 

3.2.2应用调用

package com.springjpa.orclSpringJpaTest;

import java.sql.SQLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

import com.springjpa.orclSpringJpa.IJpaPersonRepository;
import com.springjpa.orclSpringJpa.JpaPerson;

public class PersonTest {
private ApplicationContext ctx =null;
private IJpaPersonRepository  jpaPersonRepository =null;
{
ctx = new ClassPathXmlApplicationContext("classpath:spring-config.xml");
jpaPersonRepository = ctx.getBean(IJpaPersonRepository.class);
}

@Test
public void testSpringData_saveJpaPerson(){
JpaPerson jpaPerson = new JpaPerson();
//jpaPerson.setId(2);
jpaPerson.setAge(2);
jpaPerson.setName("yyy");
jpaPersonRepository.save(jpaPerson);//save是JpaRepository中定义的方法
List jPersonList = jpaPersonRepository.findAll();//findAll也是JpaRepository中定义的方法,还有其他方法不举例了
System.out.println(jPersonList);
}

 

以上简单的Spring Data Jpa的应用方式完成了, 下面根据规则增加对数据库的操作方式:

(数据库表实例与3.1一样)

3.3.1关键字、注解方式接口定义

package com.springjpa.orclSpringJpa;

import java.util.List;
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.Repository;
import org.springframework.data.repository.query.Param;

public interface IJpaPersonRepository extends JpaRepository {
//关键字方法查询-简单条件查询,方法名定义需符合规则1.方法名2.连接词3.关键词4.属性名首字母大写
List getByName(String name);
List getByNameInAndAgeLessThan(List name,Integer age);

//宽注解的方式查询
//查询年龄最大的Person,FROM后面是类名不是Table名
@Query("SELECT p FROM JpaPerson p WHERE p.age = (SELECT max(p2.age) FROM JpaPerson p2)")
JpaPerson getMaxAgePerson(); 

//查询年龄为( ?占位符)的Person
@Query("SELECT p FROM JpaPerson p WHERE p.age = ?1 AND p.id  getPersonByAgeAndId(Integer age,Integer id);
//排序
//查询年龄,从大到小排列
@Query("SELECT p.age FROM JpaPerson p ORDER BY p.age DESC")
List getAgeOrderByDESC();


//原生的SQL查询,需设置nativeQuery = true
//Jpa_Person表名,非类名
@Query(value = "SELECT count(id) FROM Jpa_Person",nativeQuery = true)
long getTotalCount();
//------------------------
//自定义JQL可以完成UPDATE,DELETE,不支持INSERT(需要INSERT时可以采用nativeQuery = true,直接用原生的SQL方式)
//在Query中写JQL语句需用@Modifying修饰
//凡是对表中数据产生改动的,UPDATE,DELETE,INSERT操作都需要使用事务,要定义Service层
//默认情况SpringData每个方法都有事务,但都是一个只读事务,不能完成修改操作
@Modifying
@Query("UPDATE JpaPerson p SET p.name = :name WHERE p.id =:id")
void updateJpaPersonName(@Param("id")Integer id,@Param("name")String name);

@Modifying
@Query("DELETE FROM JpaPerson p WHERE p.age = :age")
void deleteJpaPersonAge(@Param("age")Integer age);
  @Modifying
 @Query(value = "INSERT INTO Jpa_Person(name,age) VALUES (?1,?2)", nativeQuery = true)
    void insert(String name, int age);

}

 

3.3.2 Service

package com.springjpa.orclSpringJpa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class JpaPersonService {
@Autowired
private IJpaPersonRepository jpaPersonRepository;

@Transactional
//Transactional标注下的方法可以实现多个操作
//对于事物Transactional标注下的函数如果有多个操作,如插入/修改/删除数据,只要有一条操作失败,整个事务就会回滚,成功的插入/修改/删除的数据会被恢复到这个事务开始前的状态
public void actionJpaPersonName(String name,Integer id,Integer age){
jpaPersonRepository.updateJpaPersonName(id, name);
jpaPersonRepository.deleteJpaPersonAge(age);
}
//update
@Transactional
public void updateJpaPersonName(String name,Integer id){
jpaPersonRepository.updateJpaPersonName(id, name);
}
//delete
@Transactional
public void deleteJpaPersonAge(Integer age){
 jpaPersonRepository.deleteJpaPersonAge(age);
}

//insert

 @Transactional
 public void insert(String name, int age) {
      jpaPersonRepository.insert(String name, int age);
    }


}

 

3.3.3应用调用

package com.springjpa.orclSpringJpaTest;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

import com.springjpa.orclSpringJpa.IJpaPersonRepository;
import com.springjpa.orclSpringJpa.JpaPerson;
import com.springjpa.orclSpringJpa.JpaPersonService;

public class PersonTest {
private ApplicationContext ctx =null;
private IJpaPersonRepository  jpaPersonRepository =null;
private JpaPersonService jpaPersonService =null;
{
ctx = new ClassPathXmlApplicationContext("classpath:spring-config.xml");
jpaPersonRepository = ctx.getBean(IJpaPersonRepository.class);
jpaPersonService = ctx.getBean(JpaPersonService.class);
}

@Test
public void testSpringData_getTotalCount(){
long count = jpaPersonRepository.getTotalCount();
System.out.println("记录数: " + count);
}

@Test
public void testSpringData_updateJpaPersonName(){
jpaPersonService.updateJpaPersonName("美丽传说", 1);
}

@Test
public void testSpringData_deleteJpaPersonAge(){
jpaPersonService.deleteJpaPersonAge(5);
}
}

 

你可能感兴趣的:(Spring,Data,JPA)