下面是我自己定義的標簽mycontent_list
首先,在數據庫里創建了一個jc_mycontent的表,其中有id,title,content三個字段
其次,創建了一個實體類
public class MyContent {
private static final long serialVersionUID = 1L;
private Integer id;
private String title;
private String content;
public MyContent () {
super();
}
……get?set方法
}
接下來是配置hibernate中jc_mycontent表的配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.jeecms.cms.entity.main">
<class name="MyContent" table="jc_mycontent">
<meta attribute="sync-DAO">false</meta>
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer" column="id"><generator class="identity"/></id>
<property name="title" column="title" type="java.lang.String" not-null="true" />
<property name="content" column="content" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>
與數據庫交互的持久層接口
public interface MyContentDao {
public List<MyContent> getList();
}
持久層實現類
@Repository//持久層
public class MyContentDaoImpl extends HibernateBaseDao<MyContent, Integer>
implements MyContentDao {
@SuppressWarnings("unchecked")
public List<MyContent> getList(){
return find(byNothing());
}
private Finder byNothing(){
Finder f = Finder.create();
f.append("from MyContent");//可以在此處添加查詢條件或者添加各種方法進行動態查詢
f.setCacheable(true);
return f;
}
@Override
protected Class<MyContent> getEntityClass() {
return MyContent.class;
}
}
業務層接口
public interface MyContentMng {
public List<MyContent> getList();
}
業務層實現類
@Service//業務層
@Transactional
public class MyContentMngImpl implements MyContentMng {
@Transactional(readOnly = true)//配置事務為只讀
public List<MyContent> getList(){
return myContentDao.getList();
}
private MyContentDao myContentDao;
@Autowired//自動綁定
public void setMyContentDao(MyContentDao myContentDao) {
this.myContentDao = myContentDao;
}
private List<ContentListener> listenerList;
@Autowired
public void setListenerList(List<ContentListener> listenerList) {
this.listenerList = listenerList;
}
}
標簽類的抽象類,最主要的就是getData這個方法,以及綁定業務層,其中可以添加多種查詢方法。可參考類AbstractContentDirective
public abstract class AbstractMyContentDirective implements TemplateDirectiveModel {
protected Object getData(Map<String, TemplateModel> params, Environment env)
throws TemplateException {
return myContentMng.getList();
}
@Autowired
protected MyContentMng myContentMng;
}
自定義標簽中最重要的類繼承上邊的抽象類
public class MyContentListDirective extends AbstractMyContentDirective {
/**
* 模板名稱
*/
public static final String TPL_NAME = "mycontent_list";
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
//獲取站點
CmsSite site = FrontUtils.getSite(env);
//獲取內容列表
List<MyContent> list = getList(params, env);
Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(params);
//OUT_LIST值為tag_list,將內容列表放入其中
paramWrap.put(MYOUT_LIST, DEFAULT_WRAPPER.wrap(list));
//將params的值復制到variable中
Map<String, TemplateModel> origMap = DirectiveUtils.addParamsToVariable(env, paramWrap);
//沒有采用默認的模板,直接采用自己寫的簡單的模板(mycontent_list.html)
FrontUtils.includeTpl(TPL_NAME, site, params, env);
//將variable中的params值移除
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
}
protected List<MyContent> getList(Map<String, TemplateModel> params,
Environment env) throws TemplateException {
return myContentMng.getList();
}
}
樣式模板mycontent_list.html內容,里邊可以自己添加一些樣式,可參考\t\cms_sys_defined\style_list下樣式文件
[#list mytag_list as a]
<li><a href="${a.title}"><font color='blue'>"${a.content}"</font></a></li>
[/#list]
首頁里加入如下代碼
[@cms_mycontent_list]
<ul class="topnews">
</ul>
[/@cms_mycontent_list]
通過以上這些代碼,可以實現將自己的表jc_mycontent中的數據查詢并顯示在頁面上
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

