Download presentation
Presentation is loading. Please wait.
Published byClarence Mothershed Modified over 10 years ago
1
1 juni 30, 2005 Spring JDBC
2
2 juni 30, 2005 Doel Database acties Flexibel Gecontroleerd Productief
3
3 juni 30, 2005 Database acties CRUD stored procedures Lob...
4
4 juni 30, 2005 Flexibel jdbc ORM framework connection pooling datasource testen outside container mock objecten Database onafhankelijk (?!)
5
5 juni 30, 2005 Gecontroleerd Exception handling Connection leaking
6
6 juni 30, 2005 Productief gericht op functie geen plumbing
7
7 juni 30, 2005 Architectuur Service DAO Domain Object Value Object Domain Object JNDI
8
8 juni 30, 2005 SIDE STEP - Architectuur issue Hoe implementeer je de emp – dept en emp – mgr relatie in de domein objecten? Hoe ga je met die relatie om in DAO’s? ORM taak lazy loading vs... Department Employee
9
9 juni 30, 2005 Example import java.sql.*; import javax.sql.*; public class EmpDao { public List getAllEmployees() { Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; List emps = new ArrayList(); try { con = getConnection(); pstmt = con.prepareStatement ("select * from emp"); rs = pstmt.executeQuery(); while (rs.next()) { Employee e = new Employee(); e.setId (rs.getLong(1)); e.setName (rs.getString(2)); //... emps.add(e); } } catch (SQLException e) { // handle exception } finally { try { rs.close(); pstmt.close(); con.close(); } catch (SQLException e1) { // no action needed } return emps; } private Connection getConnection() throws SQLException { try { Context ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup ("java:comp/env/jdbc/myDatabase"); return ds.getConnection(); } catch (NamingException e) { // handle exception return null; } private Connection getConnection() throws SQLException { try { DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver()); return DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:orcl“,"scott", "tiger"); } catch (SQLException sqle) { // handle exception return null; }
10
10 juni 30, 2005 How can Spring help? Make life easier: DAO Support JDBC, Hibernate, Toplink, iBatis, JDO,... Dependency Injection Jdbc helper classes Exception Handling MockObjects
11
11 juni 30, 2005 Spring Architectuur Service DAO Domain Object Domain Object Domain Object DAO Interface ApplicationContext-jdbc XXDAO Support JdbcDaoSupport HibernateDaoSupport TopLinkDaoSupport...
12
12 juni 30, 2005 Example A public interface empDao { public List getAllEmployees (); } public class EmployeeJdbcDao extends JdbcDaoSupport implements EmpDao { public List getAllEmployees() { JdbcTemplate jt = getJdbcTemplate(); return jt.queryForList (“select * from emp”); } <bean id="dataSourceDBDirect" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
13
13 juni 30, 2005 Exception Handling RuntimeException ipv checked SQLException DataAccessException SQLException Translation DataIntegrityViolationException DataRetrievalFailureException CannotGetJdbcConnectionException ...
14
14 juni 30, 2005 jdbc helper classes JdbcTemplate query, queryForList, queryForInt, queryFor.. ArrayList (per row) of HashMaps (column name as key) RowMapper PreparedStatementCreator/Callback MappingSQLQuery...
15
15 juni 30, 2005 Testen en MockObjects public class TestEmployeeDao extends AbstractDependencyInjectionSpringContextTests { private EmployeeDao employeeDAO; public void setEmployeeDAO(EmployeeDao employeeDAO) { this.employeeDAO = employeeDAO; } protected String[] getConfigLocations() { return new String[] {"nl/amis/demo/dao/jdbc/applicationContext-jdbc.xml"}; } public void testFindEmployeeById () { Employee emp = employeeDAO.getEmployeeById(7839); assertEquals("KING", emp.getName()); assertEquals("PRESIDENT", emp.getJob()); //... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.