• spring data jpa 代码实现 增删改查


    package cn.kgc.test;

    import cn.kgc.pojo.Customer;
    import org.junit.*;

    import javax.persistence.*;
    import java.util.List;

    public class CustomerTest {

    private EntityManager entityManager;

    @Before
    public void init(){
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
    entityManager=factory.createEntityManager();
    }
    //添加信息 persist
    @Test
    public void addFirst(){
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    for(int i=0;i<10;i++){
    Customer customer = new Customer();
    customer.setCstName("大神"+i);
    customer.setCstAddress("懒人中心"+i);
    customer.setCstLevel("vip");
    entityManager.persist(customer);
    }
    transaction.commit();
    entityManager.close();
    }

    //删除信息 remove
    @Test
    public void deleteTest(){
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    Customer customer = entityManager.find(Customer.class, 2l);
    entityManager.remove(customer);
    transaction.commit();
    entityManager.close();
    }

    //修改信息 merge
    @Test
    public void updateTest(){
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    Customer customer = entityManager.find(Customer.class, 3l);
    customer.setCstName("小小");
    customer.setCstAddress("北京");
    customer.setCstLevel("svip");
    entityManager.merge(customer);
    transaction.commit();
    entityManager.close();
    }

    //根据ID查询 find getReference
    @Test
    public void findByIdTest(){
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    Customer customer = entityManager.find(Customer.class, 3l);
    System.out.println(customer);
    transaction.commit();
    entityManager.close();
    }
    @Test
    public void findByIdTest2(){
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    Customer customer = entityManager.getReference(Customer.class, 3l);
    System.out.println(customer);
    transaction.commit();
    entityManager.close();
    }

    //查询所有
    @Test
    public void findAllTest(){
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    Query query = entityManager.createQuery("from Customer");
    List<Customer> resultList = query.getResultList();
    for(Customer customer:resultList){
    System.out.println(customer);
    }
    entityManager.close();
    }

    //分页查询
    @Test
    public void findPageTest(){
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    Query query = entityManager.createQuery("from Customer");
    query.setFirstResult(0);
    query.setMaxResults(5);
    List<Customer> resultList = query.getResultList();
    for(Customer customer:resultList){
    System.out.println(customer);
    }
    entityManager.close();
    }

    //jpql按ID查询
    @Test
    public void jpqlFindByIdTest(){
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    Query query = entityManager.createQuery("from Customer where cstId=?");
    query.setParameter(1,3l);
    Customer singleResult =(Customer) query.getSingleResult();
    System.out.println(singleResult);
    entityManager.close();
    }

    //模糊查询
    @Test
    public void jpqlLikeFind(){
    Query query = entityManager.createQuery("from Customer where cstName like ? and cstAddress like ?");
    query.setParameter(1,"%神%");
    query.setParameter(2,"%中%");
    List<Customer> resultList = query.getResultList();
    for(Customer customer:resultList){
    System.out.println(customer);
    }
    entityManager.close();
    }

    //排序
    @Test
    public void findByOrder(){
    Query query = entityManager.createQuery("from Customer order by cstId asc");
    List<Customer> resultList = query.getResultList();
    for(Customer customer:resultList){
    System.out.println(customer);
    }
    entityManager.close();
    }

    //函数
    @Test
    public void mathTest(){
    Query query = entityManager.createQuery("select count(*) from Customer");
    Object singleResult = query.getSingleResult();
    System.out.println(singleResult);
    entityManager.close();
    }
    }
  • 相关阅读:
    HTML课堂笔记
    pycrul使用
    计算机网络概述
    重温冒泡排序
    初识MySQL
    宝塔Linux面板安装教程
    运维和shell
    nginx学习总结
    docker学习汇总
    linux 安装redis 完整步骤
  • 原文地址:https://www.cnblogs.com/geng-geng1997/p/11359172.html
Copyright © 2020-2023  润新知