Struts 2 直接利用查询字段生成Entity实体,select new cn.hdu.domain.application.Application(t.id,t.applicationNO) from Application t
来源:互联网 发布:尼古拉二世 知乎 编辑:程序博客网 时间:2024/06/05 13:58
下面是自己在做项目时新知识的收获,关键部分都做了说明,请详细阅读红色字体:
file--Application.java:
package cn.hdu.domain.application;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorValue("Application")
@SequenceGenerator(name = "application_seq", allocationSize = 1)
public class Application {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="application_seq")
private Long id;
@Column(unique = true)
private String applicationNO; // 申请号
@Column(length = 200)
private String projectName; // 项目名称
... getter() 和 setter() 方法 省略
// 别忘了默认构造函数,否则会出错,小心哦。因为实体类必须包含默认构造函数
// default constructor
public Application() { }
// 查询语句选择的字段构造一个Entity必须有对应的构造函数
public Application(Long id, String applicationNO, String projectName, String projectCateName, String unitName, String principalName,
String administrator, String manageFieldsName, Date submitDate) {
this.id = id;
this.applicationNO = applicationNO;
this.projectName = projectName;
this.projectCateName = projectCateName;
this.unitName = unitName;
this.principalName = principalName;
this.administrator = administrator;
this.manageFieldsName = manageFieldsName;
this.submitDate = submitDate;
}
}
file--ApplicationQueryServiceImpl.java :
package cn.hdu.service.impl.application;
public class ApplicationQueryServiceImpl implements ApplicationQueryService
{
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em)
{
this.em = em;
}
public int numberator(String numberRaw)
{
return Integer.parseInt(numberRaw.substring(1, numberRaw.length() - 1));
}
@SuppressWarnings("unchecked")
@Override
public List<Application> applicationList_AByState(int start, int limit, String searchPara, String searchYear) {
String sqlCount = "select count(*)";
// 以下事项非常关键
// Note:1. 类的名称必须用绝对路径名称;
// 2. 选择的属性必须在Entity类中有对应的构造函数;
// 3. 属性拼写一定要和对应Entity类中的属性名称相同,因为Java是根据反射机制实现类的属性初始化的。
String sqlQuery = "select new cn.hdu.domain.application.Application(t.id,t.applicationNO,t.projectName,t.projectCateName,t.unitName,t.principalName,t.administrator,t.manageFieldsName,t.submitDate)";
String sql = " from Application t where t.state=12";
if(null != searchPara && !searchPara.equals("")) {
sql += " and (t.projectName like '%" + searchPara + "%'" + " or t.principalName like '%" + searchPara + "%'" + " or t.unitName like '%" + searchPara + "%'"
+ " or t.projectCateName like '%" + searchPara + "%'" + " or t.applicationNO like '%" + searchPara + "%')";
}
if(null != searchYear && !searchYear.equals("")) {
sql += " and t.applicationNO like '" + searchYear + "%'";
}
sqlCount += sql;
sqlQuery += sql;
Query query0 = em.createNativeQuery(sqlCount);
Map<String, Integer> recordSession = ActionContext.getContext().getSession();
recordSession.put("recordCount", numberator(query0.getSingleResult().toString()));
// 上述利用查询直接生成Entity实体的只能利用 EntityManager 的 createQuery 方法,它是利用JPQL查询的。
Query query1 = em.createQuery(sqlQuery);
return query1.setMaxResults(limit).setFirstResult(start).getResultList();
}
}
file--ApplicationQueryAction.java:
package cn.hdu.action.application;
@SuppressWarnings("serial")
public class ApplicationQueryAction extends ActionSupport implements ModelDriven<Application>
{
private Application application = new Application();
private ApplicationQueryService applicationQueryService;
private List<Application> results;
@JSON(serialize = false)
public Application getModel()
{
return application;
}
@SuppressWarnings("unchecked")
public String viewApplication_AByState() {
int iStart = Integer.parseInt(start);
int iLimit = Integer.parseInt(limit);
Map<String, Integer> session = ActionContext.getContext().getSession();
// 根据调试得出:是根据Application实体类的非默认构造函数初始化类的,其它对象都为null,基本类型赋值为0。
results = applicationQueryService.applicationList_AByState(iStart, iLimit, searchPara, searchYear);
recordCount = session.get("recordCount");
return SUCCESS;
}
}
- Struts 2 直接利用查询字段生成Entity实体,select new cn.hdu.domain.application.Application(t.id,t.applicationNO) from Application t
- xcode can’t install application
- Couldn't communicate with helper application
- This application is modifying the autolayout engine from a background thread, which can lead t
- Application Domain
- select * from t 和select * from t order by id两个有什么区别
- Empty Application won't compile, Asset Catalog Compiler Error
- gitBox 出现Couldn’t communicate with a helper application.
- Xcode Couldn’t communicate with a helper application.
- The Application of Cisco compatible 10GBASE-T Transceiver
- T-SQL SELECT 查询
- Cocos2dx研究:关于‘Can't install application The Info.plist for application at..'的错误
- 生成短链接t.cn
- 【iOS开发-71】解决方案:Attempting to badge the application icon but haven't received permission from the...
- Attempting to badge the application icon but haven't received permission from the user to badge the
- Swift之Attempting to badge the application icon but haven't received permission from the user to badg
- Attempting to badge the application icon but haven't received permission from the user to badge the
- Attempting to badge the application icon but haven't received permission from the user to badge the
- dataset与xml之间的转换
- 安全认证(Login画面的web.config)
- 在VMware的虚拟机平台上如何进行NAT网络设置
- vivi 启动解析 (转)
- web.config常用配置标记
- Struts 2 直接利用查询字段生成Entity实体,select new cn.hdu.domain.application.Application(t.id,t.applicationNO) from Application t
- Flash 会表数据 之oracle 10g
- 都江堰操作系统简介
- 类的概念
- LINUX的热插拔机制(经典文章)
- 看美女怎样将正装穿出性感范儿(多图)
- 使用异步 I/O 大大提高应用程序的性能
- 基于CMPP3.0的企业短信网关API
- c#.net函数列表