个人对SpringJPA的理解为:通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。说通俗点就是,用纯面向对象语言取代SQL语句来进行数据库数据的处理。
例如:
1、创建一个Spring Boot 工程,在pom.xml里添加jpa依赖,jap和mysql
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2、创建两个实体类
public class CustomerEntity {
private Long id;
private String name;
private String source;
private String industry;
private String level;
private String address;
private String phone;
...省略get和set方法
}
public class LinkManEntity {
private Long id;
private String name;
private String gender;
private String phone;
private String mobile;
private String email;
private String position;
private String memo;
...省略get和set方法
}
3、添加注解,将两个实体类修改为两张表
@Entity:JPA注解,表示该类是一个实体
@Table(name = "customer"):JPA注解,表示实体对应的表,name指的是数据库里的表名。
@Id:用于声明一个实体类的属性映射为数据库的主键列(把该类里名为id的成员变量映射为表的主键列)
@GenerationType.IDENTITY:使用数据库自增键
@Column(name = "xxxx"):用于指定列的相关属性,name为列名
@Entity
@Table(name = "customer")
public class CustomerEntity
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name", length = 25)
private String name;
@Column(name = "source")
private String source;
@Column(name = "industry")
private String industry;
@Column(name = "level")
private String level;
@Column(name = "address")
private String address;
@Column(name = "phone")
private String phone;
...省略get和set方法...
}
@Entity
@Table(name = "link_man")
public class LinkManEntity
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name", length = 25)
private String name;
@Column(name = "gender")
private String gender;
@Column(name = "phone")
private String phone;
@Column(name = "mobile")
private String mobile;
@Column(name = "email")
private String email;
@Column(name = "position")
private String position;
@Column(name = "memo")
private String memo;
...省略get和set方法...
}
4、配置application.yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 5208282
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
format_sql: true
show-sql: true
hibernate:
ddl-auto: create
5、去项目入口运行项目
Hibernate:
drop table if exists customer
Hibernate:
drop table if exists link_man
Hibernate:
create table customer (
id bigint not null auto_increment,
address varchar(255),
industry varchar(255),
level varchar(255),
name varchar(25),
phone varchar(255),
source varchar(255),
primary key (id)
) engine=InnoDB
Hibernate:
create table link_man (
id bigint not null auto_increment,
email varchar(255),
gender varchar(255),
memo varchar(255),
mobile varchar(255),
name varchar(25),
phone varchar(255),
position varchar(255),
customer bigint,
primary key (id)
) engine=InnoDB
两张数据表已经被映射添加成功
6、让customer表对link_man表建立一对多的关系
修改CustomerEntity实体和LinkManEntity实体:
//将Link_Man与customer两张表产生外键关联
//一个客户有多个联系人而一个联系人只有一个特定的客户
//将下行代码添加至CustomerEntity类的成员变量后
//与目标建立一对多关系
@OneToMany(mappedBy = "customerEntity")
Set<LinkManEntity> linkManEntitySet;
//将下行代码添加至LinkManEntity类的成员变量后
//与目标建立多对一关系
//@JoinColumn:主外键,用来指定与所操作实体或实体集合相关联的数据库表中的列字段
@ManyToOne(targetEntity = CustomerEntity.class)
@JoinColumn(name = "customer", referencedColumnName = "id")
CustomerEntity customerEntity;
7、运行项目成功后
8、添加一个客户
创建一个CustomerRepository 接口
public interface CustomerRepostitory extends JpaRepository<CustomerEntity, Long>
{
}
添加客户测试代码
@SpringBootTest
public class CustomerRepositoryTest
{
@Autowired
CustomerRepostitory customerRepostitory;
@Test
public void save_customer_success()
{
CustomerEntity customerEntity = new CustomerEntity();
customerEntity.setAddress("成都");
customerEntity.setLevel("low");
customerEntity.setName("王仁杰");
customerEntity.setPhone("13795693962");
customerRepostitory.save(customerEntity);
Assertions.assertTrue(customerEntity.getId() != null);
}
}
测试结果
见数据表可知添加成功
个人总结:SpringJPA相比常规MyBatis所需求的处理工具或平台更少更集中更便捷,少量数据处理使用SpringJPA更方便,但若是需求处理的数据量很多,还是用MyBatis更加方便管理和理解 。