Struts2,OPENJPA DAO。这是DAO-CRUD的好设计吗?

时间:2013-02-15 00:32:08

标签: java struts2 dao crud openjpa

您如何看待这种肮脏的设计。我正在使用openjpa 2.2,struts2。我也使用接口删除插件更新。我有一个名为Product的实体。我是初学者,我想知道你们是如何创建或设计这种功能的。如果您想了解更多信息,请说出来。谢谢

我的界面。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lotmovement.business.crud.crudinterface;

/**
 *
 * @author god-gavedmework
 */
public interface CrudInterface {

    public void Insert();
    public void Delete();
    public void Update();

}

我的主要CRUD课程。

*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lotmovement.business.crud;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import lotmovement.business.crud.crudinterface.CrudInterface;
import lotmovement.business.entity.Product;

/**
 *
 * @author god-gavedmework
 */
public class ProductCrud implements CrudInterface {

    private Date today;
    EntityStart entityStart;
    Product product;

    public Date getToday() {
        return today;
    }

    public void setToday(Date today) {
        this.today = today;
    }

    public EntityStart getEntityStart() {
        return entityStart;
    }

    public void setEntityStart(EntityStart entityStart) {
        this.entityStart = entityStart;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public void Insert() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Insert(Product product) 
            {
        entityStart.StartDbaseConnection();


        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        DateFormat timeFormat = new SimpleDateFormat("hh:mm:ss a");


        setToday(new Date());
        String dateToday = dateFormat.format(getToday());
        String timeToday = timeFormat.format(getToday());
       // product.setProduct_Id(product_Id);
      //  product.setSerial_Number(serial_Number);
      //  product.setDate_Assembled(date_Assembled);
      //  product.setModel(model);
      ///  product.setBatch_Id(batch_Id);
     //   product.setProcess_Code(process_Code);
     //   product.setDc_Power_PCB_Serial(dc_Power_PCB_Serial);
      //  product.setControl_Power_PCB_Serial(control_Power_PCB_Serial);
      //  product.setMains_Power_PCB_Serial(mains_Power_PCB_Serial);
      //  product.setBlower_Serial(blower_Serial);
      //  product.setHeaterPlate_Serial(heaterPlate_Serial);
     //   product.setLast_Process(last_Process);


        product.setTime_Assembled(timeToday);
        product.setDate_Assembled(dateToday);


        entityStart.StartPopulateTransaction(product);

        entityStart.CloseDbaseConnection();


    }

    @Override
    public void Delete() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void Update() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

我的openjpa工厂类

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lotmovement.business.crud;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class EntityStart {
    public EntityManagerFactory factory;
    public EntityManager em;


    public void StartDbaseConnection()
    {

        factory = Persistence.createEntityManagerFactory("LotMovementPU");
        em = factory.createEntityManager();

    }

    public void Begintransaction(){

        EntityTransaction userTransaction = em.getTransaction();
        userTransaction.begin();

    }


    public void StartPopulateTransaction(Object entity){

        EntityTransaction userTransaction = em.getTransaction();

        userTransaction.begin();

        em.merge(entity);
        userTransaction.commit();
        em.close();
    }

    public void CloseDbaseConnection(){
        factory.close();
    }

}

0 个答案:

没有答案