有时候,您可能希望拥有自己的Hibernate实体和dao来访问插件中的额外数据库表。通过添加以下文件和类到您的插件是非常简单的。

与Spring + Hibernate的开发类似,插件需要一个应用程序上下文文件。在我的示例插件中,我创建了一个如下所示的productsApplicationContext.xm

 

<?xml version="1.0" encoding="UTF-8"?>
 
 
 
    <bean id="productSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="setupDataSource"/>
        <property name="mappingResources">
            <list>
                <value>/org/joget/sample/products/model/Products.hbm.xml</value>
             </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>
 
    <bean id="productsDao" class="org.joget.products.dao.ProductsDaoImpl">
        <property name="sessionFactory" ref="productSessionFactory" />
    </bean>
 
</beans>

 

在应用程序的上下文中,我创建了2个bean。Bean“productSessionFactory”是用hibernate映射文件初始化会话工厂。Bean“productsDao”是初始化我的示例插件的dao对象。

接下来,我们需要一个Hibernate映射文件。在我的示例插件中,它是/org/joget/sample/products/model/Products.hbm.xml。它将POJO“org.joget.products.model.Product”映射到“valu_products”表。

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class entity-name="Products" name="org.joget.products.model.Product" table="valu_products">
    <id column="id" name="id" type="string"/>
    <property column="name" name="name" type="string"/>
    <property column="description" name="description" type="string"/>
  </class>
</hibernate-mapping>

 

您需要一个实用程序类来初始化您的应用程序上下文,并允许您检索该bean对象。

 

package org.joget.products;
 
import org.joget.apps.app.service.AppUtil;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class AppContext {
 
    private static AppContext instance;
    private AbstractApplicationContext appContext;
 
    public synchronized static AppContext getInstance() {
        if (instance == null) {
            instance = new AppContext();
        }
        return instance;
    }
 
    private AppContext() {
        Thread currentThread = Thread.currentThread();
        ClassLoader threadContextClassLoader = currentThread.getContextClassLoader();
        try {
            currentThread.setContextClassLoader(this.getClass().getClassLoader());
            this.appContext = new ClassPathXmlApplicationContext(new String[]{"/productsApplicationContext.xml"}, this.getClass(), AppUtil.getApplicationContext());
        finally {
            currentThread.setContextClassLoader(threadContextClassLoader);
        }
    }
 
    public AbstractApplicationContext getAppContext() {
        return appContext;
    }
}

 

与joget版本4相比,您需要使用orverride  findSession 方法   ProductsDaoImpl

 

 

package org.joget.products.dao;
import java.util.Collection;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.joget.commons.spring.model.AbstractSpringDao;
import org.joget.commons.util.LogUtil;
import org.joget.products.model.Product;
 
public class ProductsDaoImpl extends AbstractSpringDao implements ProductsDao {
 
    @Override
    public Session findSession() {
        Session session = null;
        SessionFactory sf = super.getSessionFactory();
         
        try {
            session = sf.getCurrentSession();
        catch (Exception e) {}
         
        if (session == null) {
            session = sf.openSession();
        }
         
        return session;
    }
     
    public Boolean addProduct(Product product) {
        try {
            save("Products", product);
            return true;
        catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Add Product Error!");
            return false;
        }
    }
 
    public Boolean updateProduct(Product product) {
        try {
            merge("Products", product);
            return true;
        catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Update Product Error!");
            return false;
        }
    }
 
    public Boolean deleteProduct(String id) {
        try {
            Product product = getProduct(id);
            if (product != null) {
           
                delete("Products", product);
            }
            return true;
        catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Delete Product Error!");
            return false;
        }
    }
 
    public Product getProduct(String id) {
        try {
            return (Product) find("Products", id);
        catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Get Product Error!");
            return null;
        }
    }
 
    public Collection<Product> getProducts() {
        try {
            Collection products = super.find("Products"""nullnullnullnullnull);
            return products;
        catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Get Products Error!");
        }
        return null;
    }
   
}

 

在你实现你的POJO和dao类后,你应该可以在你的插件中使用你的dao,如下所示。请参阅POJO附带的示例插件和dao实现。

 

ProductsDao productdao = (ProductsDao) AppContext.getInstance().getAppContext().getBean("productsDao");
 
Product p = new Product();
p.setId("001");
p.setName("Product A");
p.setDescription("Product A Descpription");
 
productdao.addProduct(p);

 

In this KB:sample plugin, 您可以通过以下JSON API添加,删除和列出产品。

ADD,

 

http://localhost:8080/jw/web/json/plugin/org.joget.products.ProductsApi/service?_action=add&name=Product A&desc=Product A Descpription

 

DELETE,

 

http://localhost:8080/jw/web/json/plugin/org.joget.products.ProductsApi/service?_action=delete&id=001

 

LIST,

 

http://localhost:8080/jw/web/json/plugin/org.joget.products.ProductsApi/service?_action=list
  • No labels