【Spring六】JDBC编程之XML配置

jdbc编程最主要的就是要引入数据源,常见的有dbcp数据源,c3p0数据源等。

几个重要的类:
JdbcTemplate,里面提供了dao的方法,需要提供数据源给他!
JdbcDaoSupport
RowMapper

说明:
   无论采用什么样的方法必须把dataSource注入到JdbcTemplate里
  1、继承JdbcDaoSupport(该类有JdbcTemplate)
  2、继承JdbcTemplate
  3、引入JdbcTemplate

1.xml配置:
< 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-2.5.xsd"  >
     
     
      < bean
            class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
            <property name= "locations">
                <value> classpath:jdbc.propertiesvalue >
            property>
      bean  >
     
     
      < bean  id="dataSource" destroy-method="close"
            class= "org.apache.commons.dbcp.BasicDataSource" >
            <property name= "driverClassName" value="${jdbc.driverClassName}" />
            <property name= "url" value ="${jdbc.url}" />
            <property name= "username" value="${jdbc.username}" />
            <property name= "password" value="${jdbc.password}" />
      bean  >
     
     
      < bean  id="classesDao" class="cn.itheima03.spring.jdbc.ClassesDaoImpl" >
            <property name= "dataSource">
                <ref bean= "dataSource"/>
            property>
      bean  >
     
     
      < bean  id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
            <constructor-arg index= "0" ref="dataSource" >constructor-arg>
      bean  >
     
      < bean  id="classesDao2" class="cn.itheima03.spring.jdbc.ClassesDaoImpl2" >
            <property name= "jdbcTemplate">
                <ref bean= "jdbcTemplate"/>
            property>
      bean  >
     
        
      < bean  id="classesDao3" class="cn.itheima03.spring.jdbc.ClassesDaoImpl3" >
            <constructor-arg index= "0" ref="dataSource" >constructor-arg>
      bean  >
     
     
      < bean  id="itheimaTemplate" class="cn.itheima03.spring.itheima03db.ItHeimaTemplate" >
            <constructor-arg index= "0" ref="dataSource" >constructor-arg>
      bean  >
     
      < bean  id="classesDao4" class="cn.itheima03.spring.itheima03db.ClassesDaoImpl4" >
            <constructor-arg index= "0" ref="dataSource" >constructor-arg>
      bean  >
     

beans >


==============================================
位于src目录下的配置文件: jdbc.properties

jdbc.driverClassName= com.mysql.jdbc.Driver
jdbc.url= jdbc\:mysql \://localhost\:3306/hibernate_itheima03
jdbc.username= root
jdbc.password= root


2.dao测试
/*
 * 方式一:继承JdbcDaoSupport,需要使用数据源,在配置文件中应该配置。
 */
public  class ClassesDaoImpl extends JdbcDaoSupport implements ClassesDao{
      public  void saveClasses() {
            this.getJdbcTemplate().execute("insert into classes(cname,description) values('a','a')");
     }

      @Override
      public  List getClasses() {
            return this .getJdbcTemplate().query("select * from classes", new ClassesRowMapper()) ;
     }
}
===================================================
/**
 * 方式二:引入JdbcTemplate
 */
public  class ClassesDaoImpl2 implements ClassesDao{
      private  JdbcTemplate jdbcTemplate;
      public  JdbcTemplate getJdbcTemplate() {
            return jdbcTemplate ;
     }
      public  void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
     }
      public  void saveClasses() {
            this.jdbcTemplate .execute("insert into classes(cname,description) values('a','a')");
     }
      @Override
      public  List getClasses() {
            return null ;
     }
}
===================================================
/**
 * 方式三:继承JdbcTemplate,写一个以DataSource作为参数的构造函数。
 */
public  class ClassesDaoImpl3 extends JdbcTemplate implements ClassesDao{
     
      public  ClassesDaoImpl3(DataSource dataSource){
            super(dataSource);
     }
     
      public  void saveClasses() {
            this.execute("insert into classes(cname,description) values('a','a')");
     }

      @Override
      public  List getClasses() {
            return null;
     }
}
===================================================
/**
 * 方式四:自定义模板。
 *
 * 在spring内部完成是面向接口编程
 * 在spring的配置文件中,通过set方法或者构造器,给接口注入实现的类
 *
 */
public  class ItHeimaTemplate {
      private  DataSource dataSource;
     
      public  DataSource getDataSource() {
            return dataSource ;
     }
      public  void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
     }
      public  ItHeimaTemplate(){}
      public  ItHeimaTemplate(DataSource dataSource){
            this.dataSource = dataSource;
     }
      /**
      * 实现插入数据
      */
      public  void insert(String sql){
            try {
              Connection conn =  this.dataSource .getConnection();
              Statement statement = conn.createStatement();
              statement.executeUpdate(sql);
          }  catch (Exception e) {
          }
     }
}
---------------------------------------------------
public  class ClassesDaoImpl4 extends ItHeimaTemplate{
      public  ClassesDaoImpl4(DataSource dataSource){
            super(dataSource);
     }
      public  void insert(){
            this.insert("insert into classes(cname,description) values('a','a')");
     }
}
===================================================
public  class DataSourceTest {
      @Test
      public  void testDataSource(){
          ApplicationContext context =  new ClassPathXmlApplicationContext("cn/itheima03/spring/jdbc/applicationContext.xml" );
          DataSource dataSource = (DataSource)context.getBean( "dataSource"  );
          System.  out.println(dataSource);
     }
}

===================================================
/**
 * 查询时,需要写一个类实现RowMapper
 */
public  class ClassesRowMapper implements RowMapper{

      @Override
      public  Object mapRow(ResultSet rs, int rowNum) throws SQLException {
          Classes classes =  new Classes();
          classes.setCid(rs.getLong(  "cid"));
          classes.setCname(rs.getString(  "cname"));
          classes.setDescription(rs.getString(  "description"));
            return classes;
     }

}

===================================================
public  class ClassesDaoTest {
      @Test
      public  void testSaveClasses(){
          ApplicationContext context =  new ClassPathXmlApplicationContext("cn/itheima03/spring/jdbc/applicationContext.xml" );
          ClassesDao classesDao = (ClassesDao)context.getBean( "classesDao3"  );
          classesDao.saveClasses();
     }
     
      @Test
      public  void testSaveClasses4(){
          ApplicationContext context =  new ClassPathXmlApplicationContext("cn/itheima03/spring/jdbc/applicationContext.xml" );
          ClassesDaoImpl4 classesDao = (ClassesDaoImpl4)context.getBean( "classesDao4"  );
          classesDao.insert();
     }
      /**
      * 测试查询
      */
      @Test
      public  void testGetClasses(){
          ApplicationContext context =  new ClassPathXmlApplicationContext("cn/itheima03/spring/jdbc/applicationContext.xml" );
          ClassesDao classesDao = (ClassesDao)context.getBean( "classesDao"  );
          System.  out.println(classesDao.getClasses().size());
     }
}

你可能感兴趣的:(Spring,JDBC,框架,spring,jdbc)