Spring使用JdbcTemplate操作数据库

来源:互联网 发布:投顾说软件下载 编辑:程序博客网 时间:2024/04/27 13:41

文章来源:http://www.blogjava.net/baoyaer/articles/154080.html

首先,假设如下SQL表中有数据username=test1,passwd=test1,address=test1

 

CREATE TABLE `login` (

  `username` varchar(10) default NULL,

  `passwd` varchar(10) default NULL,

  `address` varchar(10) default NULL

) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

 

配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

<beans>

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

   <property name="driverClassName">

     <value>com.mysql.jdbc.Driver</value>

   </property>

   <property name="url">

     <value>jdbc:mysql://localhost:3306/javaee</value>

   </property>

   <property name="username">

     <value>root</value>

   </property>

   <property name="password">

     <value>1234</value>

   </property>

 </bean>

 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

   <property name="dataSource">

     <ref local="dataSource"/>

   </property>

 </bean>

 

<bean id="personDAO" class="SpringJDBCSupport.ReadData.PersonDAO">

  <property name="jdbcTemplate">

    <ref local="jdbcTemplate"/>

  </property>

</bean> 

</beans>

 

 JavaBean:

 

运行结果:

test1-test1-test1