spring data jpa查询一个实体类的部分属性
使用Spring Data Repository查询时候,通常情况下返回的是一个实体所有的属性。但是在很多情况下,我们只需要实体类的部分属性。下面的部分讲述如何实现查询一个实体类的部分属性。
首先我们定义两个实体类
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package cn.net.leadu.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; /** * Created by PengChao on 2016/12/1. */ @Entity public class Person { @Id @GeneratedValue private Long id; private String firstName; private String lastName; @OneToOne private Address address; public Long getId() { return id; }
public void setId(Long id) { this .id = id; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this .firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this .lastName = lastName; }
public Address getAddress() { return address; }
public void setAddress(Address address) { this .address = address; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package cn.net.leadu.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;
/** * Created by PengChao on 2016/12/1. */ @Entity public class Address { @Id @GeneratedValue private Long id; private String street; private String state; private String country; public Long getId() { return id; }
public void setId(Long id) { this .id = id; }
public String getStreet() { return street; }
public void setStreet(String street) { this .street = street; }
public String getState() { return state; }
public void setState(String state) { this .state = state; }
public String getCountry() { return country; }
public void setCountry(String country) { this .country = country; } } |
然后创建person实体类的repository
|
1 2 3 4 5 6 7 8 9 10 |
package cn.net.leadu.dao; import cn.net.leadu.domain.Person; import org.springframework.data.jpa.repository.JpaRepository;
/** * Created by PengChao on 2016/12/1. */ public interface PersonRepository extends JpaRepository<Person, Long> { Person findByFirstName(String firstName); // 1 } |
"1"方法将会返回Person对象的所有属性(包括address)
但实际情况我们不需要返回address对象,这种情况我们可以定义一个projection,如下:
|
1 2 3 4 5 6 7 8 |
package cn.net.leadu.domain; /** * Created by PengChao on 2016/12/1. */ public interface NoAddress { String getFirstName(); //2 String getLasetName(); //3 } |
[2]输出person对象的firstName属性,[3]输出person对象的lastName属性
最后在Person实体类的repository中定义一个查询方法,返回值是NoAddress,如下
|
1 2 3 4 5 6 7 8 9 10 |
package cn.net.leadu.dao; import cn.net.leadu.domain.NoAddress; import cn.net.leadu.domain.Person; import org.springframework.data.jpa.repository.JpaRepository; /** * Created by PengChao on 2016/12/1. */ public interface PersonRepository extends JpaRepository<Person, Long> { NoAddress findByFirstName(String firstName); } |
返回结果只包含firstName和lastName两个属性
spring data jpa查询部分字段、多余附加字段
spring data jpa查询部分字段
第一种方法:使用 model 查询时转化
首先建立一个 model ,写上自己想要查询的字段,然后写上构造函数,这步很重要,因为spring jpa 转化时会调用这个构造方法
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class MyModel implements Serializable { private String userName; private String name; private String gender; private String description; public MyModel() {}; public MyModel(String userName, String name, String gender, String description) { this .userName = userName; this .name = name; this .gender = gender; this .description = description; } } |
然后在 dao 类中写查询方法
|
1 2 |
@Query (value = "select new pers.zhuch.model.MyModel(u.userName, ui.name, ui.gender, ui.description) from UserInfo ui, User u where u.id = ui.userId" ) public List<MyModel> getAllRecord(); |
直接在查询语句中 new model 框架底层会调用它,然后返回这个对象(这里我写了完整的类路径,不写的时候它报错说找不到类型什么的)
然后就可以获得只有指定字段的 model 了。然后就把它转成 JSON 格式就 O 了。
第二种方法:在service 里边转换成 JSON
原理其实和第一种方法差不多,只是处理结果的方式不太一样,只是这种方法我们就不在 hql 中 new Model 了,直接写查询方法
|
1 2 |
@Query (value = "select new map(u.userName, ui.name, ui.gender, ui.description) from UserInfo ui, User u where u.id = ui.userId" ) public List<Map<String, Object>> getCustomField(); |
直接new map(这里得是小写,不知道大写有木有问题,反正没试,编译器提示是要小写的)
然后返回的结果是这样的
[
{
"0": "admin",
"1": "你猜",
"2": "男",
"3": "一段描述"
}, {
"0": "abc",
"1": "你猜人家",
"2": "女",
"3": "没事先挂了"
}
]
注:多表联合查询附加额外字段,亦可使用此方法。例如
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@Query ( "select new map(u as user,'hello' as ext) from User u where u.id=?1" ) List<Map<String,Object>> findExtTest(Long id); @Query (value= "select u.*,'hello' as ext from user u where u.id=?1" ,nativeQuery = true ) List<Object> findExtNativeTest(Long id); List<Map<String,Object>> rows = userRepository.findExtTest(10001l); for (Map<String,Object> row :rows) { User user = (User) row.get( "user" ); String ext = (String) row.get( "ext" ); System.out.println(user); System.out.println(ext); } List<Object> objects = userRepository.findExtNativeTest(10001l); for (Object obj :objects) { Object[] row = (Object[])obj; Long id = ((BigInteger) row[ 0 ]).longValue(); String nickName = (String)row[ 17 ]; //数据库字段顺序 String ext = (String) row[row.length- 1 ]; System.out.println(id); System.out.println(nickName); System.out.println(ext); } |
第三种方法:select语句部分字段使用默认值
|
1 2 |
@Query (value = "select u.user_name, ui.name, ui.gender,ui.description,'' as headImg from user_info u where u.id = 1" ,nativeQuery= true ) public List<User> getAllRecord(); |
原文链接:https://blog.csdn.net/Peng__Chao/article/details/84885354
查看更多关于spring data jpa查询一个实体类的部分属性方式的详细内容...