zoukankan      html  css  js  c++  java
  • spring data jap操作

    package com.example.demo;
    
    import com.example.entity.UserJ;
    import com.example.respository.UserJRespository;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Sort;
    import org.springframework.data.jpa.domain.Specification;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.persistence.EntityManager;
    import javax.persistence.criteria.CriteriaBuilder;
    import javax.persistence.criteria.CriteriaQuery;
    import javax.persistence.criteria.Predicate;
    import javax.persistence.criteria.Root;
    import java.util.ArrayList;
    import java.util.List;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestJpaCurd {
        @Autowired
        private UserJRespository userJRespository;
    
        @Autowired
        private EntityManager entityManager;
    
        /**
         * 插入或者是更新一条数据
         */
        @Test
        public void insertAndUpdate() {
            UserJ userJ = new UserJ();
            userJ.setUsername("alex222");
            userJ.setEmail("wangzhilei@jd.com");
            userJ.setId(1);
            userJRespository.save(userJ);
        }
    
        /**
         * 删除一条数据
         */
        @Test
        public void delete() {
            userJRespository.deleteById(1);
        }
    
        /**
         * 获取所有的数据
         *
         * @return
         */
        @Test
        public void getAll() {
            List<UserJ> all = userJRespository.findAll();
            for (UserJ userJ : all) {
                System.out.println(userJ);
            }
        }
    
        /**
         * 查询所有并+分页+排序
         * <p>
         * 从0页开始
         * 用最新的PageRequest.of来分页
         */
        @Test
        public void getOneData() {
            Sort sort = new Sort(Sort.Direction.DESC, "id");
            PageRequest pageRequest = PageRequest.of(0, 1, sort);
            /**
             * 使用匿名内部类
             * 构建查询条件
             * root 代表查询的对象
             * criteriaBuilder 条件封闭器,如and or like 等
             *  criteriaQuery 查询的条件
             */
            Specification<UserJ> specification = new Specification<UserJ>() {
                @Override
                public Predicate toPredicate(Root<UserJ> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                    // 单个条件查询
    //                Predicate predicate = cb.equal(root.get("email"), "sqyinchao@jd.com");
                    /**
                     * 多条件查询
                     */
                    List<Predicate> list = new ArrayList<>();
                    list.add(cb.equal(root.get("email"), "sqyinchao@jd.com"));
                    list.add(cb.equal(root.get("email"), "wangzhilei@jd.com"));
                    // 这里面放的是可变参数,则可以放一个数组进去,然后给拆分 相当于js中的...符号
                    Predicate[] predicates = new Predicate[list.size()];
                    return cb.or(list.toArray(predicates));
                }
            };
            Page<UserJ> all = userJRespository.findAll(specification, pageRequest);
            for (UserJ userJ : all) {
                System.out.println(userJ);
            }
        }
    }
    

      

  • 相关阅读:
    按钮组件如何处理点击事件(将原生事件绑定到自定义组件)
    一个简单的transition动画
    根据路由记录(利用matched)动态生成面包屑导航
    assets和static的异同
    Ceph集群概念以及部署
    腾讯质量效能提升最佳实践:智能自动化测试探索和建设
    腾讯WeTest压测大师通过中国计量科学研究院测试认证,获国家级权威认可
    新办公司每年费用
    2
    Leetcode刷题第三题 无重复字符的最长子串
  • 原文地址:https://www.cnblogs.com/leigepython/p/10141275.html
Copyright © 2011-2022 走看看