springDataJPA入门
一:JPA
1.1 jpa 是一个开发规范 是一个ORM框架的开发规范 是SUN 公司定义的 中文名:JAVA持久层API
orm : 对象关系映射 是一个解决问题的思路 对对象的操作就是对数据库的操作
二:什么是SpringDataJPA
Spring Data JPA框架主要针对的是Spring 唯一没有简化到业务逻辑代码 开发者连仅实现持久层业务逻辑的工作都省了,唯一要做的就是生明持久层的接口 其它的都交给Spring Data JPA来帮完成
并不是JPA规范实现 基于原生的jpa 的api 进行了再次的封装
如果要使用SpringDataJpa 还需要使用Hibernate
三:入门程序
1需求
向客户中插入一条数据
如果使用Jpa框架可以不用先建表 可以使用框架生成表
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.hibernate.version>5.0.7.Final</project.hibernate.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--hibernate 对jpa的支持--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${project.hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>${project.hibernate.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies>
b 创键一个配置文件
配置文件的路径必须是:META-INF/persistence.xml
配置连接数据库的相关配置
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <!--配置持久化单元 在配置文件中至少有一个 name 持久化单元的名称 transaction-type 事物类型 RESOURCE_LOCAL 单数据库的事物 JTA 分布式事物 跨数据的事物 多个数据库的事物 --> <persistence-unit name="myjpa" transaction-type="RESOURCE_LOCAL"> <properties> <property name="javax.persistence.jdbc.user" value="用户"/> <property name="javax.persistence.jdbc.password" value="数据库密码"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/数据库名"/> <!--配置hibernate的属性--> <property name="hibernate.show_sql" value="true"></property> <!--sql语句是否格式化--> <property name="hibernate.format_sql" value="true"/> <!--是否自动创建数据库表 value 可选值 create update none create 程序自动创建数据库表 如果表存在 先删除后创建 update 程序自动创建数据库表 如果表存在 不创建 none 不会创建 --> <property name="hibernate.hbm2ddl.auto" value="create"/> </properties> </persistence-unit> </persistence>
c: 创键一个Entry类 对应数据库中每个表创键一个实体类
(这一段直接复制会报错,也不知道为啥)
import javax.persistence.*;
@Entity // 该类是jpa的实体类 @Table(name="cust_customer") // 配置实体类和数据库表中映射关系 name 对应的表名
public class Customer { // 配置主键的生成策略 GenerationType.IDENTITY 自增长 @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id //配置属性和字段名之间关系 @Column(name="cust_id") private long custId;
@Column(name="cust_name") private String custName;
@Column(name="cust_source") private String custSource;
@Column(name="cust_industry") private String custIndustry;
@Column(name="cust_level") private String custLevel;
@Column(name="cust_address") private String custAddress;
@Column(name="cust_phone") private String custPhone; public long getCustId() { return custId; } public void setCustId(long custId) { this.custId = custId; } public String getCustNmae() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } }
d: 编写测试程序 实现数据的添加
1 创键一个EntityManagerFactory 对象 使用完关闭
2 使用工厂对象EntityManagerFactory 就是一个连接
3 开启事物
4 创键 Customer 对象
5 使用Entitymanager 对象 的persist 方法向数据库添加数据
6 事物提交
7 关闭连接
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaTest { @Test public void firstTest(){ // 1 创键一个EntityManagerFactory 对象 使用完关闭 EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa"); // 2 使用工厂对象EntityManagerFactory 就是一个连接 EntityManager entityManager = factory.createEntityManager(); // 3 开启事物 EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // 4 创键 Customer 对象 Customer customer = new Customer(); customer.setCustName("ruirui"); customer.setCustLevel("vip"); customer.setCustSource("网络"); customer.setCustPhone("123456"); customer.setCustAddress("懒人中心"); // 5 使用Entitymanager 对象 的persist 方法向数据库添加数据 entityManager.persist(customer); // 6 事物提交 transaction.commit(); // 7 关闭连接 entityManager.close(); factory.close();
如果错误欢迎大神们指出,谢谢!!!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步