一、java反射概述java
JAVA反射機制是在運行狀態中,對於任意一個實體類,都可以知道這個類的全部屬性和方法;對於任意一個對象,都可以調用它的任意方法和屬性;這種動態獲取信息以及動態調用對象方法的功能稱爲java語言的反射機制。sql
Java的反射機制容許編程人員在對類未知的狀況下,獲取類相關信息的方式變得更加多樣靈活,調用類中相應方法,是Java增長其靈活性與動態性的一種機制。數據庫
二、java註解編程
註解自己沒有任何的做用。簡單說和註釋沒啥區別,而它有做用的緣由是:註解解釋類,也就是相關對代碼進行解釋的特定類。通常這些類使用反射是能夠拿到的。Java 註解用於爲 Java 代碼提供元數據。做爲元數據,註解不直接影響你的代碼執行,但也有一些類型的註解實際上能夠用於這一目的。Java 註解是從 Java5 開始添加到 Java 的。app
3 、反射結合註解、自實現JPA、實體映射this
話很少說、直接上乾貨spa
註解TableEntry:用於解釋實體的表名code
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface TableEntry { String tableName(); }
·PKField註解:用於解釋主鍵及主鍵生成規則對象
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface PKField { /* "ORCLE_SEQUENCE", "MYSQL_AUTO_INCREASING", "SELECTED_INCREASING", "CURE_UUID"*/ PkGeneratorEnum pkGenerator() default PkGeneratorEnum.SEQUENCE; String sequenceName() default ""; }
ColumnField註解:用於解釋實體字段和數據庫字段的對應blog
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface ColumnField { String columnName(); }
Transient註解:用於描述無需查詢出的實體屬性
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface Transient { String value() default ""; }
實體映射接口
定義接口用於 獲取實體
public interface EntryMapper {
abstract String getEntryTableName(Class<?> clazz); abstract String[] getPKFieldName(Class<?> clazz); abstract String getSequenceName(Class<?> clazz); abstract PkGeneratorEnum getPkGenerator(Class<?> clazz); }
實體映射接口實現類
經過反射獲取註解對應做用域提供的信息
public class JpaEntryMapper implements EntryMapper{
public String getEntryTableName(Class<?> clazz){ Assert.notNull(clazz, "clazz不能爲空"); TableEntry tableAnno=(TableEntry) clazz.getAnnotation(TableEntry.class); Assert.notNull(tableAnno, "TableEntry註解未設置"); Assert.state(StringUtil.isNotEmpty(tableAnno.tableName()), "tableAnno.tableName未設置"); return StringUtil.toUpperCase(tableAnno.tableName()); } public String[] getPKFieldName(Class<?> clazz){ Assert.notNull(clazz, "clazz不能爲空"); Field[] fields= clazz.getDeclaredFields(); int pkArrLen=(fields.length>10?10:fields.length); // String[] pkArr=new String[pkArrLen]; List<String> pk_list = new ArrayList<String>(pkArrLen); for(int i=0; i < fields.length ; i++){ if("serialVersionUID".equalsIgnoreCase(fields[i].getName()))continue; PKField pkField = fields[i].getAnnotation(PKField.class); if(pkField != null){ ColumnField columnField = fields[i].getAnnotation(ColumnField.class); if(columnField==null){ //未設置columnField註解默認屬性值 pk_list.add(StringUtil.toLowerCase(fields[i].getName())); }else{ pk_list.add(StringUtil.toLowerCase(columnField.columnName())); } } } Assert.notEmpty(pk_list, "PKField未設置或ColumnField未設置"); return pk_list.toArray(new String[0]); } public String getSequenceName(Class<?> clazz){ Assert.notNull(clazz, "clazz不能爲空"); Field[] fields= clazz.getDeclaredFields(); for(Field field : fields){ if("serialVersionUID".equalsIgnoreCase(field.getName()))continue; PKField pkField = field.getAnnotation(PKField.class); if(pkField != null){ if(StringUtil.isNotEmpty(pkField.sequenceName())){ return StringUtil.toUpperCase(pkField.sequenceName()); } } } Assert.state(false, "pkField.sequenceName未設置"); return null; } public PkGeneratorEnum getPkGenerator(Class<?> clazz){ Assert.notNull(clazz, "clazz不能爲空"); Field[] fields= clazz.getDeclaredFields(); for(Field field : fields){ if("serialVersionUID".equalsIgnoreCase(field.getName()))continue; PKField pkField = field.getAnnotation(PKField.class); if(pkField != null){ // Assert.notNull(pkField.pkGenerator(), "pkField.pkGenerator未設置"); if(pkField.pkGenerator()!=null){ return pkField.pkGenerator(); } } } Assert.state(false, "PKField未設置"); return null; } }
抽象sql中的where 條件 和 whereUnit單元
Where類
public class Where {
private List<WhereUnit> whereUnitList ; private boolean isNamed = false ; public Where(){ whereUnitList = new ArrayList<WhereUnit>() ; } public Where(boolean isNamed){ this(); this.isNamed=isNamed; } public Where(Object inputDto,boolean isNamed){ this(isNamed); initEntry(inputDto); } public Where(Object inputDto){ this(); initEntry(inputDto); } public Where and(String colName ,String comparator ,Object colVal) { add(new WhereUnit("AND",colName,comparator,colVal)); return this; } public Where or(String colName ,String comparator ,Object colVal) { add(new WhereUnit("OR",colName,comparator,colVal)); return this; } public boolean add(WhereUnit unit) { return whereUnitList.add(unit); } public WhereUnit get(int index) { return whereUnitList.get(index); } public WhereUnit set(int index,WhereUnit unit) { if(index<0 || index >= size() ){ throw new IndexOutOfBoundsException("setUnit的index超出List的長度"); } return whereUnitList.set(index, unit); } public Iterator<WhereUnit> iterator(){ return whereUnitList.iterator(); } public WhereUnit remove(int index) { return whereUnitList.remove(index); } public boolean remove(WhereUnit unit) { return whereUnitList.remove(unit); } public int size() { return whereUnitList.size(); } public int indexOf(WhereUnit unit) { return whereUnitList.indexOf(unit); } public StringBuffer toSqlString(){ StringBuffer where_Buffer=new StringBuffer(" WHERE 1=1 "); Iterator<WhereUnit> iter=whereUnitList.iterator(); while(iter.hasNext()){ WhereUnit unit = iter.next(); Assert.notNull(unit,"WhereUnit不未空"); where_Buffer.append(unit.toSqlString(this.isNamed)); } return where_Buffer; } public Object[] getParamterValArr(){ List<Object> paras= new ArrayList<Object>(size()); Iterator<WhereUnit> iter = whereUnitList.iterator(); while(iter.hasNext()){ WhereUnit unit =iter.next(); if("IN".equalsIgnoreCase(unit.getComparetor())){ continue; } paras.add(unit.getColVal()); } return paras.toArray(); } public Map<String,Object> getParamterMap(){ Map<String,Object> para_map= new HashMap<String,Object>(size()); Iterator<WhereUnit> iter = whereUnitList.iterator(); while(iter.hasNext()){ WhereUnit unit =iter.next(); if("IN".equalsIgnoreCase(unit.getComparetor())){ continue; } para_map.put( unit.getColName(),unit.getColVal()); } return para_map; } private void initEntry(Object entry) { // TODO Assert.notNull(entry, "entry不爲空"); Assert.notNull(whereUnitList, "whereUnitList不爲空"); Class<?> entry_class = entry.getClass(); try{ Field[] fields = entry_class.getDeclaredFields(); for(Field field : fields){ String field_name = field.getName(); if(field_name.equalsIgnoreCase("serialVersionUID"))continue; String read_method_name = "get"+StringUtil.toUpperFristChar(field_name); Method read_method= entry_class.getDeclaredMethod(read_method_name); Object field_val = read_method.invoke(entry); if ( field_val != null ){ ColumnField columnFieldAnno = field.getAnnotation(ColumnField.class); if(columnFieldAnno == null){ and(field_name,"=",field_val); }else{ and(columnFieldAnno.columnName(),"=",field_val); } } } } catch (Exception e) { e.printStackTrace(); } } }
WhereUnit類
public class WhereUnit {
private String colName; private String comparetor; private String linkSign; private Object colVal; public WhereUnit(){} public WhereUnit(String linkSign, String colName , String comparetor,Object colVal){ this.linkSign=linkSign; this.colName=colName; this.comparetor=comparetor; this.colVal=colVal; } public String getColName() { return colName; } public void setColName(String colName) { this.colName = colName; } public Object getColVal() { return colVal; } public void setColVal(Object colVal) { this.colVal = colVal; } public String getComparetor() { return comparetor; } public void setComparetor(String comparetor) { this.comparetor = comparetor; } public String getLinkSign() { return linkSign; } public void setLinkSign(String linkSign) { this.linkSign = linkSign; } public StringBuffer toSqlString(boolean isNamed){ StringBuffer unit_Buffer = new StringBuffer(); unit_Buffer.append(" ").append(linkSign).append(" ").append(colName).append(" ").append(comparetor); if("IN".equalsIgnoreCase(comparetor)){ unit_Buffer.append(" (").append(colVal).append(") ");; }else{ if(isNamed){ unit_Buffer.append(" :"+colName+" "); }else{ unit_Buffer.append(" ? "); } } return unit_Buffer; } }
JpaService接口定義經常使用的方法
public interface JpaService {
<T> T findById(Class<T> clazz, Long id); <T> T findById(Class<T> clazz, Where pks_where); <T> T findById(Class<T> clazz, Long id, Boolean cascade); <T> T findById(Class<T> clazz, Where pks_where, Boolean cascade); <T> List<T> findList(T inputDto); <T> List<T> findList(Class<T> clazz, Where where, OrderBy orderby); <T> List<T> findList(Class<T> clazz, Where where); List<Map<String,Object>> findList(String tableName, Where where, OrderBy orderby); List<Map<String,Object>> findList(String tableName, Where where); <T> List<T> findAll(Class<T> clazz); <T> List<T> findAll(Class<T> clazz, OrderBy orderby); List<Map<String,Object>> findAll(String tableName); List<Map<String,Object>> findAll(String tableName, OrderBy orderby); <T> Pager<T> findPager(Class<T> clazz, Pager<T> pager, Where where); <T> Pager<T> findPager(Class<T> clazz, Pager<T> pager); <T> Pager<T> findPager(Class<T> clazz, Pager<T> pager, Where where, OrderBy orderby); Long selectTotal(String tableName, Where where); Long selectTotal(Class<?> clazz, Where where); <T> List<T> selectPageList(Class<T> clazz, Pager<T> pager, Where where, OrderBy orderby); int insert(Object entry); int insert(Class<?> clazz,SqlSet sql_set); int update(Object entry); int update(Class<?> clazz, SqlSet sql_set); long insertUseKey(Object t); long insertUseKey(Class<?> clazz, SqlSet sql_set); int delete(Class<?> clazz, Long id); int delete(Class<?> clazz, Where pk_where); Long getSeq(Class<?> clazz); }
實現JpaService的接口,就能愉快的經過jpaService直接調用經常使用的方法啦
我是飛奔的企鵝:
一隻有夢想,有故事的企鵝 ,歡迎訴說你的故事