Spring JDBCTemplate配置信息

这篇博客介绍了如何在Spring配置文件中设置数据源和JDBC模板,针对MySQL 8.0和5.0两个版本的不同驱动进行详细说明。配置包括了指定数据库驱动、URL、用户名和密码,以及如何注入JdbcTemplate到业务类中以实现数据库操作。
摘要由CSDN通过智能技术生成

Spring 中 JDBC 的相关信息是在 Spring 配置文件中完成的,其配置模板如下所示:

① mysql数据库版本为8.0

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    ">
	<!-- 配置数据源 ,建议下面class后面的路径自行复制自己的(避免包名大小写问题)-->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!--数据库驱动 -->
		<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
		<!--连接数据库的url -->
		<property name="url" value="jdbc:mysql://localhost:3306/数据库名字?serverTimezone=UTC"></property>
		<!--连接数据库的用户名 -->
		<property name="username" value="root"></property>
		<!--连接数据库的密码 -->
		<property name="password" value="123456"></property>
	</bean>

	<!--配置JDBC模板 -->
	<!-- 配置jdbcTemplate,数据库操作工具类,使用它完成CRUD -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!--配置注入类 -->
	<bean id="xxx" class="xxx">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>
</beans>

② mysql数据库版本为5.0

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    ">
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!--数据库驱动 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<!--连接数据库的url -->
		<property name="url" value="jdbc:mysql://localhost:3306/数据库名字"></property>
		<!--连接数据库的用户名 -->
		<property name="username" value="root"></property>
		<!--连接数据库的密码 -->
		<property name="password" value="123456"></property>
	</bean>

	<!--配置JDBC模板 -->
	<!-- 配置jdbcTemplate,数据库操作工具类,使用它完成CRUD -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!--配置注入类 -->
	<bean id="xxx" class="xxx">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值