Java tutorial
/* * Copyright 2016 OPEN TONE Inc. * * 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 jp.co.opentone.bsol.linkbinder.dao; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.ibatis.sqlmap.client.SqlMapClient; import jp.co.opentone.bsol.framework.core.dao.DataSourceSelector; import jp.co.opentone.bsol.framework.core.exception.ReflectionRuntimeException; /** * ???Dao??. * * @author opentone * */ public class BaseDao extends SqlMapClientDaoSupport implements Serializable { /** * SerialVersionUID. */ private static final long serialVersionUID = 3039177945884371425L; /** * ???%. */ private static final String PERCENT = "%"; /** * ???_. */ private static final String UNDER_BAR = "_"; /** * SQL?namespace. */ private String namespace; /** * ??DataSource?????. */ @Resource private DataSourceSelector selector; /** * SQL???????. * @param namespace * SQL?namespace */ public BaseDao(String namespace) { this.namespace = namespace; } /** * ?. */ @PostConstruct public void initialize() { initDataSource(); } private void initDataSource() { selectDataSource(selector); } /** * ????? ??????. * @param dataSourceSelector * ? */ public void selectDataSource(DataSourceSelector dataSourceSelector) { super.setDataSource(dataSourceSelector.select()); } /** * SqlMapClient?. ??SqlMapClientDaoSupport?SqlMapClient???? * ?????. * @param sqlMapClient * SqlMapClient */ @Resource public final void setMySqlMapClient(SqlMapClient sqlMapClient) { super.setSqlMapClient(sqlMapClient); } /** * SQL?SQL ID?namespace???. * @param id * ID * @return namespace??ID. namespace????id??????. */ protected String getSqlId(String id) { if (StringUtils.isNotEmpty(namespace)) { return namespace + "." + id; } else { return id; } } /** * ??????. * * ???? * (???????????) * * @param <C> ?? * @param condition ? * @param fields ???? * @return ? * @throws ReflectionRuntimeException ??? */ @SuppressWarnings("unchecked") protected <C> C getLikeSearchCondition(Object condition, List<String> fields) throws ReflectionRuntimeException { C clone = null; try { clone = (C) condition.getClass().getConstructor().newInstance(); PropertyUtils.copyProperties(clone, condition); // escape(clone, fields); } catch (NoSuchMethodException e) { throw new ReflectionRuntimeException(e); } catch (IllegalArgumentException e) { throw new ReflectionRuntimeException(e); } catch (SecurityException e) { throw new ReflectionRuntimeException(e); } catch (InstantiationException e) { throw new ReflectionRuntimeException(e); } catch (IllegalAccessException e) { throw new ReflectionRuntimeException(e); } catch (InvocationTargetException e) { throw new ReflectionRuntimeException(e); } return clone; } /** * ???. * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalAccessException */ private void escape(Object clone, List<String> fields) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { String escapeChar = (String) PropertyUtils.getProperty(clone, "escapeChar"); String escapeEscapeChar = escapeChar + escapeChar; String escapePercent = escapeChar + PERCENT; String escapeUnderBar = escapeChar + UNDER_BAR; for (String field : fields) { Object object = PropertyUtils.getProperty(clone, field); if (object != null) { String value = String.valueOf(object); // ? value = value.replaceAll(escapeChar, escapeEscapeChar); // %? value = value.replaceAll(PERCENT, escapePercent); // _? value = value.replaceAll(UNDER_BAR, escapeUnderBar); PropertyUtils.setProperty(clone, field, value); } } } }