1.DAO是數據訪問對象的縮寫,DAO提供了數據讀寫和寫入到數據庫中的一種方式,他們應該以藉口的方式發佈的功能,而應用程序的其他部分就可以通過接口來進行訪問
數據訪問層是以持久化技術無關的方式來進行訪問的。持久化方式的選擇獨立於DAO,只有相關的數據訪問放過發通過接口來進行發佈
2.數據訪問模板化
Spring 將數據訪問過城中固定的和可變的部分明確劃分爲兩個不同的類:模板和回調。模板管理過程中固定的部分,而回調處理自定義的數據訪問代碼,針對不同的持久化平臺spring提供了多個可選擇的模板,如果直接使用jdbc則可以選擇jdbcTemplate。如果希望使用對象關係映射框架,則hibernateTemplate 或JpaTemplate可能是很好的選擇
3.配置數據源連接池
<!-- 配置dpcp連接池 -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean>
4.配置SimpleJdbcTemplate
<!-- 配置SimpleJdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="datasource"></constructor-arg>
</bean>
5.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:annotation-config />
<context:component-scan base-package="com.qunar.dao.impl"></context:component-scan>
<!-- 配置dpcp連接池 -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean>
<!-- 配置SimpleJdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="datasource"></constructor-arg>
</bean>
</beans>
@Component("userservices")
public class IUserDAOImpl implements IUserDAO {
@Autowired
private SimpleJdbcTemplate jdbcTemplate;
private static final String SQL_INSERT_USER = "insert into user(username,nickname,email,address) values(?,?,?,?)";
@Override
public void addUser(User user) {
jdbcTemplate.update(SQL_INSERT_USER, user.getUsername(), user
.getNickname(), user.getEmail(), user.getAddress());
}
}
測試類:
package com.qunar.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qunar.dao.IUserDAO;
import com.qunar.model.User;
public class TestJdbcTemplate {
@Test
public void addUser() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDAO dao = (IUserDAO) context.getBean("userservices");
User user = new User();
user.setUsername("蘇浩");
user.setNickname("suhao");
user.setEmail("[email protected]");
user.setAddress("qtech");
dao.addUser(user);
}
}
測試通過