spring和JDBC

来源:互联网 发布:阿里云栖大会苏州 编辑:程序博客网 时间:2024/06/02 06:31

1.首先肯定是要的到数据源的,在学习阶段,主要还是选择创建一个DataSource连接池,因为它的所有属性都是通过setter方法暴露的所以,我们可以将其交给spring来管理,像配置Bean一个配置它。

........................................................................................................................................

    <!-- 配置数据源 -->

   <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/demo</value>

    </property>

    <property name="username">

      <value>root</value>

    </property>

    <property name="password">

      <value>19897979</value>

    </property>

     </bean>

 

2.直接使用JDBC,都会觉得代码量大,并且很麻烦,spring把数据访问流程中的固定部分和可变部分分开,分别映射为2个截然不同的类:

   模板(Template)和回调(Callback)。

 

   2.1模板类JdbcTemplate,需要用到JdbcTemplate,就要为每一个DAO配置一个JdbcTemplate实例

 

   <!-- 配置JdbcTemplate -->

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

    <property name="dataSource"><ref bean="dataSource"/></property>

    </bean>

 

 <!-- 配置DAO -->

    <bean id="student" class="com.zhang.DAO.student">

     <constructor-arg index="0">

     <value>1</value>

     </constructor-arg>

       <constructor-arg index="1">

     <value>张齐</value>

     </constructor-arg>

       <property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property>  

 

    </bean>

 

3.写入数据

   我们仅需要非常少的代码就可以达到执行SQL语句的目的,一旦获得一个 DataSource和一个JdbcTemplate, 我们就可以使用JdbcTemplate提供的丰富功能实现我们的操作。 下面的例子使用了极少的代码完成创建一张表的工作。

 

import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class ExecuteAStatement {    private JdbcTemplate jt;    private DataSource dataSource;    public void doExecute() {        jt = new JdbcTemplate(dataSource);        jt.execute("create table mytable (id integer, name varchar(100))");     }    public void setDataSource(DataSource dataSource) {        this.dataSource = dataSource;    }}

除了execute方法之外,JdbcTemplate还提供了大量的查询方法。 在这些查询方法中,有很大一部分是用来查询单值的。比如返回一个汇总(count)结果 或者从返回行结果中取得指定列的值。这时我们可以使用queryForInt(..)queryForLong(..)或者queryForObject(..)方法。 queryForObject方法用来将返回的JDBC类型对象转换成指定的Java对象,如果类型转换失败将抛出 InvalidDataAccessApiUsageException异常。 下面的例子演示了两个查询的用法,一个返回int值,另一个返回 String

 

import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class RunAQuery {    private JdbcTemplate jt;    private DataSource dataSource;      public int getCount() {        jt = new JdbcTemplate(dataSource);        int count = jt.queryForInt("select count(*) from mytable");        return count;    }    public String getName() {        jt = new JdbcTemplate(dataSource);        String name = (String) jt.queryForObject("select name from mytable", String.class);        return name;    }    public void setDataSource(DataSource dataSource) {        this.dataSource = dataSource;    }}

除了返回单值的查询方法,JdbcTemplate还提供了一组返回List结果 的方法。List中的每一项对应查询返回结果中的一行。其中最简单的是queryForList方法, 该方法将返回一个List,该List中的每一条 记录是一个Map对象,对应应数据库中某一行;而该Map 中的每一项对应该数据库行中的某一列值。下面的代码片断接着上面的例子演示了如何用该方法返回表中 所有记录:

public List getList() {    jt = new JdbcTemplate(dataSource);    List rows = jt.queryForList("select * from mytable");    return rows;}

返回的结果集类似下面这种形式:

[{name=Bob, id=1}, {name=Mary, id=2}]

11.2.8. 更新数据库

JdbcTemplate还提供了一些更新数据库的方法。 在下面的例子中,我们根据给定的主键值对指定的列进行更新。 例子中的SQL语句中使用了“?”占位符来接受参数(这种做法在更新和查询SQL语句中很常见)。 传递的参数值位于一个对象数组中(基本类型需要被包装成其对应的对象类型)。

import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class ExecuteAnUpdate {    private JdbcTemplate jt;    private DataSource dataSource;    public void setName(int id, String name) {        jt = new JdbcTemplate(dataSource);        jt.update("update mytable set name = ? where id = ?", new Object[] {name, new Integer(id)});    }    public void setDataSource(DataSource dataSource) {        this.dataSource = dataSource;    }}

 

原创粉丝点击