Frameworks
- Hibernate -
ORM framework
(Object to
relational
mapping) DB
- Spring - ORM,
Service level
architecture,
MVC, DI
- Struts - MVC
JDBC
example of POJO
class
package
com.pga;
public class Student
{
private int id;
private String
name;
private float
marks;
public Student()
{}
public int getId(
) { return this.id; }
public void
setId(int id) { this.id =
id; }
public String
getName( ) { return
this.name; }
public void
setName(String name)
{ this.name = name; }
public float
getMarks( ) { return
this.marks; }
public void
setMarks(String marks)
{ this.marks = marks; }
}
Hibernate Dialect
(Component of
Hibernate which will
convert object
oriented query into
SQL query with the
help of JDBC Driver)
http://hibernate.o
rg/orm/download
s/
Hibernate-
folder/project/hib
ernate-
ehcache/src/test/
resouces/hiberna
te-
config/domain/Ev
ent.hbm.xml
/
protject/documen
tation/src/main/d
ocbook/integrati
onGuide/en-
us/extras/hiberna
te.cfg.xml
HCQL -
Hibernate
Criteria Query
Language
Hibernate
application using
annotation :
- Instead of defining
mapping in XML file,
Hibernate provides
some
annotations(@Entity,@T
able,@Id,@Column) to
write same mapping in
POJO class.
- In configuration
file, add mapping as
below:
<mapping
class="com.pga.Student"
/>
-
https://maven.apache.org/
download.cgi
Struts 2 MVC:
The model contains
the business logic and
interact with the
persistance storage to
store, retrive and
manipulate data.
The view is
responsible for
dispalying the results
back to the user. In
Struts the view layer is
implemented using JSP.
The controller
handles all the request
from the user and
selects the appropriate
action.
user.
• The action class process the request
http://struts.apache.org/
download.cgi
FilterDispatcher
<filter>
<filter-
name>struts2</filter-name>
<filter-class>
org.apache.struts2.d
ispatcher.FilterDispa
tcher
</filter-class>
</filter>
<filter-mapping>
<filter-
name>struts2</filter-name>
<url-pattern>/*</url-
pattern>
</filter-mapping>
StrutsPrepareAndExe
cuteFilter
Entry of controller in
web.xml file:
<filter>
<filter-
name>struts2</filter-
name>
<filter-class>
org.apache.strut
s2.dispatcher.ng.
filter.StrutsPrepa
reAndExecuteFilt
er
</filter-class>
</filter>
<filter-mapping>
<filter-
name>struts2</filter-
name>
<url-
pattern>/*</url-
pattern>
</filter-mapping>
which of following
component plays role of
Controller in Struts MVC?
1. JSP 2. Servlet 3.
Filter
:
-How to define action
and it’s mapping in
struts.xml?
<action name=“s
earchStudent” class=“c
om.pga.SearchStudentH
andler”>
</action>
In this example,
request(action)
“searchStudent” is
mapped to action
class/model
SearchStudentHandler.
</action>
In this, whenever
model/action class
SearchStudnetHanlder
returns result name as
“success”, then that
result will be be passed
to view “display.jsp”.
//constructor
//getter,setter methods
public class
LoginAction implements
Action {
public String execut
e(){
return "success"; /
/returning result name
}
}
3. By extending
ActionSupport class
4. Annotating class with
@Action, @Actions,
@Result
<package name="user"
namespace="/User"
extends="strutsdefault">
<action name="home">
<result>/login.jsp</result>
</action>
<action name="login"
class="com.journaldev.struts2.a
ction.LoginAction">
<result
name="SUCCESS">/welcome.jsp</re
sult>
<result
name="ERROR">/error.jsp</result
>
</action>
</package>
- Specify
annotations in
Action class as
below:
@Action(value="welcome")
@ResultPath(value="/")
@Result(name="success",
location="sayWelcome.jsp")
http://localhos
t:8080/Struts
Annotation/ho
me.jsp
http://localhost:8
080/StrutsAnnot
ation/welcome.a
ction
Welcome PGA j2ee
Spring
framework:
Download Spring lib:
http://repo.spring.io/rele
ase/org/springframewor
k/spring/4.3.6.RELEASE/
OR
Use build tool Maven with
following entry in pom.xml file:
<dependencies>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-
context</artifactId>
<version>4.3.6.RELEASE</versi
on>
</dependency>
</dependencies>
Spring Modules:
1. Spring Core
module :
- IoC (Inversion of
Control) OR
DI(Dependency
Injection)
- Beans
- Core, Context
- Spring +
JDBCTemplate
- Spring + ORM
(Hibernate)
4. Spring AOP(Aspect
Oriented
programming)
By default beans
declared in
configuration file are
created in advance
whenever spring
framework is
initialized(Eager
initialisation)
If we want some
beans to be
instantiated only when
Java program call
getBean( ), then we
have to use an
attribute lazy-
init="true" in bean
declaration as below.
<bean id="date"
class="java.util.Date" lazy-
init="true”/>
<bean id="stud"
class="com.pga.Student"
lazy-init="true”>
<property
name="rollNo" value="5"
/>
<property
name="name"
value="Fred" />
<property
name="marks" value="80"
/>
</bean>
Dependency
Injection:
- DI is used when we
have two classes and
there is dependency
between them.
- There are two ways
to achieve
dependency
injection:
1. Setter based
dependency injection
2. Constructor
based dependency
injection
Example of Setter
based DI:
Injecting Collection:
set, list, map, props
class Emp {
int id;
String name;
Set<String>
technologies;
}
<bean id=“emp”
class=“com.pga.Emp
”>
<property
name=“id”
value=“5”/>
<property
name=“name”
value=“John”/>
</bean>
Injecting array of
objects:
Bean Inheritance
Bean post
processors:
We can write a Java
class which
implements interface
BeanPostProcessor with
2 methods as below:
Module 2: Spring
DAO(Data access
object) - Interacting
with DBMS
- Using
JdbcTemplate
(Spring +
JdbcTemplate)
- Using ORM (Spring
+ Hibernate)
- JDBCTemplate is
dependent on
DataSource.
- Data source is
cerated by specifying
all DBMS related
properties(username,
password,driver,url)
Steps for writing
Spring +
JDBCTemplate
application:
- Add required libs
(lib required for core
+ jdbc + tx + JDBC
driver file)
- Define POJO class
(Class with fields
matches to table
fields)
- Define Dao
class(e.g.
StudentDao) with
“JdbcTemplate”
object as member of
class. Add setter
method for
JdbcTemplate.
- Define all DB
related methods
(add,delete,update,se
lect) in Dao class
- Define following
beans in beans.xml
file 1. DataSource
bean with properties
“username,password,
driverClassName,url”
.
(DriverManagerDataS
ource is one of the
DataSource)
2. JdbcTemplate
bean which depends
on DataSource
3. Dao bean
(StudentDao) which
depends on
JdbcTemplate
- Declare demo
class which initlizes
spring framework
and obtain Dao bean
to call some
method(add/update/d
el/select)
NamedParameterJdbc
Template Demo:
1. Defining EmpDao
interface
hibernate.cfg.xml
- JDBC related
properties
(Drivername,user,pw
d,url)
- dialect, list of
mapping files
xmlns:tx="http://www.springfra
mework.org/schema/tx"
http://www.springframework.or
g/schema/tx
<beans
xmlns="http://www.springframe
work.org/schema/beans"
xmlns:xsi="http://www.w3.org/
2001/XMLSchema-instance"
xmlns:tx="http://www.springfra
mework.org/schema/tx"
xsi:schemaLocation="http://ww
w.springframework.org/schema/
beans
http://www.springframework.or
g/schema/beans/spring-beans-
3.0.xsd
http://www.springframework.or
g/schema/tx
http://www.springframework.or
g/schema/tx/spring-tx-
2.0.xsd">
<tx:annotation-driven/>
Spring4 + Hibernate5
(ORM)
- What all libraries are
needed?
- Define following
beans in beans.xml
file.
1. DataSource bean
(DriverManagerDataSource
) with 4
properties(username,pass
word,url,driverClassName)
2. Define
LocalSessionFactoryBean(
org.springframework.orm.hiber
nate5.LocalSessionFactoryBean
)
with following
properties:
- dataSource(We
already created in first
step, give it’s reference)
-
hibernateProperties
(Property collection with
fields hibernate.dialect,
hibernate.show_sql)
- mappingResources
(Hibernate mapping .hbm
files)
3. Define Dao bean by
giving reference of
sessionFactory created
above.
4. Declare
TransactionManager bean
by given dependency on
sessionFactory as below
<bean
id="transactionManager"
class="org.springframework.or
m.hibernate5.HibernateTransac
tionManager">
<property
name="sessionFactory"
ref="mySessionFactory" />
5. Declare
<tx:annotation-driven/>
tag.
Working example of
beans.xml:
If we specify dependancy
using @Atuowired and if
that bean is not exist
then creation of bean will
failed due to lack of
dependent bean.
Now what if we want to
continue creating bean
even if dependency is not
resolved then you can
auto-wire using following:
@Autowired(required=false)
Bean init-method and
destroy-method
attributes:
init-method : We can
specify method name
which will be called on
bean creation.
destroy-method : We
can specify method
which will be called
when bean is destroyed.