日期:2014-05-16  浏览次数:20457 次

JdbcTemplate和spring 实战
applicationContext.xml





<?xml version ="1.0" encoding ="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans  default-autowire ="autodetect">
     <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://127.0.0.1:3306/test?characterEncoding=UTF-8&amp;characterSetResults=UTF-8</value>
          </property>
         <property name="username">
             <value>root</value>
         </property>
         <property name="password">
             <value></value>
         </property>
         <property name="maxActive">
             <value>10</value>
         </property>
         <property name="maxIdle">
             <value>2</value>
         </property>
     </bean>
     <bean id="TransactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource">
             <ref bean="dataSource" />
         </property>
     </bean>
     <bean id ="JdbcTemplate"
         class ="org.springframework.jdbc.core.JdbcTemplate">
         <property name ="dataSource">
             <ref bean ="dataSource"  />
         </property>
     </bean>
</beans>

pojo User.java





import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class User ...{
    Integer id;
    String username;
    String password;

    public Integer getId() ...{
        return id;
    }

    public void setId(Integer id) ...{
        this.id = id;<