Spring Study[10]

來源:互聯網
上載者:User

the service objects are accessing the DAOs through interfaces.This has a couple of advantages. First, it makes your service objects easily testable since they are not coupled to a specific data access implementation.In addition, the data access tier is accessed in a persistence technology-agnostic manner.

Spring’s DAO frameworks do not throw technology-specific exceptions, such as SQLException or HibernateException. Instead, all exceptions thrown are subclasses of the technology-agnostic org.springframework.dao.DataAccessException.
DataAccessException is a RuntimeException, so it is an unchecked exception.

Working with DataSources:
In Spring’s DAO frameworks, Connection objects are obtained through a DataSource.

[1]Getting a DataSource from JNDI:
In this case, we use the JndiObjectFactoryBean.

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
 <property name="jndiName">
  <value>java:comp/env/jdbc/myDatasource</value>
 </property>
</bean>

[2]Creating a DataSource connection pool:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driver">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>

ManagerDataSource. This class can easily be configured and used with a unit test or suite of unit tests.
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);

Spring separates the fixed and variant parts of the data access process into two distinct classes: templates and callbacks. Templates manage the fixed part of the process while callbacks are where you fill in the implementation details.

Using JDBC with Spring:
Using JdbcTemplate:

public class StudentDaoJdbc implements StudentDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

}

<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="studentDao" class="StudentDaoJdbc">
<property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property>
</bean>
<bean id="courseDao" class="CourseDaoJdbc">
<property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property>
</bean>


The first callback we will explore is PreparedStatementCreator.
PreparedStatement createPreparedStatement(Connection conn) throws SQLException;
When you implement this interface, you are responsible for creating and returning a PreparedStatement from the Connection argument

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.