关闭

Spring data jpa实现CRUD的三种方式

标签: Sping data jpaJpaRepositoryCRUD
583人阅读 评论(0) 收藏 举报
分类:

1. 继承JpaRepository

    JpaRepository继承PagingAndSortingRepository因此可以很好的实现分页和排序功能。

    

public interface JpaRepository<T, ID extends Serializable>
  extends CrudRepository<T, ID> {

  Iterable<T> findAll(Sort sort);

  Page<T> findAll(Pageable pageable);
}

2. 使用 jpql语句


public interface UserRepository extends JpaRepository<User, Long> {

  @Query("select u from User u where u.emailAddress = ?1")
  User findByEmailAddress(String emailAddress);
}


3. 使用实体处理器


package com.jike.usermanage.repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import com.jike.usermanage.model.Interest;
import com.jike.usermanage.model.User;

public class UserRepositoryImpl {

	@PersistenceContext
	private EntityManager entityManager;
	
	@Transactional
	public void add(User user) {
		entityManager.persist(user);

	}
	@Transactional
	public User update(User user) {
		User userUpdate = entityManager.find(User.class, user.getId());
		userUpdate.setName(user.getName());
		userUpdate.setPhone(user.getPhone());
		return userUpdate;
	}
	@Transactional
	public User addOrUpdate(User user) {
		
		return entityManager.merge(user);
	}
	@Transactional
	public void delete(User user) {
		entityManager.remove(user);

	}

	public User findOne(Integer id) {
		
		return entityManager.find(User.class, id);
	}
	@Transactional
	public List<User> findAll() {
		
		/*
		 * + , - , *, /
		 * 
		 * =, >, <, >=, <=, <>
		 * 
		 * between, like, in, is null, is empty, member of, not , and ,or
		 * 
		 * */

		String queryString = "select u from User u where :interest member of u.interests";
		Query query = entityManager.createQuery(queryString);
		Interest interest = new Interest();
		interest.setId(1L);
		query.setParameter("interest", interest);
		List list = query.getResultList();
		/*if (list!=null){
			List<User> userList = new ArrayList<User>();
			for(int i=0; i<list.size(); i++){
				
				Object[] o = (Object[]) list.get(i);
				User user = new User();
				user.setName(o[0].toString());
				user.setInCome(new BigDecimal(o[1].toString()));
				userList.add(user);
				
			}
			return userList;
			
		}else
			return null;*/
		return list;
		

		
	}
	
	@Transactional
	public List<Object> findObject(int uid) {
		
		/*
		 * 基本语法:select 实体属性  聚合函数(实体属性) from 实体 group by 实体属性 having 条件
		 * 
		 * avg(distinct...),sum(distinct...),count(distinct...),min,max
		 * 
		 * */
		
		
		String queryString = "select new com.jike.usermanage.model.User(u.id,u.name) from User u where u.id=:id";
		//Query query = entityManager.createQuery(queryString);
		Query query =entityManager.createNativeQuery("select id_,name_ from t_user where id_=1"); 
		query.setParameter("id", uid);
		List list = query.getResultList();
		return list;
		
		

		
	}	


}

相关文档参考:http://docs.spring.io/spring-data/jpa/docs/current/reference/html/





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27130次
    • 积分:638
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    最新评论