【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 {

}
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 findAll() {  

           Iterable persons = personDao.findAll();  
           return persons;  
 }

1.2.5.  分页查询

 public Iterable findAll(Pageable pageable) {  

           Page 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 findAll();  
         
        Iterable 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 findAll() {  
       
           Iterable persons = personDao.findAll();  
           return persons;  
        }  
       
        public Iterable findAll(Pageable pageable) {  
           Page 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 {
   /*
    * 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 list = query.getResultList();
            if (list.size() > 0 && list != null) {
                return list.get(0);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            entityManager.close();
        }
        return null;
    }

}

你可能感兴趣的:(JPA,Query,EntityManager)