首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

上面是JAVA代码中学生信息DAO方法,帮忙详细说说每个函数是做什么的

2012-12-21 
下面是JAVA代码中学生信息DAO方法,帮忙详细说说每个函数是做什么的package cn.myexam.modelimport cn.mye

下面是JAVA代码中学生信息DAO方法,帮忙详细说说每个函数是做什么的
package cn.myexam.model;

import cn.myexam.hibernate.BaseHibernateDAO;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;


public class StudentDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(StudentDAO.class);


public static final String STU_ID = "stuId";
public static final String PASSWORD = "password";
public static final String NAME = "name";
public static final String SEX = "sex";
public static final String TEL = "tel";
public static final String ADDRESS = "address";
public static final String EMAIL = "email";
public static final String TYPE = "type";

    
    public boolean save(Student transientInstance) {
        log.debug("saving Student instance");
        boolean passed = false;
        try {
        Transaction tx = getSession().beginTransaction();
            getSession().save(transientInstance);
            tx.commit();
            log.debug("save successful");
            passed = true;
        } catch (RuntimeException re) {
            log.error("save failed", re);
            passed = false;
            throw re;
        }finally{
        closeSession();
        }
        return passed;
    }
    
public boolean delete(Student persistentInstance) {
        log.debug("deleting Student instance");
        boolean passed = false;
        try {
        Transaction tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
            log.debug("delete successful");
            passed = true;
        } catch (RuntimeException re) {
            log.error("delete failed", re);


            passed = false;
            throw re;
        }finally{
        closeSession();
        }
        return passed;
    }
    
    public Student findById( java.lang.Long id) {
        log.debug("getting Student instance with id: " + id);
        try {
            Student instance = (Student) getSession()
                    .get("cn.myexam.model.Student", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }finally{
        closeSession();
        }
    }
    
    
    public List findByExample(Student instance) {
        log.debug("finding Student instance by example");
        try {
            List results = getSession()
                    .createCriteria("cn.myexam.model.Student")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }finally{
        closeSession();
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Student instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Student as model where model." 


         + propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
 queryObject.setParameter(0, value);
 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }finally{
      closeSession();
      }
}

public List findByStuId(Object stuId) {
return findByProperty(STU_ID, stuId);
}

public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}

public List findByName(Object name) {
return findByProperty(NAME, name);
}

public List findBySex(Object sex) {
return findByProperty(SEX, sex);
}

public List findByTel(Object tel) {
return findByProperty(TEL, tel);
}

public List findByAddress(Object address) {
return findByProperty(ADDRESS, address);
}

public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}

public List findByType(Object type) {
return findByProperty(TYPE, type);
}

    public Student merge(Student detachedInstance) {
        log.debug("merging Student instance");
        try {
            Student result = (Student) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }finally{
        closeSession();
        }
    }

    public boolean attachDirty(Student instance) {
        log.debug("attaching dirty Student instance");
        boolean passed = false;
        try {
        Transaction tx = getSession().beginTransaction();
            getSession().saveOrUpdate(instance);
            tx.commit();            
            log.debug("attach successful");


            passed = true;
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            passed = false;
            throw re;
        }finally{
        closeSession();
        }
        return passed;
    }
    
    public void attachClean(Student instance) {
        log.debug("attaching clean Student instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }finally{
        closeSession();
        }
    }
}
[最优解释]
楼主,抽空还是自己去学习下吧,简单注释,希望能“稍微”有点帮助!

package cn.myexam.model;

import cn.myexam.hibernate.BaseHibernateDAO;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

/**
 * 此方法适用于针对Student类的数据库操作
 * 具体包括增删改查
 * @author Fancy
 * @date 2012-12-1
 */
public class StudentDAO extends BaseHibernateDAO {

private static final Log log = LogFactory.getLog(StudentDAO.class);

public static final String STU_ID = "stuId";
public static final String PASSWORD = "password";
public static final String NAME = "name";
public static final String SEX = "sex";
public static final String TEL = "tel";
public static final String ADDRESS = "address";
public static final String EMAIL = "email";
public static final String TYPE = "type";

/**
 * 此方法用于保存一条Student学生记录到数据库
 * 返回一个boolean值,如果保存成功返回true,如果保存失败返回false
 * @param transientInstance
 * @return
 */
public boolean save(Student transientInstance) {
log.debug("saving Student instance");//log对象调用一个debug方法,用于在控制台打印调试信息,此方法被调用的时候控制台就会打印 "saving Student instance"。
boolean passed = false;
try {
Transaction tx = getSession().beginTransaction();//生成一个事物对象,用于处理一次事物(也就是数据库操作)


getSession().save(transientInstance);//保存学生记录的具体操作方法(通过session会话调用)
tx.commit();//每次事物完成之后都要提交的一个方法
log.debug("save successful");
passed = true;
} catch (RuntimeException re) {
log.error("save failed", re);
passed = false;
throw re;
} finally {
closeSession();//关闭session会话
}
return passed;
}

/**
 * 删除一条学生记录
 * @param persistentInstance
 * @return
 */
public boolean delete(Student persistentInstance) {
log.debug("deleting Student instance");
boolean passed = false;
try {
Transaction tx = getSession().beginTransaction();
getSession().delete(persistentInstance);
tx.commit();
log.debug("delete successful");
passed = true;
} catch (RuntimeException re) {
log.error("delete failed", re);
passed = false;
throw re;
} finally {
closeSession();
}
return passed;
}

/**
 * 根据数据表Student的唯一标示ID,找到这条记录
 * @param id
 * @return
 */
public Student findById(java.lang.Long id) {
log.debug("getting Student instance with id: " + id);
try {
Student instance = (Student) getSession().get(
"cn.myexam.model.Student", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
} finally {
closeSession();
}
}

/**
 * 根据Student对象instance,找到一条学生记录
 * @param instance
 * @return
 */
public List findByExample(Student instance) {
log.debug("finding Student instance by example");
try {
List results = getSession()
.createCriteria("cn.myexam.model.Student")
.add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
} finally {
closeSession();
}
}

/**
 * 根据数据库表Student表里的一个字段,查找出符合条件的记录
 * 返回所有满足条件记录的一个集合List
 * @param propertyName字段名
 * @param value具体的值,作为条件
 * @return
 */
public List findByProperty(String propertyName, Object value) {
log.debug("finding Student instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Student as model where model."
+ propertyName + "= ?";//? 作为一个占位符,用value去替代
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);//0  表示sql语句中的第一个 问好?,  然后用value去替代?
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
} finally {
closeSession();
}
}

//根据id查询满足条件的记录


public List findByStuId(Object stuId) {
return findByProperty(STU_ID, stuId);
}

//根据密码查询满足条件的记录
public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}

public List findByName(Object name) {
return findByProperty(NAME, name);
}

public List findBySex(Object sex) {
return findByProperty(SEX, sex);
}

public List findByTel(Object tel) {
return findByProperty(TEL, tel);
}

public List findByAddress(Object address) {
return findByProperty(ADDRESS, address);
}

public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}

public List findByType(Object type) {
return findByProperty(TYPE, type);
}

public Student merge(Student detachedInstance) {
log.debug("merging Student instance");
try {
Student result = (Student) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
} finally {
closeSession();
}
}

public boolean attachDirty(Student instance) {
log.debug("attaching dirty Student instance");
boolean passed = false;
try {
Transaction tx = getSession().beginTransaction();
getSession().saveOrUpdate(instance);
tx.commit();
log.debug("attach successful");
passed = true;
} catch (RuntimeException re) {
log.error("attach failed", re);
passed = false;
throw re;
} finally {
closeSession();
}
return passed;
}

public void attachClean(Student instance) {
log.debug("attaching clean Student instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
} finally {
closeSession();
}
}
}


[其他解释]
/**
 *  将传入的detached状态的对象的属性复制到持久化对象中,并返回该持久化对象  
 * 如果该session中没有关联的持久化对象,加载一个,
 * 如果传入对象未保存,保存一个副本并作为持久对象返回,传入对象依然保持detached状态。
 * @param detachedInstance
 * @return
 */
public Student merge(Student detachedInstance) {
        log.debug("merging Student instance");
        try {
            Student result = (Student) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {


            log.error("merge failed", re);
            throw re;
        }finally{
         closeSession();
        }
    }

/**
 *  将传入的对象持久化并保存。  
 * 如果对象未保存(Transient状态),调用save方法保存。如果对象已保存(Detached状态),
 * 调用update方法将对象与Session重新关联。
 * @param instance
 * @return
 */
   public boolean attachDirty(Student instance) {
        log.debug("attaching dirty Student instance");
        boolean passed = false;
        try {
         Transaction tx = getSession().beginTransaction();
            getSession().saveOrUpdate(instance);//也就是:如果数据库有相关的记录,则更新这条记录,如果没有,就执行插入操作,新增一条记录
            tx.commit();            
            log.debug("attach successful");
            passed = true;
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            passed = false;
            throw re;
        }finally{
         closeSession();
        }
        return passed;
    }
    
   /**
    * 将传入的对象状态设置为Transient状态(注解状态,java关键字)
    * @param instance
    */
    public void attachClean(Student instance) {
        log.debug("attaching clean Student instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }finally{
         closeSession();
        }


[其他解释]
是我给的分太少了吗,为什么没人回复啊
[其他解释]
这些方法定义的名字都很清晰的啊,你这是从网上down下来的么?
[其他解释]
确实已经很清楚了,参数也有,慢慢看吧。


[其他解释]
大哥,我是刚学的,菜鸟,帮忙详细说说
[其他解释]
你这个demo是的确够详细的,虽然没注释。
LZ还是尽量找个有前后交互的小例子吧!
[其他解释]
增删改查,条件查,还有什么。
[其他解释]

引用:
大哥,我是刚学的,菜鸟,帮忙详细说说

这个还看不懂就是英语不行了,查字典吧,哥们!命名规则很规范的!
[其他解释]
上面的程序是用hibernate的。你看来连jdbc都不懂就开始接触hibernate了。请先去学习下jdbc再来看这些程序。而且hibernate还涉及到session开闭问题、手动提交事务、回滚事务……要解释,得费不少口舌。

你这是要交作业了临时抱佛脚吧?呵呵……
[其他解释]
是选修课的期末作业,稍微搞懂就行了,但我什么都不懂啊
[其他解释]
//这些都是数据库表Student的一些字段--静态变量
public static final String STU_ID = "stuId";
public static final String PASSWORD = "password";
public static final String NAME = "name";
public static final String SEX = "sex";
public static final String TEL = "tel";
public static final String ADDRESS = "address";
public static final String EMAIL = "email";
public static final String TYPE = "type";

[其他解释]
hibernate 开启事务  持久层对象的增删改查  提交事务  关闭session  捕获异常  打印日志文件
[其他解释]
   public Student merge(Student detachedInstance) {
        log.debug("merging Student instance");
        try {
            Student result = (Student) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }finally{
         closeSession();
        }
    }

    public boolean attachDirty(Student instance) {
        log.debug("attaching dirty Student instance");
        boolean passed = false;
        try {
         Transaction tx = getSession().beginTransaction();
            getSession().saveOrUpdate(instance);
            tx.commit();            


            log.debug("attach successful");
            passed = true;
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            passed = false;
            throw re;
        }finally{
         closeSession();
        }
        return passed;
    }
    
    public void attachClean(Student instance) {
        log.debug("attaching clean Student instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }finally{
         closeSession();
        }
这三个函数又是做什么用的,能不能像10楼那样详细说说
[其他解释]
[quote=引用:]
楼主,抽空还是自己去学习下吧,简单注释,希望能“稍微”有点帮助!


 public Student merge(Student detachedInstance) {
        log.debug("merging Student instance");
        try {
            Student result = (Student) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }finally{
         closeSession();
        }
    }

    public boolean attachDirty(Student instance) {
        log.debug("attaching dirty Student instance");
        boolean passed = false;
        try {
         Transaction tx = getSession().beginTransaction();
            getSession().saveOrUpdate(instance);


            tx.commit();            
            log.debug("attach successful");
            passed = true;
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            passed = false;
            throw re;
        }finally{
         closeSession();
        }
        return passed;
    }
    
    public void attachClean(Student instance) {
        log.debug("attaching clean Student instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }finally{
         closeSession();
        }
这三个函数又是做什么用的,请问10楼能不能在详细说说 
[其他解释]
[quote=引用:
大哥,解释的太专业了,能不能用通俗的语言解释一下
[其他解释]
hibernate对象的状态以及转换规则-----

http://blog.csdn.net/baininghan/article/details/8253134

[其他解释]
1.兄弟是否了解update();
2.http://www.cnblogs.com/hyteddy/archive/2011/05/10/2041762.html这里有关于update()和merge()的解释!
3.了解hibernate是如何持久化对象的。它的几个状态!

热点排行