目录
1.创建项目,导入依赖:pom.xml
2.配置Spring核心配置文件:ApplicationContext.xml
3.创建实体:
4.Repository接口
5.Repository接口中方法定义约定
6.@Query注解自定义SQL
7.CrudRepository接口
8.PagingAndSortingRepository接口
4.0.0
SpringDataJPA_Demo01
SpringDataJPA_Demo01
0.0.1-SNAPSHOT
jar
SpringDataJPA_Demo01
http://maven.apache.org
UTF-8
junit
junit
4.10
test
mysql
mysql-connector-java
5.1.38
org.springframework
spring-context
4.3.5.RELEASE
org.springframework
spring-jdbc
4.3.5.RELEASE
org.springframework.data
spring-data-jpa
1.8.0.RELEASE
org.hibernate
hibernate-entitymanager
4.3.6.Final
org.hibernate.cfg.ImprovedNamingStrategy
org.hibernate.dialect.MySQL5InnoDBDialect
true
true
update
在mysql建表
create database demodb;
use demodb;
添加注解——
@Entity 声明当前bean映射称为一个Entity实体,Entity实体会对应数据库中的表
@Id 当前属性是当前实体的Id属性
@GeneratedValue 指定该属性自增
@Column 指定列的名字和长度
package com.lj.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity(name="stu")
public class Student {
private int id;
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="username",length=20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
SpringDataJPA的核心接口是Repository,可以通过自定义接口继承该接口,在其中定义方法实现对实体的管理
可以在该接口的实现类中定义方法来实现对实体的管理,这些方法的名称是基于约定而来的,按照约定定义方法,SpringData将会按照约定操作表。
package com.lj.repository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import com.lj.domain.Student;
public interface StudentRepository extends Repository {
// 可以通过将nativeQuery属性设置为true来表明之后的value中是原生的sql语句
@Query(nativeQuery=true,value="select * from stu where id between ?1 and ?2")
public List queryBetween2(int begin,int end);
//可以通过?n的方式在JPA Query语句中引用方法中的参数
@Query("select o from stu o where o.id between ?1 and ?2")
public List queryBetween(int begin,int end);
//此处注意,写的不是sql,而是JPA Query语句,操作的不是数据库中的表,而是实体,所以不要写表名,而是写实体名称
@Query("select count(1) from stu o ")
public Integer getCount();
public List findByAgeBetweenAndNameLikeAndIdGreaterThan(int begin, int end, String nameLike, int id);
public List findByAgeBetween(int begin, int end);
public Student findByNameAndAge(String name, int age);
public Student findByName(String name);
public void exists(Integer id);
public void delete(Integer id);
public void save(Student student);
}
进行测试看:
package com.lj.test;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lj.domain.Student;
import com.lj.repository.StudentRepository;
public class Test01 {
/**
* 查询
*/
@Test
public void test11(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
List list = repository.queryBetween2(2, 4);
System.out.println(list);
}
/**
* 查询
*/
@Test
public void test10(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
List list = repository.queryBetween(2, 4);
System.out.println(list);
}
@Test
public void test09(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
int count = repository.getCount();
System.out.println(count);
}
/**
* 查询
*/
@Test
public void test08(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
List list = repository.findByAgeBetweenAndNameLikeAndIdGreaterThan(20, 30, "z%", 3);
System.out.println(list);
}
/**
* 查询
*/
@Test
public void test07(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
List list = repository.findByAgeBetween(20, 30);
System.out.println(list);
}
/**
* 查询
*/
@Test
public void test06(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
Student stu = repository.findByNameAndAge("zl",33);
System.out.println(stu);
}
/**
* 查询
*/
@Test
public void test05(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
Student stu = repository.findByName("ww");
System.out.println(stu);
}
/**
* 删除
*/
@Test
public void test04(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
repository.delete(1);
}
/**
* 修改
*/
@Test
public void test03(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
Student stu = new Student(1,"zsf",29);
repository.save(stu);
}
/**
* 新增
*/
@Test
public void test02(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取StudentRepository
StudentRepository repository = context.getBean(StudentRepository.class);
//3.调用方法
Student stu = new Student(0,"zs",19);
repository.save(stu);
}
/**
* 初始化容器时创建表
*/
@Test
public void test01(){
//1.初始化Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
查询操作:查询以findBy开头,后跟查询条件,多个查询条件之间可以通过and或or连接
可以在Repository中的方法上使用@Query注解直接声明该方法要执行的操作语句,则此时调用该方法会直接执行指定SQL,而不参照默认方法名对应语句规则。
自带了增删改查相关方法的Repository接口的子接口
public interface StudentRepository2 extends CrudRepository {
}
可以附带分页和排序效果的Repository接口的子接口
public interface StudentRepository3 extends PagingAndSortingRepository {
}