如何优化java代码,springMVC写的.

package project001.admin.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import project001.admin.entity.WebInfo;
import project001.admin.impl.webInfoImpl;
import project001.admin.model.WebInfoModel;


@Controller
@RequestMapping("/admin/Config")
public class ConfigController {
    @RequestMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("/admin/config/index","command",new WebInfoModel());
    }
    @RequestMapping("/submit")
    public String submit(WebInfoModel webInfoModel){
        WebInfo webInfo = webInfoImpl.get(0);
        webInfo.setTitle(webInfoModel.getTitle());
        webInfo.setCopyright(webInfoModel.getCopyright());
        webInfo.setKeyword(webInfoModel.getKeyword());
        webInfo.setDescription(webInfoModel.getDescription());
        webInfoImpl.update(webInfo);
        return "redirect:index";
    }
}

public String submit方法里面的代码能优化下吗?
如何字段很多的话.那要写很多webInfo.set......那就工作量大了.
能不能通过枚举或者循环来做?

阅读 2.8k
2 个回答

构造函数,bean里面set值的时候返回对象本身等等

有如下三种解决方案:

1.在WebInfoModel中定义一个函数来设置这些参数(函数参数为需要设置的某些字段);
    package cn.lonecloud.pojo;

/**
 * Created by lonecloud on 2017/9/9.
 */
public class WebInfo {

    private String title;
    private String copyright;
    private String keyword;
    private String description;

    /**
     * 初始化webInfo
     * @param title
     * @param copyright
     * @param keyword
     * @param description
     */
    public void initWebInfo(String title, String copyright, String keyword, String description) {
        this.title = title;
        this.copyright = copyright;
        this.keyword = keyword;
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCopyright() {
        return copyright;
    }

    public void setCopyright(String copyright) {
        this.copyright = copyright;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
2.利用反射,将字段进行赋值

package cn.lonecloud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

/**
 * Created by lonecloud on 2017/9/9.
 */
public class ReflectCopyUtils {

static Logger logger = LoggerFactory.getLogger(ReflectCopyUtils.class);
//序列化id不拷贝
private static final String NOCOPYFIELD = "serialVersionUID"

/**
 * 拷贝
 * @param src 源
 * @param desc 目标
 * @throws IllegalAccessException
 */
public static void copyObject(Object src, Object desc) throws IllegalAccessException {
    //获取类类型
    Class srcClazz = src.getClass();
    Class descClazz = desc.getClass();
    //获取源对象的所有属性值
    Field[] fields = srcClazz.getDeclaredFields();
    //循环属性值
    for (Field field : fields) {

        String fieldName = field.getName();
        if (NOCOPYFIELD.equals(fieldName)) {
            continue;
        }
        //设置属性值可以访问
        field.setAccessible(true);
        Object value = field.get(src);
        try {
            Field descField = descClazz.getDeclaredField(fieldName);
            descField.setAccessible(true);
            descField.set(desc, value);
        } catch (NoSuchFieldException e) {
            logger.debug("desc{} filed {} is not found", descClazz, fieldName);
        }
    }
}
}

3.使用Spring的BeanUtils工具类进行Bean拷贝

package cn.lonecloud;

/**
 * Created by lonecloud on 2017/9/9.
 */
public class BeanUtils {
/**
 * 拷贝
 *
 * @Description:
 * @param src 源对象
 * @param target 目标对象
 * @param excludeFields 不要复制的属性
 */
public static void copyPropertiesIgnoreNull(Object src, Object target,String... excludeFields) {
    org.springframework.beans.BeanUtils.copyProperties(src, target,excludeFields);
}
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题