Top Banner
Spring Data Access By, Srinivas Reddy.S www.JAVA9S.com
21

Spring Data Access

Feb 25, 2016

Download

Documents

aiko

Spring Data Access. By, Srinivas Reddy.S. www.JAVA9S.com. Introduction to Spring JDBC. www.JAVA9S.com. Multiple ways to connect to DB. JDBC IBATIS Hibernate JPA JCA. www.JAVA9S.com. Layered architecture. Security. Controller. Service. DAO. DB. client. www.JAVA9S.com. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Spring Data Access

Spring Data Access

By,Srinivas Reddy.S

www.JAVA9S.com

Page 2: Spring Data Access

Introduction to Spring JDBC

www.JAVA9S.com

Page 3: Spring Data Access

Multiple ways to connect to DB

• JDBC• IBATIS• Hibernate• JPA• JCA

www.JAVA9S.com

Page 4: Spring Data Access

Layered architecture

client DB

www.JAVA9S.com

Page 5: Spring Data Access

DAO – Advantages and Best practices

Advantages:• Application is independent of the Data access

techniques and database dependency.• Offers loose coupling with the other layers of

application.• Allows to unit test the service layer using

mock objects with out connecting to database.

www.JAVA9S.com

Page 6: Spring Data Access

DAO LayerService Layer

DAO Interfaces

DAO Implementations

DB

www.JAVA9S.com

Page 7: Spring Data Access

DAO – Advantages and Best practices

Best practices to be followed:• Always DAO classes should adhere to

interfaces.• Interfaces should expose the functionality that

are needed by the Service layers.• Keep the database related configurations

outside the code as configurable items in an xml or properties file.

www.JAVA9S.com

Page 8: Spring Data Access

Problems with JDBC

• Poor Exception hierarchy– SQLException– DataTruncation– SQLWarning– BatchUpdateException

Issues with JDBC Exceptions:Least explantiveDefined as checked exceptions which mandate us to write the catch blocks

www.JAVA9S.com

Page 9: Spring Data Access

Spring Exception hirerarchy CannotAcquireLockException CannotSerializeTransactionException CleanupFailureDataAccessException ConcurrencyFailureException DataAccessException DataAccessResourceFailureException DataIntegrityViolationException DataRetrievalFailureException DeadlockLoserDataAccessException

www.JAVA9S.com

Page 10: Spring Data Access

Spring Exception hirerarchy EmptyResultDataAccessException IncorrectResultSizeDataAccessExceptionIncorrectUpdateSemanticsDataAccessException InvalidDataAccessApiUsageException InvalidDataAccessResourceUsageException OptimisticLockingFailureException PermissionDeniedDataAccessException PessimisticLockingFailureException TypeMismatchDataAccessException UncategorizedDataAccessException

www.JAVA9S.com

Page 11: Spring Data Access

Spring Exception Hierarchy - Advantages

• More elaborate and detailed exceptions• Exceptions not specific to an API. Can be used

with any ORM and data access API.• Declared as unchecked exceptions – No catch

blocks needed.

www.JAVA9S.com

Page 12: Spring Data Access

Problems with JDBC – Data access• Create a connection• Create a statement• Start the transaction• Execute the statement• Iterate the result set and get the values• Commit the transaction/roll back when

exception occurs• Close the statement• Close the connection

www.JAVA9S.com

Page 13: Spring Data Access

Spring JDBC – Template pattern

• Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses.

• Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithms structure.

www.JAVA9S.com

Page 14: Spring Data Access

Data access templates in Spring

• JdbcTemplate• NamedParameterJdbcTemplate• SimpleJdbcTemplate• HibernateTemplate• JpaTemplate• SqlMapClientTemplate• JdoTemplate• CciTemplate

www.JAVA9S.com

Page 15: Spring Data Access

JDBCTemplate Dependencies

Data source

JDBCTemplat

e

DAOImpl

www.JAVA9S.com

Page 16: Spring Data Access

Spring JDBC Configuration steps

1. Choose a data source type:1. Data sources by JDBC API.• DriverManagerDataSource• SingleConnectionDataSource

2. Data sources looked up by JNDI.• <jee:jndi-lookup id="dataSource"

jndi-name="/jdbc/springjdbc" resource-ref="true" />

3. Data sources that pool connections.– Using Apache commons DBCP– org.apache.commons.dbcp.BasicDataSource

www.JAVA9S.com

Page 17: Spring Data Access

Spring JDBC Configuration steps

2. Configure the Data Source<bean id ="dataSource" class

="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name ="driverClassName" value ="com.mysql.jdbc.Driver"/><property name ="url" value ="jdbc:mysql://localhost:3306/springjdbc"/><property name ="username" value ="root"/><property name ="password" value ="srinureddy"/></bean>

www.JAVA9S.com

Page 18: Spring Data Access

Spring JDBC Configuration steps

3. Configure the JdbcTemplate and add data source as dependency

<bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate">

<property name ="dataSource" ref ="dataSource"/></bean>

www.JAVA9S.com

Page 19: Spring Data Access

Spring JDBC Configuration steps

• Register the Dao Impl class and add the JdbcTemplate as Dependency

<bean id ="employeeDao" class ="EmployeeDaoImpl"><property name ="jdbcTemplate" ref ="jdbcTemplate"/></bean>

www.JAVA9S.com

Page 20: Spring Data Access

JDBCTemplate Dependencies

Data source

JDBCTemplat

e

DAOImpl

www.JAVA9S.com

Page 21: Spring Data Access

Thank you

Follow Java9s onTwitter : @java9sfacebook: www.facebook.com/java9s

www.JAVA9S.com