Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_pt
CreateManager_pt
LeftMenu




JSPWiki v2.2.33

[RSS]


Hide Menu

CreateDAO_pt


Difference between version 36 and version 18:

At line 178 changed 2 lines.
<div class="note" style="font-size: 12px">
<a name="appgen"></a>__The AppGen Tool__
%%note <a name="appgen"></a>__NOTE:__ AppFuse versions 1.6.1+ contain include an [AppGen] tool that can be used to generate all the classes for the rest of these tutorials. However, it's best that you go through these tutorials before using this tool - then you'll know what code it's generating.%%
At line 181 removed 17 lines.
As part of 1.6.1, I created an __AppGen__ tool that can be used to generate all the classes for the rest of these tutorials. This tool was based on contributions from [Lance Lavandowska|https://appfuse.dev.java.net/issues/show_bug.cgi?id=60] and [Ben Gill|https://appfuse.dev.java.net/issues/show_bug.cgi?id=78]. At first, I didn't want to add a code-generation feature like this b/c you'd end up with a 1-to-1 relationship between tables/pojos, DAOs and Managers. On most of my projects, I have far fewer DAOs and Managers than POJOs.
By default, AppGen will generate only Actions/Controllers, Action/Controller Tests, test data, i18n keys and JSPs. It will also configure Actions/Controllers for you. It uses the generic BaseManager and BaseDaoHibernate classes (configured as "manager" and "dao") to reduce the number of files that are generated. However, I realize that sometimes you will want to generate all the DAO and Manager classes (as well as their tests), so I've added that option too.
To use the AppGen tool (after installing your web framework), perform the following steps:
# Create your POJO (in the model directory) and <a href="#ref-CreateDAO-5">configure the mapping file</a> in applicationContext-hibernate.xml.
# cd into the ''extras/appgen'' directory and run "ant -Dobject.name=Person -Dappgen.type=pojo". In this example, the __Person__ class should exist in your "model" package. This generates all the files you create in the tutorials on this site (for your chosen web framework).
# To install the generated files, run "ant install". You can run "ant install -Dmodel.name=Person -Dmodel.name.lowercase=person" if you want to do everything in one fell swoop. __WARNING:__ You might want to backup your project before you do this - or at least make sure it's checked into a source code repository. I've tested this code, and I think it works well - but it ''is'' modifying your source tree for you.
The reason for the "lowercase" parameter is to rename the JSPs to begin with a lowercase letter. If I tried to rename them and change the filename programmatically, it took 1MB worth of BSF and Rhino JARs (+5 lines of code) and this just seemed easier. Speaking of JSPs - it's up to you to modify the *Form.jsp and make it look pretty. This is covered in Step 5 of each respective web framework's "Create Action/Controller" tutorial: <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=CreateActions#ref-CreateAction-5">Struts</a>, <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=SpringControllers#ref-SpringControllers-5">Spring</a> and <a href="http://raibledesigns.com/wiki/Wiki.jsp?page=WebWorkActions#ref-WebWorkActions-5">WebWork</a>.
__NOTE:__ If you'd like to generate all the DAOs/Managers/Tests, run "ant install-detailed" instead of "ant install". Before you install anything, the files will be created in the extras/appgen/build/gen directory (in case you want to look at them before installing). If you just want to test the tool, you can cd to this directory and run "ant test" to see the contents of these tutorials created.
I encourage you to read these tutorials even if you decide to generate all your code. That way you'll understand what's being generated for you and you'll only need to mailing list for [asking smart questions|http://www.catb.org/~esr/faqs/smart-questions.html]. ;-) Hopefully this tool will remove the pain of writing simple CRUD code and let you concentrate on developing your business logic and fancy UIs!</div>
At line 200 changed 1 line.
Para começar, crie uma classe PessoaDaoTest.java no diretório test/dao/**/dao. Esta classe deve estender [BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOTestCase.java.html], que já existe neste pacote. Esta classe pai é usada para carregar ApplicationContext do [Spring's|http://www.springframework.org] (Spring une todas as camadas), e para automaticamente carregar um arquivo .properties (ResourceBundle) que tem o mesmo nome que sua classe *Test.class. Neste exemplo, se você colocar um arquivo PessoaDAOTest.properties no mesmo diretório que PessoaDAOTest.java, este arquivo de propriedades estará disponível via uma variável “rb”.
Para começar, crie uma classe PessoaDaoTest.java no diretório test/dao/**/dao. Esta classe deve estender [BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOTestCase.java.html], sub-classe da classe [AbstractDependencyInjectionSpringContextTests|http://www.springframework.org/docs/api/org/springframework/test/AbstractDependencyInjectionSpringContextTests.html] do Spring que já existe neste pacote. Esta classe pai é usada para carregar ApplicationContext do [Spring's|http://www.springframework.org] (Spring une todas as camadas), e para (opcionalmente) carregar um arquivo .properties (ResourceBundle) que tem o mesmo nome que sua classe *Test.class. Neste exemplo, se você colocar um arquivo PessoaDAOTest.properties no mesmo diretório que PessoaDAOTest.java, este arquivo de propriedades estará disponível via uma variável “rb”.
At line 202 changed 1 line.
;:%%(color: blue)''Usualmente copio (abrir salvar como) um teste existente (ex.: UserDaoTest.java) e procurar/substituir [Uu]ser com [Pp]essoa, ou qualquer que seja o nome do meu objeto.''%%
;:%%(color: blue)''Usualmente copio (abrir salvar como) um teste existente (ex.: UserDaoTest.java) e procurar/substituir [[Uu]ser com [[Pp]essoa, ou qualquer que seja o nome do meu objeto.''%%
At line 208 removed 2 lines.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
At line 191 added 1 line.
import org.springframework.dao.DataAccessException;
At line 212 removed 1 line.
At line 215 removed 3 lines.
//~ Instance fields ========================================================
At line 221 changed 4 lines.
//~ Methods ================================================================
protected void setUp() {
log = LogFactory.getLog(PersonDaoTest.class);
dao = (PersonDao) ctx.getBean("personDao");
public void setPersonDao(PersonDao dao) {
this.dao = dao;
At line 226 removed 8 lines.
protected void tearDown() {
dao = null;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(PersonDaoTest.class);
}
At line 237 changed 1 line.
O código que você vê acima é o que precisamos para um teste JUnit básico, que inicializa e destrói nosso PessoaDAO. O objeto “ctx” é uma referência ao ApplicationContext do Spring, o qual é inicializado em um bloco estático da classe [BaseDaoTestCase's|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html] class.
O código que você vê acima é o que precisamos para um teste de integração Spring básico que inicializa e destrói nosso PessoaDAO. Spring irá usar autowiring (auto-ligamento) por tipo para chamar o método ''setPersonDao()'' e atribuir o bean "personDao" como uma dependência desta classe.
At line 225 added 1 line.
At line 266 changed 1 line.
assertTrue(person.getLastName().equals("Last Name Updated"));
assertEquals(person.getLastName(), "Last Name Updated");
At line 249 added 1 line.
At line 284 changed 1 line.
assertNull(dao.getPerson(person.getId()));
try {
person = dao.getPerson(person.getId());
fail("Person found in database");
} catch (DataAccessException dae) {
log.debug("Expected exception: " + dae.getMessage());
assertNotNull(dae);
}
At line 325 changed 1 line.
Primeiro, crie a interface PessoaDAO.java no diretório src/dao/**/dao e especifique os métodos CRUD básicos para qualquer classe de implementação. Eliminei as partes JavaDocs na classe abaixo apenas por propósito de visualização.''
Primeiro, crie a interface PessoaDAO.java no diretório src/dao/**/dao e especifique os métodos CRUD básicos para qualquer classe de implementação. Eliminei as partes JavaDocs na classe abaixo apenas por propósito de visualização.
At line 333 removed 2 lines.
import java.util.List;
At line 336 removed 3 lines.
public List getPeople(Person person);
At line 340 changed 3 lines.
public void savePerson(Object person);
public void savePerson(Person person);
At line 357 removed 3 lines.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
At line 328 added 1 line.
import org.springframework.orm.ObjectRetrievalFailureException;
At line 363 removed 2 lines.
import java.util.List;
At line 367 changed 1 line.
private Log log = LogFactory.getLog(PersonDaoHibernate.class);
public Person getPerson(Long id) {
Person person = (Person) getHibernateTemplate().get(Person.class, id);
At line 369 changed 3 lines.
public List getPeople(Person person) {
return getHibernateTemplate().find("from Person");
}
if (person == null) {
throw new ObjectRetrievalFailureException(Person.class, id);
}
At line 373 changed 2 lines.
public Person getPerson(Long id) {
return (Person) getHibernateTemplate().get(Person.class, id);
return person;
At line 377 changed 1 line.
public void savePerson(Object person) {
public void savePerson(Person person) {
At line 382 changed 2 lines.
Object person = getHibernateTemplate().load(Person.class, id);
getHibernateTemplate().delete(person);
// object must be loaded before it can be deleted
getHibernateTemplate().delete(getPerson(id));
At line 388 removed 21 lines.
Você notará aqui que não estamos fazendo nada com o parâmetro pessoa. Isto é somente um “place holder” por enquanto – no futuro você pode filtrar suas propriedades usando [Hibernate's Query Language|http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html] (HQL) ou usando [Criteria Queries|http://www.hibernate.org/hib_docs/reference/en/html/querycriteria.html].
''Um exemplo usando Criteria Query:''
[{Java2HtmlPlugin
Example example = Example.create(person)
.excludeZeroes() //exclude zero valued properties
.ignoreCase(); //perform case insensitive string comparisons
try {
return getSession().createCriteria(Person.class)
.add(example)
.list();
} catch (Exception e) {
throw new DAOException(e);
}
return new ArrayList();
}]
At line 422 removed 2 lines.
<value>org/appfuse/model/UserCookie.hbm.xml</value>
<value>org/appfuse/model/UserRole.hbm.xml</value>
At line 452 changed 1 line.
''Next Up:'' __Part II:__ [Creating new Managers|CreateManager] - A HowTo for creating [Business Delegates|http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html] that talk to the database tier (DAOs) and the web tier (Struts Actions).
''Próxima:'' __Parte II:__ [Criando novos Managers|CreateManager_pt] - Um "HowTo" para criar [Business Delegates|http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html] que comunicam-se com a camada de banco de dados (DAOs) e web (Spring Controllers).

Back to CreateDAO_pt, or to the Page History.