码迷,mamicode.com
首页 > 数据库 > 详细

【Spring六】JDBC编程之XML配置

时间:2015-07-21 18:50:54      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:框架   spring   jdbc   

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.properties</value >
           </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 >
     
     <!--方法一  继承JdbcDaoSupport -->
     <bean id="classesDao" class="cn.itheima03.spring.jdbc.ClassesDaoImpl" >
           <property name= "dataSource">
               <ref bean= "dataSource"/>
           </property>
     </bean >
     
     <!--方法二   引入JdbcTemplate,即让JdbcTemplate变成dao的成员变量-->
     <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 >
     
     <!--方法三  继承JdbcTemplate,需要在构造方法中提供数据源 -->  
     <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<Classes> 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<Classes> 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<Classes> 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编程之XML配置

标签:框架   spring   jdbc   

原文地址:http://blog.csdn.net/damogu_arthur/article/details/46988425

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!