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

myeclpise 自动生成的DAO类应该怎么使用

2012-01-26 
myeclpise 自动生成的DAO类应该如何使用啊用myeclipsehibernate3.1可以自动生成dao类,但是dao类的方法不是

myeclpise 自动生成的DAO类应该如何使用啊
用myeclipse   hibernate3.1可以自动生成dao类,但是dao类的方法不是静态的,应该如何来调用这些方法呢?

代码如下

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.criterion.Example;

/**
  *   Data   access   object   (DAO)   for   domain   model   class   ArtSpecialty.
  *  
  *   @see   edu.njut.art.hibernate.ArtSpecialty
  *   @author   MyEclipse   Persistence   Tools
  */

public   class   ArtSpecialtyDAO   extends   BaseHibernateDAO   {
private   static   final   Log   log   =   LogFactory.getLog(ArtSpecialtyDAO.class);


 
//   property   constants
public   static   final   String   SP_NAME   =   "spName ";

 
public   void   save(ArtSpecialty   transientInstance)   {
log.debug( "saving   ArtSpecialty   instance ");
try   {
getSession().save(transientInstance);
log.debug( "save   successful ");
}   catch   (RuntimeException   re)   {
log.error( "save   failed ",   re);
throw   re;
}
}

public   void   delete(ArtSpecialty   persistentInstance)   {
log.debug( "deleting   ArtSpecialty   instance ");
try   {
getSession().delete(persistentInstance);
log.debug( "delete   successful ");
}   catch   (RuntimeException   re)   {
log.error( "delete   failed ",   re);
throw   re;
}
}

public   ArtSpecialty   findById(java.lang.String   id)   {
log.debug( "getting   ArtSpecialty   instance   with   id:   "   +   id);
try   {
ArtSpecialty   instance   =   (ArtSpecialty)   getSession().get(
"edu.njut.art.hibernate.ArtSpecialty ",   id);
return   instance;
}   catch   (RuntimeException   re)   {
log.error( "get   failed ",   re);
throw   re;
}
}

public   List   findByExample(ArtSpecialty   instance)   {
log.debug( "finding   ArtSpecialty   instance   by   example ");
try   {
List   results   =   getSession().createCriteria(
"edu.njut.art.hibernate.ArtSpecialty ").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;
}
}

public   List   findByProperty(String   propertyName,   Object   value)   {
log.debug( "finding   ArtSpecialty   instance   with   property:   "
+   propertyName   +   ",   value:   "   +   value);
try   {
String   queryString   =   "from   ArtSpecialty   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;
}
}

public   List   findBySpName(Object   spName)   {
return   findByProperty(SP_NAME,   spName);
}

 
public   List   findAll()   {
log.debug( "finding   all   ArtSpecialty   instances ");
try   {
String   queryString   =   "from   ArtSpecialty ";
Query   queryObject   =   getSession().createQuery(queryString);
return   queryObject.list();
}   catch   (RuntimeException   re)   {
log.error( "find   all   failed ",   re);
throw   re;
}
}

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

public   void   attachDirty(ArtSpecialty   instance)   {
log.debug( "attaching   dirty   ArtSpecialty   instance ");
try   {
getSession().saveOrUpdate(instance);
log.debug( "attach   successful ");
}   catch   (RuntimeException   re)   {
log.error( "attach   failed ",   re);
throw   re;
}
}

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

[解决办法]
你可以利用DAO类通过hibernate访问、操作对象,进而实现对数据库的操作,包括CURD。
[解决办法]
你可以改写生成的DAO,按照自己的需求,这样不更好。。
[解决办法]
生成是只是一些基本的数据库操作要有别的再自己加上就好

[解决办法]
这样说把,struts你可以认为分成几个层次,页面层也就是jsp,逻辑层用action写,然后是业务处理层用javaBean,这里边说的是task.最后就是你说的dao了,用来进行和数据库之间的连接,查找插入删除修改数据库等.在你需要数据库的时候使用相应的dao旧可以了,然后在把数据传如task中进行处理
[解决办法]
刚好我这两天在看,为后来人留一条明路吧。
package com.test.Hibernate;

import java.util.Iterator;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import junit.framework.Assert;
import junit.framework.TestCase;

public class TestUser extends TestCase {



private Users user = null;

String[] path = null;

ApplicationContext ctx = null;

UsersDAO dao = null;

Long id = new Long(2);

protected void setUp() throws Exception {
super.setUp();
String[] path = { "/WebRoot/WEB-INF/applicationContext.xml " };
ApplicationContext ctx = new FileSystemXmlApplicationContext(path);
dao = (UsersDAO) ctx.getBean( "UsersDAO ");
}

protected void tearDown() throws Exception {
super.tearDown();

}

public void testFindById() {

try {
user = dao.findById(id);
System.out.print(user.getUsername());
Assert.assertEquals(user.getUsername(), "fenix ");
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}

}

热点排行