java注解中可使用对象_Java注解(二):实战 - 直接使用对象列表生成报表...
通過對Java注解(一):介紹,思想及優點學習了解,相信大家對Java注解有一定程度的了解,本篇文章將實戰項目中的應用來加深對Java注解的了解。
本實例實現根據指定字段的JavaBean,生成對應列的報表。使用Java注解就是方便實現JavaBean與Excel或CSV列已一一映射。直觀展現數據,不需要中間轉換,Java注解可以很輕松實現。
下面先給出Java注解的定義:
import java.lang.annotation.*;
/**
* 類功能描述:列屬性
*
* @author WangXueXing create at 18-10-29 下午4:40
* @version 1.0.0
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ColumnProperty {
/**
* 列序號
* @return
*/
int index();
/**
* 列表頭名
* @return
*/
String name();
}
本注解定義目的是使用在JavaBean的每個字段映射到Excel中的列序號及列表頭名。這樣對于開發者,我只需要定義好報表展示的列及相對位置,并組裝JavaBean list.直觀,不需要關心轉換過程。
下面代碼就是定義JavaBean及使用上面定義的注解:
import com.today.service.financereport.util.ColumnProperty
import scala.annotation.meta.field
import scala.beans.BeanProperty
/**
* 類功能描述:營收明細
*
* @author WangXueXing create at 18-10-23 下午7:52
* @version 1.0.0
*/
case class IncomeDetail(/** 店號 */
@(ColumnProperty@field)(index = 0, name = "店號")
@BeanProperty
storeId: String,
/** 店名 */
@(ColumnProperty@field)(index = 1, name = "店名")
@BeanProperty
storeName: String,
/** 門店類型 */
@(ColumnProperty@field)(index = 2, name = "門店類型")
@BeanProperty
storeType: String,
/** 營業日期 */
@(ColumnProperty@field)(index = 3, name = "營業日期")
@BeanProperty
businessDate: String,
/** 交易時間 */
@(ColumnProperty@field)(index = 4, name = "交易時間")
@BeanProperty
payTime: String,
/** 訂單號 */
@(ColumnProperty@field)(index = 5, name = "訂單號")
@BeanProperty
orderNo: String,
/** 交易流水號 */
@(ColumnProperty@field)(index = 6, name = "交易流水號")
@BeanProperty
tradeNo: String,
/** 金額 */
@(ColumnProperty@field)(index = 7, name = "金額")
@BeanProperty
tradeAmount: BigDecimal,
/** 抹零金額 */
@(ColumnProperty@field)(index = 8, name = "抹零金額")
@BeanProperty
spotAmount: BigDecimal,
/** 支付方式 */
@(ColumnProperty@field)(index = 9, name = "支付方式")
@BeanProperty
tradeType: String,
/** POS機 */
@(ColumnProperty@field)(index = 10, name = "POS機")
@BeanProperty
posId: String,
/** 收銀員 */
@(ColumnProperty@field)(index = 11, name = "收銀員")
@BeanProperty
cashierName: String,
/** 訂單類型 */
@(ColumnProperty@field)(index = 12, name = "訂單類型")
@BeanProperty
payType: String,
/** 第三方商戶訂單號 */
@(ColumnProperty@field)(index = 13, name = "第三方商戶訂單號")
@BeanProperty
thirdPartyPaymentNo: String
)
其中@BeanProperty是Scala自帶的一個注解,就是免去Java中對每個字段的getter()與setter()的定義。
以下是偽代碼,從數據庫中獲取對應數據結構為IncomeDetail的數據列表:
def getReportData: List[IncomeDetail] = {
return jdbc.getIncomeDetailList()
}
獲取到數據后,將數據通過注解轉換并填入報表:
val incomeDetailList = getReportData()
val file = File.createTempFile( "營收明細報表_" + MathUtils.getRandom(4), ".csv")
ScalaFunction.tryWithResources(new PrintWriter(file, "GBK")) { out =>
val dataObj =incomeDetailList.isInstanceOf[Seq[AnyRef@unchecked]] match {
case true => incomeDetailList.asInstanceOf[List[AnyRef@unchecked]]
case _ => List.empty
}
//根據注解順序生成CSV數據列表
AnnotationUtil.getValueWithHead(dataObj).foreach(out.println(_))
}
其中ScalaFunction.tryWithResources()仿照Java try with resources本人定義了一個Scala函數來處理流關閉,詳情請參考我以前的博客:Scala實現Try with resources自動關閉IO
/**
* 類功能描述:Scala高級函數
*
* @author WangXueXing create at 18-11-22 下午5:29
* @version 1.0.0
*/
object ScalaFunction {
/**
* Scala實現Java7 try-with-resources
* @see https://www.cnblogs.com/barrywxx/p/10002422.html
*/
def tryWithResources[A <: close unit b a> B): B = {
try {
f(a)
} finally {
if(a != null){
a.close()
}
}
}
}
我們重點關注下AnnotationUtil.getValueWithHead(), 這個方法定義了通過注解將數據填入Excel或CSV報表的過程:
import java.lang.reflect.Field
import com.today.service.financereport.dto.IncomeDetail
/**
* 類功能描述:Scala注解工具類
*
* @author WangXueXing create at 18-10-30 下午3:35
* @version 1.0.0
*/
object AnnotationUtil {
/**
* 根據注解獲取值列表
*/
def getValueWithHead(valList: List[_]): List[String] ={
var fields: Array[Field] = Array.empty
val dataList: List[String] = valList.zipWithIndex.map{ x=>
if(x._2 == 0){
fields = x._1.getClass.getDeclaredFields.sortBy(_.getAnnotations.apply(0).asInstanceOf[ColumnProperty].index())
}
fields.map{f=>
f.setAccessible(true)
val value = f.get(x._1)
if(value == null){
""
} else {
value.toString
}
}.mkString(",")
}
fields.map(_.getAnnotation(classOf[ColumnProperty]).name()).mkString(",")+:dataList
}
def main(args: Array[String]): Unit = {
val list = List(IncomeDetail("wew1","eerr1","wrw1",null,"1","1","1",BigDecimal(12.1),BigDecimal(12.1),"1","1","1"),
IncomeDetail("rer2","wrwrw2","wrw2","2","2","2","2",BigDecimal(12.1),BigDecimal(12.1),"2","2","2"))
val objList = getValueWithHead(list)
objList.foreach(println(_))
}
}
好了,到這里,這個實例已經完成。是不是以后就不需要關注怎樣組裝復雜報表數據結構,只需要將JavaBean定義好,按照這個JavaBean的數據結構組裝數據就行了!
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java注解中可使用对象_Java注解(二):实战 - 直接使用对象列表生成报表...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农民丰收卡怎么办?办理农民丰收卡有什么好
- 下一篇: 安徽大学计算机考研学硕2019初试单科线