net.duckling.falcon.api.orm.BaseDao.java Source code

Java tutorial

Introduction

Here is the source code for net.duckling.falcon.api.orm.BaseDao.java

Source

/*
 * Copyright (c) 2008-2016 Computer Network Information Center (CNIC), Chinese Academy of Sciences.
 * 
 * This file is part of Duckling project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 *
 */
package net.duckling.falcon.api.orm;

import java.util.List;
import java.util.Map;

import net.duckling.common.util.CommonUtils;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

public abstract class BaseDao {
    private static final Logger LOG = Logger.getLogger(BaseDao.class);
    private JdbcTemplate jdbcTemplate;

    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return this.jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

    public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    /**
     * @description ????,"id"
     * @author lvly
     * @since 2012-08-07
     * @param t
     *            ??
     * @return id
     * **/
    public <T> int insert(T t) {
        return insert(t, "");
    }

    /**
     * @description ????,"id"
     * @author lvly
     * @since 2012-08-07
     * @param t
     *            ??
     * @param expect
     *            ?bean
     * @return id
     * **/
    public <T> int insert(T t, String expect) {
        DAOUtils<T> daoUtils = new DAOUtils<T>(t.getClass());
        String sql = daoUtils.getInsert("id," + expect);
        LOG.debug(sql);
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new MapSqlParameterSource(daoUtils.getParamMap(t, true)),
                keyHolder);
        return keyHolder.getKey().intValue();
    }

    /***
     * @description ????
     * @author lvly
     * @since 2012-08-07
     * @param t
     *            @TempField????
     * */
    public <T> List<T> findByProperties(T t) {
        return findByProperties(t, "");
    }

    /***
     * @description ????
     * @author lvly
     * @since 2012-08-07
     * @param t
     *            @TempField????
     * @param extendSQL
     *            ?SQL?
     * */
    public <T> List<T> findByProperties(T t, String extendSQL) {
        DAOUtils<T> daoUtils = new DAOUtils<T>(t.getClass());
        String sql = daoUtils.getSelect(t) + (CommonUtils.isNull(extendSQL) ? "" : " " + extendSQL.trim());
        LOG.debug(sql);
        Map<String, Object> paramMap = daoUtils.getParamMap(t);
        return getNamedParameterJdbcTemplate().query(sql, paramMap, daoUtils.getRowMapper(null));
    }

    /***
     * @description ????
     * @author lvly
     * @since 2012-08-07
     * @param t
     * */
    public <T> T findAndReturnOnly(T t) {
        return findAndReturnOnly(t, "");
    }

    /***
     * @description ????
     * @author lvly
     * @since 2012-08-07
     * @param t
     * */
    public <T> T findAndReturnOnly(T t, String extend) {
        List<T> list = findByProperties(t, extend);
        return !CommonUtils.isNull(list) ? CommonUtils.first(list) : null;
    }

    /***
     * @description ????
     * @author lvly
     * @since 2012-08-07
     * @param t
     * @return boolean ?
     * */
    public <T> boolean findAndReturnIsExist(T t) {
        return !CommonUtils.isNull(findByProperties(t));
    }

    /***
     * @description ????
     * @author lvly
     * @since 2012-08-07
     * @param t
     * @return boolean ?
     * */
    public <T> boolean findAndReturnIsExist(T t, String extend) {
        return !CommonUtils.isNull(findByProperties(t, extend));
    }

    /***
     * @description ???
     * @author lvly
     * @since 2012-08-07
     * @param t
     * */
    public <T> int update(T t) {
        DAOUtils<T> daoUtils = new DAOUtils<T>(t.getClass());
        String sql = daoUtils.getUpdate(t);
        LOG.debug(sql);
        Map<String, Object> paramMap = daoUtils.getParamMap(t);
        return getNamedParameterJdbcTemplate().update(sql, paramMap);
    }

    /***
     * @description ????
     * @author lvly
     * @since 2012-08-07
     * @param int ?
     * */
    public <T> int getCount(T t) {
        DAOUtils<T> daoUtils = new DAOUtils<T>(t.getClass());
        String sql = daoUtils.getSelect(t).replace("*", "count(*)");
        LOG.debug(sql);
        Map<String, Object> paramMap = daoUtils.getParamMap(t);
        return getNamedParameterJdbcTemplate().queryForInt(sql, paramMap);
    }

    /***
     * @description ??????
     * @author lvly
     * @since 2012-08-07
     * @param t
     * */
    public <T> int remove(T t) {
        DAOUtils<T> daoUtils = new DAOUtils<T>(t.getClass());
        String sql = daoUtils.getDelete(t);
        LOG.debug(sql);
        Map<String, Object> paramMap = daoUtils.getParamMap(t);
        return getNamedParameterJdbcTemplate().update(sql, paramMap);
    }

}