Spring 中 JDBC 的配置模板
Spring 中 JDBC 的配置模板
mysql-8.0.22
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1. 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!-- 数据库的url -->
<property name="url" value="jdbc:mysql://localhost/spring?serverTimezone=Asia/Shanghai"/>
<!-- 数据库的用户名 -->
<property name="username" value="root"/>
<!-- 数据库的密码 -->
<property name="password" value="123456"></property>
</bean>
<!-- 2. 配置JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认必须使用数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 3. 配置注入类 -->
<bean id="accountDao" class="com.turtledove.jdbc.AccountDaoImpl">
<!-- 将jdbcTemplate注入到accountDao实例中 -->
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
浙公网安备 33010602011771号