1.BeanUtils基本用法:
java 代码
- package com.beanutil;
-
- import java.util.Map;
-
- public class User {
-
- private Integer id;
- private Map map;
- private String username;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public Map getMap() {
- return map;
- }
- public void setMap(Map map) {
- this.map = map;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
-
-
- }
java 代码
- public class Order {
- private User user;
- private Integer id;
- private String desc;
- public String getDesc() {
- return desc;
- }
- public void setDesc(String desc) {
- this.desc = desc;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
-
-
- }
java 代码
-
- import java.util.HashMap;
- import java.util.Map;
-
- import org.apache.commons.beanutils.BeanUtils;
-
- public class Test {
-
- private User user = new User();
- private Order order1 = new Order();
- private Order order2 = new Order();
- private Order order3 = new Order();
- private Map map = new HashMap();
- private User user1 = new User();
-
- public Test(){
- init();
- }
- public static void main(String[] args) throws Exception{
- Test test = new Test();
-
- System.out.println(BeanUtils.getProperty(test.user, "username"));
-
-
- System.out.println(BeanUtils.getProperty(test.order1, "user.username"));
-
-
-
- System.out.println(BeanUtils.getProperty(test.user1, "map(order2).desc"));
-
-
- User tempUser = new User();
- BeanUtils.copyProperties(tempUser, test.user1);
-
- System.out.println(tempUser.getUsername());
- System.out.println(tempUser.getId());
-
-
-
-
- }
-
-
- public void init(){
-
-
- user.setId(0);
- user.setUsername("zhangshan");
-
-
- order1.setId(1);
- order1.setDesc("order1");
- order1.setUser(user);
-
-
-
- order2.setId(2);
- order2.setDesc("order2");
- order2.setUser(user);
-
-
- order3.setId(3);
- order3.setDesc("order3");
- order3.setUser(user);
-
-
- map.put("order1", order1);
- map.put("order2", order2);
- map.put("order3", order3);
-
-
- user1.setId(1);
- user1.setUsername("lisi");
- user1.setMap(map);
-
-
- }
- }
输出结果为:
zhangshan
zhangshan
order2
lisi
1
2. BeanCompartor 动态排序
A:动态设定Bean按照哪个属性来排序,而不再需要再实现bean的Compare接口进行复杂的条件判断
java 代码
-
- List<order></order> list = new ArrayList<order></order>();
-
- list.add(test.order2);
- list.add(test.order1);
- list.add(test.order3);
-
-
- for(Order order : list){
- System.out.println(order.getId());
- }
-
- Collections.sort(list, new BeanComparator("id"));
- for(Order order : list){
- System.out.println(order.getId());
- }
B:支持多个属性的复合排序
java 代码
-
- List <beancomparator></beancomparator> sortFields = new ArrayList<beancomparator></beancomparator>();
- sortFields.add(new BeanComparator("id"));
- sortFields.add(new BeanComparator("desc"));
- ComparatorChain multiSort = new ComparatorChain(sortFields);
- Collections.sort(list, multiSort);
-
- for(Order order : list){
- System.out.println(order.getId());
- }
C:使用ComparatorUtils进一步指定排序条件
上面的排序遇到属性为null就会抛出异常, 也不能设定升序还是降序。
不过,可以借助commons-collections包的ComparatorUtils
BeanComparator,ComparableComparator和ComparatorChain都是实现了Comparator这个接口
java 代码
-
-
-
- Comparator mycmp = ComparableComparator.getInstance();
- mycmp = ComparatorUtils.nullLowComparator(mycmp);
- mycmp = ComparatorUtils.reversedComparator(mycmp);
- Comparator cmp = new BeanComparator("id", mycmp);
- Collections.sort(list, cmp);
- for(Order order : list){
- System.out.println(order.getId());
- }