spring-data-jpa

//  导入坐标  这里的spring-orm 是spring 对orm框架的支持


// 配置  配置文件    


//  实体类用jpa 注解  (参照案例文件)


//  dao层接口   要实现JpaRepository<Customer,Long> ,JpaSpecificationExecutor<Customer>   
//(如不自定义方法,什么都不用写)接口已经定义好了
//        JpaRepository和JpaSpecificationExecutor要指定对应的映射实体类    
//        Long是主键 对应实体类
//      JpaSpecificationExecutor主要是实现多条件查询

******增删改查 案例
    
**************已经定义好的方法
   @Autowired
    private CustomerDao customerDao;
    
    //根据id查询 (立即加载)
    Customer customer = customerDao.findOne(4l);
    
    //根据id查询 (延迟加载)
    Customer customer = customerDao.getOne(4l);
    
    //根据所有 
    List<Customer> list = customerDao.findAll();
    
    
    
    
    //save : 保存或者更新
     *      根据传递的对象是否存在主键id,
     *      如果没有id主键属性:保存
     *      存在id主键属性,根据id查询数据,更新数据
     Customer customer  = new Customer();
        customer.setCustName("黑马程序员");
        customer.setCustLevel("vip");
        customer.setCustIndustry("it教育");
        customerDao.save(customer);
    
    //删除
     customerDao.delete(3l);
     
     
     //查询全部的客户数量
      long count = customerDao.count();
      
      
      //查询该id的数据是否存在
     boolean exists = customerDao.exists(4l);
     

*********条件查询
        
        要在 dao 接口中 定义抽象方法---有两种方式
        
    案例:
        按命名规范命名:(命名严格要求)
        
     * 方法名的约定:
     *      findBy : 查询
     *            对象中的属性名(首字母大写) : 查询的条件
     *            CustName
     *            * 默认情况 : 使用 等于的方式查询
     *                  特殊的查询方式
     *
     *  findByCustName   --   根据客户名称查询
     *
     *  再springdataJpa的运行阶段
     *          会根据方法名称进行解析  findBy    from  xxx(实体类)
     *                                      属性名称      where  custName =
     *
     *      1.findBy  + 属性名称 (根据属性名称进行完成匹配的查询=)
     *      2.findBy  + 属性名称 + “查询方式(Like | isnull)”
     *          findByCustNameLike
     *      3.多条件查询
     *          findBy + 属性名 + “查询方式”   + “多条件的连接符(and|or)”  + 属性名 + “查询方式”
     */
        public Customer findByCustName(String custName);


        public List<Customer> findByCustNameLike(String custName);

    //使用客户名称模糊匹配和客户所属行业精准匹配的查询
        public Customer findByCustNameLikeAndCustIndustry(String custName,String custIndustry);
     
    案例:
        用@Query :(命名无严格要求)
        
         @Query(value="from Customer where custName = ?")
            public Customer findJpql(String custName);
        
        
        @Query(value = "from Customer where custName = ?2 and custId = ?1")
            public Customer findCustNameAndId(Long id,String name);
            
            
        @Query(value = " update Customer set custName = ?2 where custId = ?1 ")
        @Modifying //*当前执行的是一个更新操作
        public void updateCustomer(long custId,String custName);
        
        
        *****细节nativeQuery = true  这里的sql语句是表的sql语句,cst_customer是表名
        
         @Query(value="select * from cst_customer where cust_name like ?1",nativeQuery = true)
            public List<Object [] > findSql(String name);
            
            
            
            
            
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值