【spring boot】@Query、EntityManager、 JPA之RecoderRepository分页查询、新增、删除、更新

1.1首先:Dao接口继承JpaRepository接口

package com.xazysoft.workorder.repository;

import com.xazysoft.workorder.entities.Person;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PersonRepository extends JpaRepository<Person, Integer> {

}
1.2.  使用PersonRepository 接口的方法

1.2.1.  新增

 public void save(Person person) {  

           personDao.save(person);  
}  

 1.2.2.  删除

public void delete(Person person) {  

           personDao.delete(person);  
            
} 
public void delete(Integer id) {  

           personDao.delete(id);  
}  

 1.2.3.  修改

public void update(Person person) {  

           personDao.save(person);   
} 
1.2.4.  查询
public Person findById(Integer id) {  
       
    Person person = personDao.findOne(id);  
           return person;  
  }  
       
public Iterable<Person> findAll() {  

           Iterable<Person> persons = personDao.findAll();  
           return persons;  
 }

1.2.5.  分页查询

 public Iterable<Person> findAll(Pageable pageable) {  

           Page<Person> persons = personDao.findAll(pageable);  
           return persons;  
}
注:其中Pageable是一个接口,具体的实现类为PageRequest,下面为PageRequest的一个构造方
public PageRequest(int page, int size) {  

        this(page, size, null);  
} 

1.3.  源代码

IPersonService.java

    package com.morris.service.inter;  
    import org.springframework.data.domain.Pageable;  
    import com.morris.entity.Person;  
       
       
    public interface IPersonService {  
        void save(Person person);  
         
        void delete(Person person);  
         
        void delete(Integer id);  
         
        void update(Person person);  
         
        Person findById(Integer id);  
         
        Iterable<Person> findAll();  
         
        Iterable<Person> findAll(Pageable pageable);  
    }  
       

PersonServiceImpl.java
	
    package com.morris.service.impl;  
       
       
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.data.domain.Page;  
    import org.springframework.data.domain.Pageable;  
    import org.springframework.stereotype.Service;  
       
    import com.morris.dao.inter.PersonDao;  
    import com.morris.entity.Person;  
    import com.morris.service.inter.IPersonService;  
       
    @Service("personService")  
    public class PersonServiceImpl implements IPersonService {  
       
        @Autowired  
        private PersonDao personDao;  
         
        public void save(Person person) {  
       
           personDao.save(person);  
        }  
       
        public void delete(Person person) {  
           personDao.delete(person);  
            
        }  
       
        public void delete(Integer id) {  
           personDao.delete(id);  
        }  
       
        public void update(Person person) {  
           personDao.save(person);   
        }  
       
        public Person findById(Integer id) {  
       
           Person person = personDao.findOne(id);  
           return person;  
        }  
       
        public Iterable<Person> findAll() {  
       
           Iterable<Person> persons = personDao.findAll();  
           return persons;  
        }  
       
        public Iterable<Person> findAll(Pageable pageable) {  
           Page<Person> persons = personDao.findAll(pageable);  
           return persons;  
        }  
       
    }  

1.4.业务实现复杂复杂查询时JPA并不是很友好,建议使用注解 @Query、或者 EntityManager手写原生SQL加以实现

 1.4.1.@Query

package com.tysoft.Person.repository;

import com.tysoft.Person.entities.taskStrategy.UpGrade;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PersonRepository extends JpaRepository<UpGrade,Integer> {
   /*
    * nativeQuery = true,表示用原生的sql语句查询
    * ?1、?2表示第一个参数和第二个参数
   */
    @Query(nativeQuery = true, value = "SELECT * FROM personTable WHERE id = ?1 AND number = ?2")
    Integer getStudentByIdAndNumber(Integer Id, Integer number);
} 

1.4.2.使用EntityManager用StringBuilder声明sql对象,传递的参数用.append()进行拼接。

package com.xazysoft.workorder.service.impl;

import com.xazysoft.workorder.service.StatisticsService;
import com.xazysoft.workorder.utils.DateTimeUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class StatisticsServiceImp implements StatisticsService {

    /**
     * @PersistenceContext 注入的是实体管理器,执行持久化操作。
     * EntityManager 实体管理器
     */
    @PersistenceContext
    private EntityManager entityManager;
 @Override
    public BigInteger getAllNum(String building) {
        StringBuilder sql = new StringBuilder("SELECT COUNT(*) from tbl_Workorder_Work_Order WHERE (orderState!=214 OR distinction!=1103 OR distinction!=1104)");
        sql.append(" AND building IN").append(building);
        try {
            Query query = entityManager.createNativeQuery(sql.toString());
            List<BigInteger> list = query.getResultList();
            if (list.size() > 0 && list != null) {
                return list.get(0);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            entityManager.close();
        }
        return null;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值