Está en la página 1de 36

Cero

con Spring e Hibernate

Nicols Cornaglia

http://nikofactory.blogspot.com
2004

Contenidos
1

Introduccin

Preparacin del entorno


2.1
Directorios
2.2
Instalacin SDK
2.3
Instalacin Eclipse

El proyecto 1 parte
3.1
Anlisis
3.2
Creacin del proyecto en eclipse
3.3
Un aproximacin a la arquitectura
3.4
Test driven development
3.4.1
Anlisis
3.4.2
JUnit
3.4.3
El primer caso de prueba
3.5
Cosmtica
3.5.1
Fonts
3.5.2
Tabs
3.5.3
JavaDocs
3.6
Mas desarrollo
3.6.1
Refinamiento de Project
3.6.2
Mas clases

Persistencia
4.1
Introduccin a Hibernate
4.2
Xdoclet <doing>
4.3
Ant <todo>
4.4
MySQL <todo>
4.5
Test Cases <todo>
4.5.1
DBUnit <todo>

Uniendo todo con Spring


5.1
Inyeccin de dependencia
5.2
Spring <todo>
5.3
El Contexto <todo>

Coverage
6.1.1
Emma <todo>

Documentacin
7.1.1
Javadoc <todo>
7.1.2
java2html <todo>

ACEGI

CVS

9.1

WinCVS <todo>

10 Continuous Integration <todo>


11 El proyecto 2 parte (Web)
11.1 Tomcat <todo>
11.2 El proyecto <todo>
11.3 Spring MVC <todo>
11.4 Sitemesh <todo>
11.5 Test Cases <todo>
11.5.1 jWebUnit <todo>

1 Introduccin

!"

$ % #%

&

#%

#
!
+

./ "/ 0#
1
&

#
#
,

#
*

)
!"

#,
5
#

,
!

'

#
!

*
2
,
) !
6

" 7
-

#
.
&

"

0#

"

+
#
/ 9
!
%
:http://raibledesigns.com/page/rd;#
(
% + 6
:http://raibledesigns.com/wiki/Wiki.jsp?page=AppFuse; 5
<%
:http://www.springlive.com;*

!" *
# 8
*
-#

2 Preparacin del entorno


5

,
)#

"

#
<

,
2>4 2*

2.1 Directorios
<

!
%

!
, )

:" 7

,
*

work\

-#

work\

(
#
: 2

C:\work\*

:
;

;#
:;

:;

;*

2.2 Instalacin SDK


1
, :
$% * 1
* * 5@A0#

) ) )
"
5
&

# ,
& ,
(
- . !!
!
*
(
* 2# (
% -

,
4
(

5@A
work\jdks#

5@A* 5 )
!

%
%

%JAVA_HOME%\bin

*8

% #

work\jdks\j2sdk1.4.2_05*

!
&

5@A
.$ 5
work\downloads* B
%

% 0# ,

#
,

%
E

;
:http://java.sun.com/;#
!
C
# , )

- * *
/

$%
!

D
,
*

JAVA_HOME=work\jdks\j2sdk1.4.2_05

PATH*

2.3 Instalacin Eclipse


F@
(
:http://www.jetbrains.com/idea/;
%
*

:http://www.eclipse.org;# , F
:http://www.netbeans.org/;

$F@ +

<

" #

work\apps\eclipse#

.-data

eclipse.exe#

work\workspace0#

*
5 (

#%

, %

? Resources*

3 El proyecto 1 parte
4
F
"
: 2
!

!
5
<

<

%
,

*
-

!
H1

!"
1

;*

:http://www.extremeprogramming.org;#
!
4 5
:F
1
;
,
! :9
1
;*
,
%
*
!
#
,
4 5
,
%
!
*
'
4 5
:)
,
.
0;#
#
!
*
.%
0#&
%
*
4 5
#
#
*I
'
#
# %
*
(
#
)
) ) D
D
#
%
DJ K JDL D* 1
,
-#
D*

3.1 Anlisis
E %
H1#
!
?
5
5
5
5

% #

H1

G
! )
G
!*
G
G
*G

*
!

- :
-*5
*5
* G

;* 5 ,
-

! " *
# ! )

!
)
#

#
#

! ) *

,
#

-#

*
5 D+
5
5

#
-

+
5

-*4

- )

*
*4

#
,

*
#
*

% # !
)

!
!

# ,

*
1
)
H1#
!

*@

-*1
#
)
#

,
,

*I
,

3.2 Creacin del proyecto en eclipse


M
E

,
%

Java#

#,
!
'

*
!

Resources

? File/New/Project/Java
!

mwm?

src

Project*

bin?

, !

-#

Java?

3.3 Un aproximacin a la arquitectura


5

" :5 ;#,
,
# * :5 #5 #5 # *;# ( ,
?
:5 ;
:5 ;

) #
:5 ;

- :5 ;
:5 ;*
,

G
1
F
4

(%

,
*

! " *
*

3.4 Test driven development


#

public class Project{}

%
!"

? E@@ E

2
%

!
!
:http://www.testdriven.com;*
!
#E@@
:
;#
:
;
:
*

!
-

!
?

3.4.1 Anlisis
NO
O

:5 ;P
( ,
(
*I,
0 .! )
!
- "

!
)
5

3 0#.! )
)
!
#,

!
#)

- 0*
#

Project#

*
1

#)

,
)

, !

*1
#

$4 *

3.4.2 JUnit
$4

:) ?>>"""*(
- #,
%

(
(
/ )

;
( ,

$%
!
! 2- # 2

*/
&

3
F@

#
test*

'

#2
*
%
$4 #
$% *
TestCase#
,
)
&
#
.+
0
:assertNull()#AssertEquals()* E
&
.5
0#
(
!
(
'*

3.4.3 El primer caso de prueba


5
+

#
*
%

,
#

!
,
'File/New/Source Folder?

test

&

derecho/Properties/Java Build Path#


Path

test

src*

'File/New/JUnit
,

build path

<

Yes#

botn
Java Build

(
?

Test Case

*(

&

Source Folder

ProjectTest*

com.mwm.model

&

setUp()

test#

& )

tearDown()#

<

Finish#

7
,
*
%

com\mwm\model\ProjectTest.java

/*
* Created on Oct 14, 2004
*

Java

* TODO To change the template for this generated file go to


* Window - Preferences - Java - Code Style - Code Templates
*/
package com.mwm.model;
import junit.framework.TestCase;
/**
* @author ncornag
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ProjectTest extends TestCase {
/**
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
}

Project#

< ,
(
.
<

)
Project#
&
0
&
#
!

&
-

*
&
*E
&

setUp()#

&
)

&
% )
) )

(
&

&
!

testNewProject()#

!
Project*

protected void setUp() throws Exception {


super.setUp();
project = new Project();
}
protected void tearDown() throws Exception {
super.tearDown();

tearDown()

public class ProjectTest extends TestCase {


private Project project;

project = null;

;?

}
public void testNewProject() throws Exception {
project.setName("Mi primer proyecto");
project.setStartDate(new Date(0));
project.setEndDate(new Date(0));
assertEquals("Name", "Mi primer proyecto", project.getName());
assertNotNull("StartDate", project.getStartDate());
assertNotNull("EndDate", project.getEndDate());
}
}

< ,

&

testNewProject()#

&

Project

#
1

Object arg1, Object arg2)

%
(
! ) #

#
$4 #

&
#

assertEquals(String arg0,

assertNotNull(String arg0, Object arg1)#

(
&

*
0! #

&

,
* +)

-*

*
!

5
1

setUp()#

#
!

.
%
,

0 )
#

*1
&

Project#

5 *
-

:File/New/Class;#
?

/*
* Created on Oct 14, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.mwm.model;
import java.util.Date;
/**
* @author ncornag
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Project {
//~ Properties =============================================================
private String name;
private Date startDate;
private Date endDate;

//~ Getter/Setters =========================================================


/**
* @return Returns the endDate.
*/
public Date getEndDate() {
return endDate;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @return Returns the startDate.
*/
public Date getStartDate() {
return startDate;
}
/**
* @param endDate The endDate to set.
*/
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @param startDate The startDate to set.
*/
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
}

)
com.mwm.model

!
5

# , )
7

%
,

src;
*

,
%
$
:

!
)#
#
%

#
% ,

;?

ProjectTest

'Run/Junit
) !

Test*

5
:
,

,
;

>
# )

>
%

&

,
!

&

.
E@@

0!

*5
$4

3.5 Cosmtica
+

% )
#

!
,

3.5.1 Fonts
1

)
#

%
# !

3.5.2 Tabs
<
%

!
-

# ,
!

Insert spaces for tab?

% #

#
*

3.5.3 JavaDocs
< $% @
%

!
,

#
&

*< , )

Window/Preferences/Java/Code Style/Code Templates/Comments?

1
2

:Types;#
-

(% #

.html#

(%

@author#:

*E

? <a href="mailto:nombre@dominio.com">Nombre</a>;#
GM5 :
;*

&
-

!
?

-#
#

4
'
&
/**
* ${see_to_overridden}
*/

#
(%
2 #
*
:Overriding

*
&
:
methods;

)%
?

:Code/New

Java Files;

${package_declaration}
${typecomment}
${type_declaration}

'

Catch block body

?
log.error("Error: " + ${exception_var}.getMessage());

Log4J

1 '

!
ProjectTest :Alt+Shift+J

Project

&
;#
:Alt+Shift+F

;*

&
-

!)

;*

3.6 Mas desarrollo


&
! :E@@;#
* AF55

.A

F5

:AF55;,
%
'
;

0:/

3.6.1 Refinamiento de Project


G

)
1 (

);
#

,
!

!
*1

#
,
)
&
,
Project*
&
#
,

)
?

* F

,
I+QBF
%

F0:B
* +
* 9%

)
H1* I+QBF
;

, ,

#
%

&

I+QBF

! ,

Project#

&

*
)
%

%
- #

#
.I

RQ

&
! -

B
%

-#
&

company

-*

3
*I

,
)

Project?

description

private String description;


private Company company;

#
)
1

%
,

*
'

Company#,
package com.mwm.model;

#
)

%
%

/**
* <p>
* <a href="Company.java.html"><i>View Source</i></a>
* </p>
*
* @author Nicolas Cornaglia
* @version $Revision:$ $Date:$
*/
public class Company {
//~ Properties ===========================================================
private String name;
//~ Getters/Setters ======================================================
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
}

3.6.2 Mas clases


5 '
**
! )
G

5
*

!*
#

#
&

ProjectTest?

/**
* Test put & remove a Iteration
* @throws Exception
*/
public void testPutRemoveIteration() throws Exception {
Long id = new Long(1);
Iteration iteration = new Iteration();
iteration.setId(id);
project.addIteration(iteration);
assertEquals("Iteration", iteration, project.getIteration(id));
assertEquals("Iteration project", project,
project.getIteration(id).getProject());
project.removeIteration(iteration);
assertNull("Iteration not removed", project.getIteration(id));
}

# ,
Project
removeIteration()*

,
3 #

&
& )
%

Iteration#

addIteration()#getIteration()

!
*

-#

, )

Iteration?

public class Iteration {


//~ Properties ===========================================================
private Long id;
private String name;
private String description;
private Project project;
private Date startDate;
private Date endDate;
//~ Getter/Setters =======================================================
.
.
.
}

&

Project

;#

private Map iterations = new HashMap();


public Iteration getIteration(Long id) {
return null;
}
public void addIteration(Iteration iteration) {
}
public void removeIteration(Iteration iteration) {
}

&

, )
% %

getIteration(id)

, $4

null#)

D ? junit.framework.AssertionFailedError:

Iteration expected:

com.mwm.model.Iteration@3ee284 but was: <null>#

assertEquals("Iteration", iteration, project.getIteration(id));

) !
8

*
,

&

' ?

/**
* @param id
* @return
*/
public Iteration getIteration(Long id) {
return (Iteration)iterations.get(id);
}
/**
* @param key
* @param iteration
*/
public void addIteration(Iteration iteration) {
iteration.setProject(this);
iterations.put(iteration.getId(), iteration);
}
/**
* @param key
*/
public void removeIteration(Iteration iteration) {
iterations.remove(iteration.getId());
}

&
,

% # )
, !

4 Persistencia
4.1 Introduccin a Hibernate
<
*5

,
-

,
!

Project?

package com.mwm.model;
public class Project {
private Long id;
private String name;
private Company company;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}

?
, )
!
#
%
%

#
Long#

)
*
,

>
!

$% C

!
, 8

Constructor.newInstance()*

#)
!

,
#

$C

*1

,
,

#Company#

package com.mwm.model;
public class Company {
private Long id;

!
#

ProjectTest*

<

Company

private String name;


public Long getId() {
return id;
}
public void setId() {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

NG

, 8
;
!
#
! ) H/ <:H/ <
)

P@ !
!)

H/ <
!
; !

Project

Project

:
!

Session

Transaction

classpath?

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.mwm.model.Project"
table="project">
<id
name="id"
column="id">
<generator class="native"/>
</id>
<property
name="name"
column="name"/>
<many-to-one
name="company"
cascade="all"
column="company_id"/>
</class>
</hibernate-mapping>

Company

,
project

Project.hbm.xml

Company.hbm.xml?

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.mwm.model.Company"

table="company">
<id
name="id"
column="id">
<generator class="native"/>
</id>
<property
name="name"
column="name"/>
</class>
</hibernate-mapping>

2
2?

import
import
import
import

!)

classpath

hibernate.properties

;#

net.sf.hibernate.Session;
net.sf.hibernate.SessionFactory;
net.sf.hibernate.Transaction;
net.sf.hibernate.cfg.Configuration;

...
Configuration cfg = new Configuration();
cfg.addResource("Project.hbm.xml");
cfg.addResource("Company.hbm.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Company company = new Company();
company.setName("Mi empresa");
Project project = new Project();
project.setName("MwM");
project.setCompany(company);
s.save(project);
tx.commit();
s.close();

5O<?

insert into company (id, name)


values (1, Mi empresa);
insert into project (id, name, id_company)
values (1, MwM, 1);

% #

Project

& #
Company*

4.2 Xdoclet <doing>


4

#
8

*1

H@

:) ?>>2
G

!
-

>2

>;#

)
*

!
#

T
(

,
(% #

/**
* <p>
* <a href="Project.java.html"> <i>View Source </i> </a>
* </p>
*
* @author Nicolas Cornaglia
* @version $Revision:$ $Date:$
*
* @hibernate.class table="project"
*/
public class Project {
@hibernate.class

#
:

H@

Project.java#(

,
;#

Project.hbm.xml?
<class
name="com.mwm.model.Project"
table="project"
dynamic-update="false"
dynamic-insert="false"
>

< T

@hibernate.class.

,
!
#
:table=project;
T
H@
%

& %

(
!

*
!

H@
,

generator-class="native"

id

getIteration()#,

@hibernate.id column="id"

null

?
!

id

? unsaved-value="null"*
/**
* @param id
* @return
*
* @hibernate.id column="id" generator-class="native" unsaved-value="null"
*/
public Iteration getIteration(Long id) {
return (Iteration) iterations.get(id);
}

<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"

%
?

(
#

>
<generator class="native">
</generator>
</id>

+
)

name#

@hibernate.property

description#startDate

endDate?

/**
* @return Returns the name.
*
* @hibernate.property not-null="true"
*/
public String getName() {
return name;
}

?
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="name"
not-null="true"
/>

, ) ,
# %
1
one# ,

name

name

null*

#
%

@hibernate.many-to-

/**
* @return Returns the company.
*
* @hibernate.many-to-one column="company_id" not-null="true"
*
class="com.mwm.model.Company"
*
outer-join="true"
*/
public Company getCompany() {
return company;
}

?
<many-to-one
name="company"
class="com.mwm.model.Company"
cascade="none"
outer-join="true"
update="true"
insert="true"
access="property"
column="company_id"
not-null="true"
/>

'

iterations?
/**
* @return Returns the iterations.
*
* @hibernate.map name="iterations" inverse="true" lazy="false"
*
cascade="all-delete-orphan"
* @hibernate.collection-one-to-many class="com.mwm.model.Iteration"
* @hibernate.collection-key column="project_id"
* @hibernate.collection-index column="id" type="long"
*/
public Map getIterations() {
return iterations;
}

,
-

@hibernate.map#,

&

lazy="false"#,
cascade="all-delete-orphan"#,

inverse="true"#,

@hibernate.collection-one-to-many#,

type="long"

!
-

#
)

Map

@hibernate.collection-index column="id"

#
id

@hibernate.collection-

key column="project_id"*
<map
name="iterations"
lazy="false"
sort="unsorted"
outer-join="auto"
inverse="true"
cascade="all-delete-orphan"
>
<key
column="project_id"
>
</key>
<index
column="id"
type="long"
/>
<one-to-many
class="com.mwm.model.Iteration"
/>
</map>

Iteration*

? Company

4.3 Ant <todo>


4

4.4 MySQL <todo>


@

/ 5O< *2

) ?>>"""*

4.5 Test Cases <todo>


4.5.1 DBUnit <todo>

,*

" 7U

5 Uniendo todo con Spring


5.1 Inyeccin de dependencia
F

#
&
-;
(

:
-

)
)

8
,

!
,
-*

!%
2

#
#) %
,
*
$
C
!
,

!
+,

C#
. %

0#

%
&

#)
'

,
-#

;*
!

&
(

5
$
1

:
1

,
*< !
C* 1

&
*

!
V @%
) #F *;#
% -

*
: 2

" )

$CV #9

$)

S
8

S V
#=
!

?
Inve rsi n de Co n t ro l
El framework es responsable del ciclo
de vida del objeto

Bs que da de
De pe n de nc ias

Inyecci n de
De pe n de nc ias

El objeto implementa una API


especfica del contenedor

No hay dependencias a una API


especfica del contenedor

Inyecci n po r me di o
de M u t a do res

Inyecci n po r me di o
de Co ns t r uc t o res

Los objetos son asignados mediante


propiedades JavaB ean

Los objetos son asignados mediante


argumentos de constructores

5.2 Spring <todo>


5.3 El Contexto <todo>

6 Coverage
6.1.1 Emma <todo>

"

7 Documentacin
7.1.1 Javadoc <todo>
7.1.2 java2html <todo>

$ %

&

8 ACEGI
1 , )

'

9 CVS
9.1 WinCVS <todo>

10 Continuous Integration <todo>

,- ./

11 El proyecto 2 parte (Web)


11.1 Tomcat <todo>
@
E
D*2 http://jakarta.apache.org/tomcat#
work\apps\tomcat

11.2 El proyecto <todo>

/ MG
5
#5

#=

" 7

11.3 Spring MVC <todo>


11.4 Sitemesh <todo>
11.5 Test Cases <todo>
11.5.1

jWebUnit <todo>

También podría gustarte