1.Spring提供有DAO支持模板类,功能类似于Apache DBUtils
JdbcTemplate
HibernateTemplate
SqlMapClientTemplate(过时)
JpaTemplate (过时)
2.基础实例
A.导入jar包
核心包(4个)
日志(2个)
jdbc模板支持(1个)
spring-jdbc-3.2.0.RELEASE.jar
模板相关事务处理包(1个)
spring-tx-3.2.0.RELEASE.jar
数据库连接驱动包
mysql-jdbc.jar
B.JDBC模板类使用(编程式)
//创建Spring对应的数据源对象
//数据源描述的是一系列的相关描述信息的封装对象
DriverManagerDataSource dataSource = new DriverManagerDataSource(); //为数据源对象提供数据库连接的相关数据(4个)
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/springdb");
dataSource.setUsername("root");
dataSource.setPassword("root");
//创建JDBC模板类,需要使用数据源对象
JdbcTemplate template = new JdbcTemplate(dataSource);
//执行添加语句
template.execute("insert into tbl_user values(null,'jock',34)");
template.execute("update tbl_user set userName='jockme' where uuid=1");
3.使用Spring形式开发JDBCTemplate
A.在DAO中声明JDBCTemplate变量
B.提供setter注入
C.将DAO声明为Bean,为其注入JDBCTemplate
<!-- UserDao -->
<bean id="userDao" class="cn.itcast.jdbc.UserDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
D.声明JDBCTemplate的Bean,为其注入DataSource
<!-- JDBCTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/>
</bean>
E.声明DataSource的Bean,为其注入DataSource需要使用的数据库连接参数
<!-- Spring内置的DataSource --> <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/springdb"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> 4.数据源配置格式(3种) Spring内置的DataSource <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/springdb"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> 使用DBCP,需要导入对应的jar包 com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar com.springsource.org.apache.commons.pool-1.5.3.jar <!-- 声明DBCP的DataSource --> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/springdb"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> 使用c3p0,需要导入对应的jar包 com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar <!-- 声明c3p0的数据源DataSource --> <bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springdb"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> 5.用外部properties格式文件,输入参数 A.制作独立的属性文件jdbc.properties放置到src目录下,用于保存数据库连接的相关参数 driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/springdb jdbc_username=root password=root B.加载属性文件到Spring配置中 方式一:不推荐使用 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property>
</bean> 方式二:推荐使用 <context:property-placeholder location="classpath:jdbc.properties"/> 注意:方式二可以使用通配符格式classpath:*.properties格式,一次加载所有的配置文件,但是,该方式要加载所有的系统配置,因此属性文件中的属性名不要与系统属性冲突。userName特别注意 C.将固定的参数改为引用属性文件中的参数 格式:${属性名} ${userName} ${url} ${password} ${jdbc_user}