package com.com.jdbctemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; /** * spirng JdbcTemplate在spring中的ioc中使用 */ public class JDBCTemplateDemo2 { public static void main(String[] args) { //1.获取spirng的核心容器 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.获取对象 JdbcTemplate jt = ac.getBean("jdbcTemplate", JdbcTemplate.class); //3.执行操作 jt.execute("insert into account(name,money)values('eee',2000)"); } }
xml文件的ioc配置
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置JdbcTemplate对象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置数据源 使用spring的内置数据源--> <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/spring"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> </beans>