JPA

JPA概念

Java Persistence API:是一种规范(接口),这里我们使用hibernate去实现JPA

JPA使用步骤

1导包

在pox.xml中配置我们需要的东西

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>jpaartifactId>
        <groupId>cn.jiedadagroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>jpa_day01artifactId>
    <dependencies>
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
            <version>4.3.8.Finalversion>
        dependency>
        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-entitymanagerartifactId>
            <version>4.3.8.Finalversion>
        dependency>
        
        <dependency>
               <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.6version>
         dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>

    dependencies>

project>
View Code

 

2.设置配置文件

在资源文件中配置

JPA_第1张图片

 

 JPA_第2张图片

 

 制动生成这样的配置文件

现在开始配置我们连接数据库需要的东西

 

 在这个文件中有hibernate给我们写好的列子,直接在里面粘贴复制就可以了

xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="day01">
    <properties>
        
        <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>
        
        
        
        
        <property name="hibernate.show_sql" value="true">property>
        
        <property name="hibernate.format_sql" value="true">property>
    properties>
persistence-unit>
persistence>
View Code

 

 

实体类创建

//标记为实体类
@Entity
//表名
@Table(name = "t_user")
public class User {
//主键
@Id
//自增加
@GeneratedValue
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 + '\'' +
                '}';
    }
}
View Code

 

JPA连接CRUD

//获取工厂
factory = Persistence.createEntityManagerFactory("day01");
//获取连接对象
manager = factory.createEntityManager();
//开启事务
transaction = manager.getTransaction();
transaction.begin();
//设置语句
这里是需要我们写入的
//提交事务
transaction.commit();
//关闭流
manager.close();
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 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);
    }

}
View Code

 

你可能感兴趣的:(JPA)