我正在使用Spring Data,这是很棒的东西,但有时我需要从数据库中获取比模型可以处理的更多数据。例如,我的模型如下所示。
@Entity
@Table(name = "email")
public class Mail implements Serializable {
@Getter
@Setter
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;
@Getter
@Setter
private String text;
}
我的查询比平常更复杂。我希望使用group by
来获取我的模型以及类似实体的数量。
@Query(value = "SELECT m, COUNT(m) as countValue FROM Mail m GROUP BY m.text")
List<Mail> findAllNewsletters();
我应该如何处理这样的事情?我的模型不包含countValue
,因此我会得到List<Object[]>
如何处理这种情况,保持我的代码干净,简单 使用这个。
答案 0 :(得分:2)
第1步:创建一个容器类来保存查询的输出。
Query(value = "SELECT new MailOccurence(m, COUNT(m)) FROM Mail m GROUP BY m.text")
List<MailGroup> findAllNewsletters();
第2步:从查询中填充并返回容器类的实例。
protected void Application_BeginRequest(object sender, EventArgs e)
{
string origionalpath = Request.Path.ToString().ToLower();
string subPath = string.Empty;
string blogId = string.Empty;
int id = 0;
string orgpath = origionalpath;
//public.ajax
if (origionalpath.ToLower()=="/")
{
Context.RewritePath("~/default.aspx", false);
return;
}
if (Regex.IsMatch(origionalpath, "^.*advertisements.aspx"))
{
return;
}
if (origionalpath.ToLower().Contains("favicon.ico") || Regex.IsMatch(origionalpath, "^.*/files/") || Regex.IsMatch(origionalpath, "^.*.axd") || Regex.IsMatch(origionalpath.ToLower(), "^.*.ashx") || Regex.IsMatch(origionalpath.ToLower(), "^.*/styles/"))
{
return;
}
if (Regex.IsMatch(origionalpath, "^.*.css"))
{
Context.RewritePath(origionalpath, false);
return;
}
if (Regex.IsMatch(origionalpath, "^.*.js"))
{
return;
}
if (Regex.IsMatch(origionalpath, "^.*/images/") || Regex.IsMatch(origionalpath, "^.*/scripts/") || Regex.IsMatch(origionalpath, "^.*/css/") || Regex.IsMatch(origionalpath, "^.*/propertyphoto/") || Regex.IsMatch(origionalpath, "^.*/propertyvideos/"))
{
return;
}
if (Regex.IsMatch(origionalpath, "^.*display_ad.aspx"))
{
Context.RewritePath("~/display_ad.aspx", false);
return;
}
if (origionalpath.ToLower().Contains("default.aspx"))
{
return;
}
//favicon.ico
if (Regex.IsMatch(origionalpath, "^.*.ico/"))
{
return;
}
if ((Regex.IsMatch(origionalpath, "^.*.public.ajax")) == true)
{
if (origionalpath.ToLower().Contains("contact.public.ajax"))
{
Context.RewritePath("~/Handlers/agentContact.ashx", false);
}
else if(origionalpath.ToLower().Contains("propertydetails.public.ajax"))
{
Context.RewritePath("~/Handlers/propertyDetails.ashx", false);
}
else if (origionalpath.ToLower().Contains("agentsearch.public.ajax"))
{
Context.RewritePath("~/Handlers/agentsSearch.ashx", false);
}
}
else if ((Regex.IsMatch(origionalpath, "^.*.common.ajax")) == true)
{
if (origionalpath.ToLower().Contains("affiliates.common.ajax"))
{
Context.RewritePath("~/Common/Handlers/affiliates.ashx", false);
}
else if (origionalpath.ToLower().Contains("properties.common.ajax"))
{
Context.RewritePath("~/Common/Handlers/properties.ashx", false);
}
else if (origionalpath.ToLower().Contains("propertiesdetails.common.ajax"))
{
Context.RewritePath("~/Common/Handlers/PersonalDetails.ashx", false);
}
else if (origionalpath.ToLower().Contains("searched.common.ajax"))
{
Context.RewritePath("~/Common/Handlers/Searched.ashx",false);
}
else if (origionalpath.ToLower().Contains("saveproperty.common.ajax"))
{
Context.RewritePath("~/Common/Handlers/saveProperty.ashx", false);
}
else if (origionalpath.ToLower().Contains("saveproperty.common.ajax"))
{
Context.RewritePath("~/Common/Handlers/saveProperty.ashx", false);
}
else if (origionalpath.ToLower().Contains("ads.common.ajax"))
{
Context.RewritePath("~/Common/Handlers/ads.ashx", false);
}
else if (origionalpath.ToLower().Contains("payments.common.ajax"))
{
Context.RewritePath("~/Common/Handlers/Payments.ashx", false);
}
}
else
{
if (origionalpath.ToLower().Contains("AccountInformation".ToLower()))
{
Context.RewritePath("~/Common/AccountInfo.aspx", false);
}
else if (origionalpath.Contains("/payments".ToLower()))
{
try
{
if (origionalpath.ToLower().Contains("/payments/new/"))
{
string querystring = Context.Request.QueryString["rnd"].ToString();
string[] parameters = querystring.Split('_');
string orderID = realEstate.Tools.EncryptQueryString.Decrypt(parameters[0].ToString());
string type = realEstate.Tools.EncryptQueryString.Decrypt(parameters[1].ToString());
Context.RewritePath("~/Common/Payments/PaymentsNew.aspx?ordID=" + orderID + "&type=" + type, false);
}
else if (origionalpath.ToLower().Contains("payments/status"))
{
string querystring = Context.Request.QueryString.ToString();
string url = "../../paymentStatus.aspx?" + querystring;
Context.RewritePath("~/Common/Payments/paymentStatus.aspx?" + querystring, false);
}
else
Context.RewritePath("~/Common/Payments/Payments.aspx", false);
}
catch (Exception e1)
{
Context.RewritePath("~/pagenotfound.aspx", false);
return;
}
}
}else
{
Context.RewritePath("~/pagenotfound.aspx",false);
}
}
有关详细信息,请参阅JPA specification。
答案 1 :(得分:0)
您可以选择跟随
的DTO public class MailEntry {
private Long id;
private String text;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
在您的业务逻辑中,您可以利用弹簧模板,如下面的
@Autowired
JdbcTemplate jdbcTemplate;
private static final String SQL = "SELECT m, COUNT(m) as countValue FROM Mail m GROUP BY m.text";
public List<MailEntry> getMailEntries() {
List<MailEntry> mailEntryList = jdbcTemplate.query(SQL, new RowMapper<MailEntry>() {
public MailEntry mapRow(ResultSet rs, int rowNum) throws SQLException {
MailEntry mailEntry = new MailEntry();
mailEntry.setId(rs.getInt(1));
mailEntry.setText(rs.getString(2));
return mailEntry;
}
});
return mailEntryList;
}
希望得到这个帮助。