1. DAO
--> Spring uses a design pattern called
template design pattern by using this design pattern we can reduce the
redundant code in java project.
--> For example while connecting to the
database we provide jdbc code every time and to resolve this problem we can use
JDBC template.
--> By using DAO we can interact with
database server. DAO of spring will help us to simplify the development of JDBC
code.
Procedure
to deal with DAO module in spring:
1.
Start my eclipse IDE to point the current work
space
2.
create java project with
package
3.
Add spring core and DAO modules
to the project
4.
Configure DBS as part of
eclipse IDE by suing DB explorer perspective
5.
Configure data source object in
spring bean configure file
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"></property>
<property name="username" value="sql"></property>
<property name="password" value="sql"></property>
</bean>
6.
Configure JDBC template in
spring configuration file
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
We will done above process one for the
project.
public class
MainDAO {
public static void
main(String[] args) {
ApplicationContext continer = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate template =
(JdbcTemplate) continer.getBean("jdbcTemplate");
int no =
template.update("insert into emp values(1)");
System.out.println(no);
}
}
For this program we get " could not
find the driver". To resolve this problem we set the JDBC jar file to
project.
--> when update method is called jdbc
template internal code get the connection and send query to data base server
and close the connections.
--> by using update() method we can perform the insert, update, delete
operations.
--> we can perform above operations by
using execute() also
int update();
void execute();
Prepared
statement:
public class
MainDAO {
public static void
main(String[] args) {
ApplicationContext continer = new
ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate template = (JdbcTemplate) continer.getBean("jdbcTemplate");
String query = "insert
into emp values(?)";
Object[]
o = new Object[1];
o[0]
= 1;
template.update(query,o);
}
}
batch
updates:
public class
MainDAO {
public static void
main(String[] args) {
ApplicationContext continer = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate template = (JdbcTemplate)
continer.getBean("jdbcTemplate");
String
query = "insert into emp values(?)";
String[] querys = new
String[3];
querys[0] = "insert
into emp values(1)";
querys[1] = "insert
into emp values(2)";
querys[2] = "insert
into emp values(3)";
template.batchUpdate(querys);
}
}
develop a spring based application which can retrieve data
from database server?
-->
To extract data from "Result Set" object we need to provide the
implementation of interface "org.springframework.jdbc.core.ResultSetExtractor"
public class
MainDAO {
public static void
main(String[] args) {
ApplicationContext continer = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate template =
(JdbcTemplate) continer.getBean("jdbcTemplate");
String
query = "select * from emp";
template.query(query, new
RetriveDat());
}
}
public class
RetriveDat implements ResultSetExtractor {
public Object
extractData(ResultSet rs) throws SQLException,
DataAccessException {
while(rs.next()) {
System.out.println(rs.getString("eno"));
}
return null;
}
}
--> In the above program RetriveData is called as call back mechanism class.
--> To deal with single row or single
column results spring people provided the separate methods.
ex Long value = template.queryForLong("select
count(*) from emp);
2. ORM
--> Spring hibernate template is depends
on the session factory and session factory depends on the data source
--> The internal code of hibernate
create the object for session factory and call the configure() method.
--> Internal code of hibernate create
the session object and start the transaction and takes care of the transaction
and close the session.
Procedure to deal with hibernate in spring?
·
create the project in the IDE
·
add core and hibernate modules
to project
·
configure data source and session
factory in spring bean configuration file
·
add hibernate capabilities to
project
·
generate hbm and pojo classes
·
configure hibernate template in
spring configuration file
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"></property>
<property name="username" value="sql"></property>
<property name="password" value="sql"></property>
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ibm/spring/Emp.hbm.xml</value></list>
</property></bean>
<bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
pojo
class:
package
com.ibm.spring;
import
java.math.BigDecimal;
/**
* Emp entity. @author MyEclipse Persistence Tools
*/
public class Emp
implements java.io.Serializable {
// Fields
private
BigDecimal eno;
// Constructors
/** default constructor */
public
Emp() {
}
/** full constructor */
public
Emp(BigDecimal eno) {
this.eno =
eno;
}
// Property accessors
public
BigDecimal getEno() {
return this.eno;
}
public void
setEno(BigDecimal eno) {
this.eno =
eno;
}
}
hbm
file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by
MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.ibm.spring.Emp" table="EMP" schema="SQL">
<id name="eno" type="java.math.BigDecimal">
<column name="ENO" precision="22"
scale="0" />
<generator class="assigned" />
</id>
</class>
</hibernate-mapping>
main:
package
com.ibm.spring;
import
java.math.BigDecimal;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
org.springframework.orm.hibernate3.HibernateTemplate;
public class Main {
public static void
main(String[] args) {
ApplicationContext continer = new ClassPathXmlApplicationContext("applicationContext.xml");
HibernateTemplate
template = (HibernateTemplate) continer.getBean("hibernateTemplate");
Emp e = new
Emp();
e.setEno(new
BigDecimal(1));
template.save(e);
}
}
No comments:
Post a Comment