• 需求:查询全部记录,并按original_order字段倒序排列

    @Test
    public void selectAllTest() {
        dao = (TApprovalDAO) applicationContext.getBean("TApprovalDAO");

        TApprovalExample example = new TApprovalExample();
        
        //按`original_order`字段倒序排列
        example.setOrderByClause("original_order desc");

        //是否去重,假为不去重,真为去重
        example.setDistinct(false);

        //构造自定义查询准则,Criteria:准则,原则,标准
        Criteria criteria = example.createCriteria();

        // 主键非空
        criteria.andIdIsNotNull();
        
        List<TApproval> list = dao.selectByExample(example);

        for (TApproval tApproval : list) {
            System.err.println(tApproval);
        }
        
    }



无报错,输出全部记录列表.

看看log4j在控制台打印的SQL语句:



Preparing: select id, department_number, approvals_time, reply_opinion, original_order, auditor, approve_operates from t_approval WHERE ( id is not null ) order by original_order desc


改动部分列


@Test
    public void updateTest() {
        dao = (TApprovalDAO) applicationContext.getBean("TApprovalDAO");

        TApprovalExample example = new TApprovalExample();

        TApproval approval = new TApproval();

        approval.setApprovalsTime(new Date());
        approval.setAuditor(60);
        
        approval.setApproveOperates(false);
        approval.setReplyOpinion("nothing at all");

        Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(2);

        int selective = dao.updateByExampleSelective(approval, example);
        System.err.println("result-" + selective);

        /*
         * Preparing: update t_approval SET approvals_time = ?, reply_opinion = ?,
         * auditor = ?, approve_operates = ? WHERE ( id = ? )
         */
    }




DEFAULT
19 声望2 粉丝