Jakarta ee 如何改进我的刀爪哇EE

Jakarta ee 如何改进我的刀爪哇EE,jakarta-ee,dao,Jakarta Ee,Dao,我想访问我的申请者数据库,这就是为什么我为它创建了一个DAO类 我想我有很多代码的味道,因为我不断重复一些代码。那么,为了减少代码气味,我可以做些什么来简化代码?我违反了什么规则?如何改进代码?多谢各位 我的代码如下: public class ApplicantDAO { private static ApplicantDAO me = null; private ApplicantDAO(){}; public static synchronized ApplicantDAO getI

我想访问我的申请者数据库,这就是为什么我为它创建了一个DAO类

我想我有很多代码的味道,因为我不断重复一些代码。那么,为了减少代码气味,我可以做些什么来简化代码?我违反了什么规则?如何改进代码?多谢各位

我的代码如下:

public class ApplicantDAO {

private static ApplicantDAO me = null;

private ApplicantDAO(){};

public static synchronized ApplicantDAO getInstance() {
    if(me == null) {
        me = new ApplicantDAO();
    }
    return me;
}

public Applicant getApplicant(int applicantNumber) throws SQLException {
    Applicant applicant = null;

    Connection conn = null; 
    Statement statement= null;
    String query = null;
    ResultSet rs = null;

    try {
        conn = ConnectionManager.getConnection();
        statement = conn.createStatement();
        query = "SELECT * FROM applicant WHERE applicant_no = '" + applicantNumber +"'";    //check applicant_number
        rs = statement.executeQuery(query);

        while(rs.next()){
            applicant = new Applicant();

            applicant.setApplicantNumber(rs.getInt("applicant_no"));
            applicant.setApplicationDate(rs.getString("applicant_date")); 
            applicant.setfName(rs.getString("first_name"));
            applicant.setlName(rs.getString("last_name"));
            applicant.setmName(rs.getString("middle_name"));
            applicant.setAge(rs.getInt("age"));
            applicant.setGender(rs.getString("gender"));
            applicant.setEmail(rs.getString("email_address"));
            applicant.setContactNumber(rs.getString("contact_no"));
            applicant.setCity(rs.getString("city"));
            applicant.setSchool(rs.getString("school"));
            applicant.setCourse(rs.getString("course"));
            applicant.setYearGraduated(rs.getInt("year_graduated"));
            applicant.setYearWorkExp(rs.getInt("year_work_exp"));
            applicant.setSourceChannel(rs.getString("source_channel"));
            applicant.setStatus_id(rs.getInt("status_id"));

        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally {
        if (rs != null) try { rs.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (conn!= null) try { conn.close(); } catch (SQLException logOrIgnore) {}
    }


    return applicant;
}

public ArrayList<Applicant> getApplicants() throws SQLException{
    ArrayList<Applicant> applicantList = null;
    Applicant applicant = null;

    Connection conn = null; 
    Statement statement= null;
    String query = null;
    ResultSet rs = null;

    try {
        conn = ConnectionManager.getConnection();
        statement = conn.createStatement();
        query = "select * from applicant";
        rs = statement.executeQuery(query);

        while(rs.next()){
            if(applicantList == null){
                applicantList = new ArrayList<Applicant>();
            }
            applicant = new Applicant();

            applicant.setApplicantNumber(rs.getInt("applicant_no"));
            applicant.setApplicationDate(rs.getString("applicant_date")); 
            applicant.setfName(rs.getString("first_name"));
            applicant.setlName(rs.getString("last_name"));
            applicant.setmName(rs.getString("middle_name"));
            applicant.setAge(rs.getInt("age"));
            applicant.setGender(rs.getString("gender"));
            applicant.setEmail(rs.getString("email_address"));
            applicant.setContactNumber(rs.getString("contact_no"));
            applicant.setCity(rs.getString("city"));
            applicant.setSchool(rs.getString("school"));
            applicant.setCourse(rs.getString("course"));
            applicant.setYearGraduated(rs.getInt("year_graduated"));
            applicant.setYearWorkExp(rs.getInt("year_work_exp"));
            applicant.setSourceChannel(rs.getString("source_channel"));
            applicant.setStatus_id(rs.getInt("status_id"));

            applicantList.add(applicant);
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
         if (rs != null) try { rs.close(); } catch (SQLException logOrIgnore) {}
         if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
         if (conn!= null) try { conn.close(); } catch (SQLException logOrIgnore) {}
    }
    return applicantList;
}
公共类应用程序dao{
私有静态应用程序dao me=null;
私有应用程序dao(){};
公共静态同步应用程序dao getInstance(){
如果(me==null){
me=新应用程序dao();
}
还我;
}
公共申请人GetApplicator(int ApplicationNumber)引发SQLException{
申请人=空;
连接conn=null;
Statement=null;
字符串查询=null;
结果集rs=null;
试一试{
conn=ConnectionManager.getConnection();
statement=conn.createStatement();
query=“从申请人中选择*,其中申请人编号=”+“申请人编号+”;//检查申请人编号
rs=语句执行(查询);
while(rs.next()){
申请人=新申请人();
申请人。设置申请人编号(rs.getInt(“申请人编号”);
申请人.setApplicationDate(rs.getString(“申请人日期”);
申请人名称(rs.getString(“名字”);
申请人名称(rs.getString(“姓氏”);
申请人.setmName(rs.getString(“中间名”);
申请人。设置(rs.getInt(“年龄”));
申请人:setGender(rs.getString(“性别”));
申请人.setEmail(rs.getString(“电子邮件地址”);
申请人。设置联系人号码(rs.getString(“联系人号码”);
申请人:setCity(rs.getString(“城市”);
申请人:setSchool(rs.getString(“学校”);
申请人。设置课程(rs.getString(“课程”));
申请人:Setyear毕业(rs.getInt(“本科毕业”);
申请人:setYearWorkExp(rs.getInt(“year\u work\u exp”);
申请人.setSourceChannel(rs.getString(“source_channel”);
申请人。设置状态id(rs.getInt(“状态id”);
}
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}最后{
如果(rs!=null),请尝试{rs.close();}catch(SQLException logOrIgnore){}
如果(statement!=null),请尝试{statement.close();}catch(SQLException logOrIgnore){}
如果(conn!=null),请尝试{conn.close();}catch(SQLException logOrIgnore){}
}
返回申请人;
}
public ArrayList getapplicators()引发SQLException{
ArrayList applicationList=null;
申请人=空;
连接conn=null;
Statement=null;
字符串查询=null;
结果集rs=null;
试一试{
conn=ConnectionManager.getConnection();
statement=conn.createStatement();
query=“从申请人中选择*”;
rs=语句执行(查询);
while(rs.next()){
if(applicationList==null){
applicationList=新的ArrayList();
}
申请人=新申请人();
申请人。设置申请人编号(rs.getInt(“申请人编号”);
申请人.setApplicationDate(rs.getString(“申请人日期”);
申请人名称(rs.getString(“名字”);
申请人名称(rs.getString(“姓氏”);
申请人.setmName(rs.getString(“中间名”);
申请人。设置(rs.getInt(“年龄”));
申请人:setGender(rs.getString(“性别”));
申请人.setEmail(rs.getString(“电子邮件地址”);
申请人。设置联系人号码(rs.getString(“联系人号码”);
申请人:setCity(rs.getString(“城市”);
申请人:setSchool(rs.getString(“学校”);
申请人。设置课程(rs.getString(“课程”));
申请人:Setyear毕业(rs.getInt(“本科毕业”);
申请人:setYearWorkExp(rs.getInt(“year\u work\u exp”);
申请人.setSourceChannel(rs.getString(“source_channel”);
申请人。设置状态id(rs.getInt(“状态id”);
申请人名单。添加(申请人);
}
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}最后{
如果(rs!=null),请尝试{rs.close();}catch(SQLException logOrIgnore){}
如果(statement!=null),请尝试{statement.close();}catch(SQLException logOrIgnore){}
如果(conn!=null),请尝试{conn.close();}catch(SQLException logOrIgnore){}
}
返回申请表;
}

在使用Hibernate将对象映射到关系数据库和Spring依赖项注入功能以支持连接和事务管理之后,您的示例看起来像一个完美的“之前”图片,显示了一个非常好看的“之后”图片。

我看到了巨大而突出的问题:

  • 刀是单体的,为什么
  • 你没有用。还有,为什么

  • 如果您实际使用的是JavaEE(正如问题标记的那样),而不是J2EE(这是一个围绕Java1.4构建的糟糕规范),那么DAO模式完全没有必要。
    EntityManager
    是新的DAO

    请看一下本文的前几个部分


    我们需要使用J2ee..:(

    好的,所以您仍然需要修复singleton实现。考虑到该对象不存储任何内部状态,没有理由只创建该对象的一个实例。有一个简单的修复方法:

    • 完全删除
      专用静态应用程序dao me=null;
    • getInstance()
      实现更改为

      public static ApplicantDAO getInstance() {
          return new ApplicantDAO();
      }
      
    其他气味:

    • 您几乎总是希望声明
      List
      s,而不是
      ArrayList
      s,因此更改如下声明

      public ArrayList<Applicant> getApplicants() throws SQLException
      // to
      public List<Applicant> getApplicants() throws SQLException
      
      // and
      
      ArrayList<Applicant> applicantList = null;
      // to
      List<Applicant> applicantList = null;
      
      public ArrayList getappenders()引发SQLException
      //到
      public List getapplicators()引发SQLException
      //及
      ArrayList应用程序列表=nu
      private static ApplicantDAO me = new ApplicantDAO();
      public static ApplicantDAO getInstance() { return me; }
      
      @Stateless
      public class JPAApplicantDAO implements ApplicantDAO {
      
          @PersistenceContext(unitName = "myPU")
          private EntityManager entityManager;
      
          @Override
          public Applicant getByID(Long applicantID) {
              return entityManager.find(Applicant.class, applicantID);
          }
      
          @Override
          public void update(Applicant applicant) {
              applicant.setLastModifiedDate(new Date());
              entityManager.merge(applicant);
          }
      
          @Override
          public void delete(Applicant applicant) {
              Applicant deletedApplicant = applicant;
              if (!entityManager.contains(applicant)) {
                  deletedApplicant = entityManager.merge(applicant);
              }
      
              entityManager.remove(deletedApplicant);
          }
      
          @Override
          public List<Applicant> getBySomethingID(Long somethingID) {
              return entityManager.createNamedQuery("Applicant.getBySomethingID", Applicant.class)
                                  .setParameter("somethingID", somethingID)
                                  .getResultList();
          }
      }
      
      @Named
      @RequestScoped
      public class SomeBean {
      
          @EJB
          private ApplicantDAO applicantDAO;
      
          public void someMethod() {
              applicantDAO.delete(applicant);
          }
      }