java类:
@Entity
@Table(name = "sc_default_score_range")
public class DefaultScoreRange implements java.io.Serializable
{
private Integer id;
@Attribute
private double score;
@Attribute
private String scoreRange;
public DefaultScoreRange()
{
}
public DefaultScoreRange(double score, String scoreRange)
{
this.score = score;
this.scoreRange = scoreRange;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId()
{
return this.id;
}
public void setId(Integer id)
{
this.id = id;
}
@Column(name = "score", nullable = false, precision = 10)
public double getScore()
{
return this.score;
}
public void setScore(double score)
{
this.score = score;
}
@Column(name = "score_range", nullable = false, length = 200)
public String getScoreRange()
{
return this.scoreRange;
}
public void setScoreRange(String scoreRange)
{
this.scoreRange = scoreRange;
}
}
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<defaultScoreRangeList>
<defaultScoreRange score="150" scoreRange="35,54,72,90"/>
<defaultScoreRange score="100" scoreRange="24,36,48"/>
<defaultScoreRange score="90" scoreRange="22,32,43"/>
<defaultScoreRange score="60" scoreRange="15,22,29"/>
</defaultScoreRangeList>
xml与java映射类:
@Root
public class DefaultScoreRangeList
{
@ElementList(inline=true)
private List<DefaultScoreRange> defaultScoreRangeList = new ArrayList<DefaultScoreRange>();
public List<DefaultScoreRange> getDefaultScoreRangeList()
{
return defaultScoreRangeList;
}
public void setDefaultScoreRangeList(
List<DefaultScoreRange> defaultScoreRangeList)
{
this.defaultScoreRangeList = defaultScoreRangeList;
}
}
实现映射:
public static <ROOT> ROOT getXMLRoot(Class<ROOT> rootClass,String xmlclasspath){
Serializer serializer = new Persister();
ROOT root = null;
try {
root = serializer.read(rootClass, ResourceUtil.getResourceAsStream(xmlclasspath));
} catch (Exception e) {
throw new RuntimeException(e);
}
return root;
}
protected List<DefaultScoreRange> parse()
{
DefaultScoreRangeList rangeList = XMLUtil.getXMLRoot(DefaultScoreRangeList.class, "init/scDefaultScoreRange.xml");
return rangeList.getDefaultScoreRangeList();
}