1. 程式人生 > >hibernate 自定義sql createSQLQuery多表join查詢查詢自定義vo物件

hibernate 自定義sql createSQLQuery多表join查詢查詢自定義vo物件

由於要做個left join所以要搞個vo物件

LogManageVo.java
裡面就寫屬性和get set方法就可以了
import com.sevnce.log.entity.LogManageDetails;



/**
 * Created by zhouzhongqing on 2017/3/16.
 */
public class LogManageVo extends LogManageDetails{


    /**
     * 操作人id
     * */
    private int usrUserId;

    /**
     * 操作人名稱
     * **/
    private String usrUserName;


    /**
     * 操作人動作,操作了哪個模組
     * **/

    private String whatAction;


    /**
     * 記錄的操作日誌是否為登入,0 登入 1 其他
     * **/
    private int isLogin;

    public int getUsrUserId() {
        return usrUserId;
    }

    public void setUsrUserId(int usrUserId) {
        this.usrUserId = usrUserId;
    }

    public String getUsrUserName() {
        return usrUserName;
    }

    public void setUsrUserName(String usrUserName) {
        this.usrUserName = usrUserName;
    }

    public String getWhatAction() {
        return whatAction;
    }

    public void setWhatAction(String whatAction) {
        this.whatAction = whatAction;
    }

    public int getIsLogin() {
        return isLogin;
    }

    public void setIsLogin(int isLogin) {
        this.isLogin = isLogin;
    }

  String sql = "SELECT lmd.*,lm.usrUserId,lm.usrUserName,lm.whatAction,lm.isLogin from log_manage_details as lmd LEFT JOIN log_manage as lm on  lm.id = lmd.ref_LogManageId ";
        Query query = baseDao.querySql(sql).setResultTransformer(Transformers.aliasToBean(LogManageVo.class));
querySql裡面封裝的createSQLQuery
重要的是這個方法setResultTransformer,createSQLQuery返回的是陣列,如果要轉換的bean物件是一個VO或者是POLO物件的話就用這個方法