JPA概念
Java Persistence API:是一种规范(接口),这里我们使用hibernate去实现JPA
JPA使用步骤
1导包
在pox.xml中配置我们需要的东西
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>jpa</artifactId> <groupId>cn.jiedada</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>jpa_day01</artifactId> <dependencies> <!--配置JPA的核心配置--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.8.Final</version> </dependency> <!--配置JPA的实现类的包--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.8.Final</version> </dependency> <!--配置数据库驱动连接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!--junit测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
2.设置配置文件
在资源文件中配置
制动生成这样的配置文件
现在开始配置我们连接数据库需要的东西
在这个文件中有hibernate给我们写好的列子,直接在里面粘贴复制就可以了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="day01"> <properties> <!--配置4大金刚 #hibernate.dialect org.hibernate.dialect.MySQLDialect #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect #hibernate.connection.driver_class com.mysql.jdbc.Driver #hibernate.connection.url jdbc:mysql:///test #hibernate.connection.username gavin #hibernate.connection.password --> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"></property> <property name="hibernate.connection.url" value="jdbc:mysql:///jpa"></property> <property name="hibernate.connection.username" value="root"></property> <property name="hibernate.connection.password" value="123456"></property> <!--配置方言属性--> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property> <!--配置可选属性--> <!--配置制动生成键 #hibernate.hbm2ddl.auto create-drop #hibernate.hbm2ddl.auto create #hibernate.hbm2ddl.auto update #hibernate.hbm2ddl.auto validate --> <!--<property name="hibernate.hbm2ddl.auto" value="create"></property>--> <!--配置是否显示SQL #hibernate.show_sql true --> <property name="hibernate.show_sql" value="true"></property> <!--漂亮SQL hibernate.format_sql true --> <property name="hibernate.format_sql" value="true"></property> </properties> </persistence-unit> </persistence>
实体类创建
//标记为实体类
@Entity
//表名
@Table(name = "t_user")
public class User {
//主键
@Id
//自增加
@GeneratedValue
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.jiedada.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; //标记为实体类 @Entity //表名 @Table(name = "t_user") public class User { //主键 @Id //自增加 @GeneratedValue private Long id; private String name; public User() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
JPA连接CRUD
//获取工厂
factory = Persistence.createEntityManagerFactory("day01");
//获取连接对象
manager = factory.createEntityManager();
//开启事务
transaction = manager.getTransaction();
transaction.begin();
//设置语句
这里是需要我们写入的
//提交事务
transaction.commit();
//关闭流
manager.close();
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.jiedada.domain; import org.junit.After; import org.junit.Before; import org.junit.Test; import javax.persistence.*; import java.util.List; import static org.junit.Assert.*; public class UserTest { EntityManagerFactory factory; EntityManager manager; EntityTransaction transaction; @Before public void berfore() { //获取工厂 factory = Persistence.createEntityManagerFactory("day01"); //获取连接对象 manager = factory.createEntityManager(); //开启事务 transaction = manager.getTransaction(); transaction.begin(); //设置语句 } @After public void after() { //提交事务 transaction.commit(); //关闭流 manager.close(); } @Test public void testAdd() { User user = new User(); user.setName("杰帅"); //添加 manager.persist(user); } @Test public void testQuery() { //单个查询 User user = manager.find(User.class, 1l); System.out.println(user); } @Test public void testQueryAll() { //这里需要使用jpql面向对象的sql的语句 String jpql="select u from User u"; Query query = manager.createQuery(jpql); List<User> list = query.getResultList(); list.forEach(e->{ System.out.println(e); }); } @Test public void testDel() { //因为是面向对象的jpql所以删除的时候需要找打在删除 //找到要删除的对象 User user = manager.find(User.class, 1l); manager.remove(user); } @Test public void testUpdate() { //因为是面向对象的jpql所以删除的时候需要找打在删除 //找到要删除的对象 User user = manager.find(User.class, 2l); user.setName("小黄"); //这里我们先把这个方法看作修改,但是实际上不是修改 manager.merge(user); } }