如何从liferay自定义portlet使用自定义mysql查询? [英] How to use custom mysql query from my liferay custom portlet?
问题描述
我正在使用Liferay并开发我的自定义portlet,现在我想使用自定义查询从具有联接等功能的多个表中检索一些数据.
I am using Liferay and developing my custom portlet, now I want to use custom query to retrieve some data from multiple table with joins etc.
我已经针对问题解决了所有问题,但找不到了解分步过程的简单方法.
I have googled the things for my problem but can't find the simple way to understand the step-by-step procedure.
因此,如果有人可以指导我或给我任何教程,以便为我的自定义portlet创建自定义SQL查询.
So if any one can guide me or give me any tutorial to create Custom SQL query for my custom portlet.
在第4步之后,我已经在eclipse中构建了我的服务,并且显示成功.在服务/持久性包中创建了两个文件,名称分别为AdvertiseFinder.java
和AdvertiseFinderUtil.java
,但是当我尝试访问方法advertiseFinderUtil.getAd_DisplayforReports("Any arguement with string")
它给我的错误是AdvertiseFinderUtil中没有这样的方法
after this 4th step i have built my service in eclipse,and its showing successfully.there are two file created in service/persistence package with the name AdvertiseFinder.java
and AdvertiseFinderUtil.java
but when i try to access the method getAd_DisplayforReports
with the advertiseFinderUtil.getAd_DisplayforReports("Any arguement with string")
its giving me error that no such method in AdvertiseFinderUtil
我已在更新AdvertiseFinderImpl方法后构建了该服务,但该方法不起作用
I have build the service after updating my AdvertiseFinderImpl Method.but its not working
这是我的AdvertiseFinderImpl类
this is my AdvertiseFinderImpl Class
package emenu.advertise.database.service.persistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import emenu.advertise.database.model.ad_display;
import emenu.advertise.database.model.advertise;
import emenu.advertise.database.model.impl.ad_displayImpl;
import java.util.List;
import com.liferay.portal.SystemException;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.util.dao.orm.CustomSQLUtil;
public class AdvertiseFinderImpl extends BasePersistenceImpl<ad_display> implements advertiseFinder{
public void getall() {
}
// the name of the query
public static String GET_ADVERTISE = AdvertiseFinderImpl.class.getName()
+ ".getAdvertise";
// the method which will be called from the ServiceImpl class
public List<ad_display> getAd_DisplayforReports(String pattern) throws SystemException {
Session session = null;
try {
// open a new hibernate session
session = openSession();
// pull out our query from book.xml, created earlier
String sql = CustomSQLUtil.get(GET_ADVERTISE);
// create a SQLQuery object
SQLQuery q = session.createSQLQuery(sql);
// replace the "Book" in the query string with the fully qualified java class
// this has to be the hibernate table name
q.addEntity("a_ad_display", ad_displayImpl.class);
// Get query position instance
QueryPos qPos = QueryPos.getInstance(q);
// fill in the "?" value of the custom query
// this is same like forming a prepared statement
qPos.add(pattern);
// execute the query and return a list from the db
return (List<ad_display>)q.list();
/*
// use this block if you want to return the no. of rows (count)
int rows = 0;
Iterator<Long> itr = q.list().iterator();
if (itr.hasNext()) { Long count = itr.next();
if (count != null) { rows = count.intValue(); } }
return rows;
*/
} catch (Exception e) {
throw new SystemException(e);
} finally {
closeSession(session);
}
}
}
我的default-ext.xml正在关注
my default-ext.xml is following
<?xml version="1.0"?>
<custom-sql>
<sql file="custom-sql/emenu.xml" />
</custom-sql>
我的emenu.xml在这里
my emenu.xml is here
<custom-sql>
<sql id="emenu.advertise.database.service.persistence.AdvertiseFinderImpl.getAd_DisplayforReports">
<![CDATA[
SELECT
*
FROM
a_ad_display
]]>
</sql>
</custom-sql>
推荐答案
以下是在Liferay中编写自定义查询/查找器方法的步骤:
Following are the steps to write custom query / finder methods in Liferay:
- 在/generation/service/persistence目录中创建一个名为EntityFinderImpl.java的新发现器.
- 该项目的构建服务".
- ServiceBuilder自动生成以下两个额外文件:EntityFinder.java和EntityFinderUtil.java
- 现在打开EntityFinderImpl.java文件,并让此类扩展BasePersistenceImpl并实现EntityFinder. (假设在service.xml中定义了实体(表名),并且ServiceBuilder也自动生成了其他必需的类)
- 现在将所需的自定义方法添加到EntityFinderImpl.java并再次构建服务以将该方法分发到Util类.
可以使用liferay的DynamicQuery API或SQL查询创建自定义方法,如下所示:
Custom method can be created using liferay's DynamicQuery API or SQL-query as following:
public List<Entity> getCustomDataFromFinder("Parameters") throws SystemException {
Session session = null;
StringBuilder queryString = new StringBuilder();
Entity e = new EntityImpl();
try {
session = openSession();
queryString.append(" Write your Query here and conditionally append parameter value(s).");
SQLQuery query = session.createSQLQuery(queryString.toString());
query.addEntity("EntityName", EntityImpl.class);
return (List<Entity>) QueryUtil.list(query, getDialect(), 0, -1);
}
catch (Exception e) {
throw new SystemException(e);
}
finally {
if (session != null) {
closeSession(session);
}
}
}
这篇关于如何从liferay自定义portlet使用自定义mysql查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!