Spring对持久层技术支持
  JDBC : org.springframework.jdbc.core.JdbcTemplate
  Hibernate3.0 : org.springframework.orm.hibernate3.HibernateTemplate
  IBatis(MyBatis) : org.springframework.orm.ibatis.SqlMapClientTemplate
  JPA : org.springframework.orm.jpa.JpaTemplate
开发JDBCTemplate入门
  第一步:引入相应jar包
    spring-tx-3.2.0.RELEASE.jar
    spring-jdbc-3.2.0.RELEASE.jar
    mysql驱动.
  第二步:编写一个测试类

@Test
public void demo1(){
  // 创建连接池:
  DriverManagerDataSource dataSource = new DriverManagerDataSource();
  // 设置参数:
  dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  dataSource.setUrl("jdbc:mysql:///spring3_day02");
  dataSource.setUsername("root");
  dataSource.setPassword("123");
  // 使用JDBC的模板:
  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
}

配置连接池
  Spring默认的连接池

<!-- 配置Spring默认的连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql:///spring3_day02"/>
  <property name="username" value="root"/>
  <property name="password" value="123"/>
</bean>
<!-- 定义jdbctemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"/>
</bean>

    测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 {
  @Autowired
  @Qualifier("jdbcTemplate")
  private JdbcTemplate jdbcTemplate;
  @Test
  public void demo2(){
    jdbcTemplate.execute("create table user (id int primary key auto_increment,name varchar(20))");
  }
}

  

  DBCP连接池
    导入jar包
      com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
      com.springsource.org.apache.commons.pool-1.5.3.jar

<!-- 配置DBCP连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql:///spring3_day02"/>
  <property name="username" value="root"/>
  <property name="password" value="123"/>
</bean>

  C3P0连接池
    导入jar包
      com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  <property name="jdbcUrl" value="jdbc:mysql:///spring3_day02"/>
  <property name="user" value="root"/>
  <property name="password" value="123"/>
</bean>

参数设置到属性文件中
  在src下创建jdbc.properties

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///spring3_day02
jdbc.user = root
jdbc.password = 123

  需要在applicationContext.xml 中使用属性文件配置的内容.
    第一种写法

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="classpath:jdbc.properties"></property>
</bean>

    第二种写法(需要引入context标签的头)

<context:property-placeholder location="classpath:jdbc.properties"/>

    案例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
  <!--
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location" value="classpath:jdbc.properties"></property>
     </bean>
  -->
  <context:property-placeholder location="classpath:jdbc.properties"/>
  <!-- 配置c3p0连接池 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.user}"/>
    <property name="password" value="${jdbc.password}"/>
  </bean>
  <!-- 定义jdbctemplate -->
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
  </bean>
</beans>

JdbcTemplate的CRUD的操作
  Spring框架中提供了对持久层技术支持的类
    JDBC : org.springframework.jdbc.core.support.JdbcDaoSupport
    Hibernate 3.0 : org.springframework.orm.hibernate3.support.HibernateDaoSupport
    iBatis : org.springframework.orm.ibatis.support.SqlMapClientDaoSupport

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
  <context:property-placeholder location="classpath:jdbc.properties"/>
  <!-- 配置c3p0连接池 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.user}"/>
    <property name="password" value="${jdbc.password}"/>
  </bean>
  <!-- 定义jdbctemplate -->
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
  </bean>
  <bean id="userDao" class="cn.yzu.spring3.demo2.UserDao">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
  </bean>
</beans>
public class UserDao extends JdbcDaoSupport{
  public void add(User user){
    String sql = "insert into user values (null,?)";
    this.getJdbcTemplate().update(sql, user.getName());
  }
  public void update(User user){
    String sql = "update user set name = ? where id = ?";
    this.getJdbcTemplate().update(sql, user.getName(),user.getId());
  }
  public void delete(User user){
    String sql = "delete from user where id = ?";
    this.getJdbcTemplate().update(sql, user.getId());
  }
  public int findCount(){
    String sql = "select count(*) from user";
    return this.getJdbcTemplate().queryForInt(sql);
  }
  public String findNameById(int id){
    String sql = "select name from user where id = ?";
    return this.getJdbcTemplate().queryForObject(sql, String.class, id);
  }
  public User findById(int id){
    String sql = "select * from user where id = ?";
    User user = this.getJdbcTemplate().queryForObject(sql, new UserRowMapper(), id);
    return user;
  }
  public List<User> findAll(){
    String sql = "select * from user";
    return this.getJdbcTemplate().query(sql, new UserRowMapper());
  }
  class UserRowMapper implements RowMapper<User>{
    /**
    * rs:结果集.
    * rowNum:行号
    */
    public User mapRow(ResultSet rs, int rowNum) throws SQLException {
      User user = new User();
      user.setId(rs.getInt("id"));
      user.setName(rs.getString("name"));
      return user;
    }
  }
}

Spring的JDBC模板的更多相关文章

  1. Spring的jdbc模板1

    Spring是EE开发的一站式框架,有EE开发的每一层解决方案.Spring对持久层也提供了解决方案:ORM模块和jdbc模块,ORM模块在整合其他框架的时候使用 Spring提供了很多的模板用于简化 ...

  2. Java学习笔记43(Spring的jdbc模板)

    在之前的学习中,我们执行sql语句,需要频繁的开流,关流比较麻烦,为了更加的简化代码,我们使用Spring 的jdbc模板jdbcTemplate来简化我们的代码量:需要导入的包有: 我们在之前的dr ...

  3. 四、spring的JDBC模板和事务管理

    Spring的JDBC模板 Spring是JavaEE开发的一站式框架,对各种持久化技术都提供了简单的模板 ORM持久化技术 模板类 JDBC org.springframework.jdbc.cor ...

  4. Spring的jdbc模板3:完成CURD操作

    测试类代码如下 package zcc.spring_jdbc.demo2; import java.sql.ResultSet; import java.sql.SQLException; impo ...

  5. Spring的jdbc模板2:使用开源的连接池

    上篇简要介绍了如何在spring中配置默认的连接池和jdbc模板,这篇来介绍开源的连接池配置与属性引入 C3P0连接池配置: 引入jar包 配置c3p0连接池 <?xml version=&qu ...

  6. java框架之Spring(3)-JDBC模板使用&事务管理

    下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...

  7. 十八 Spring的JDBC模板:引入外部属性文件

    配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...

  8. Spring之JDBC模板jdbcTemplate

    要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象.           第一种方式:我们可以在自己定义的DAO 实现类中注入一个Da ...

  9. Spring框架的JDBC模板技术和事物

    Spring框架的JDBC模板技术         技术分析之Spring框架的JDBC模板技术概述  1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单     ...

随机推荐

  1. 利用vim查看日志,快速定位问题

    起因 在一般的情况下,如果开发过程中测试报告了一个问题,我一般会这么做: 1.在自己的开发环境下重试一下测试的操作,看看能不能重现问题.不行转2 2.数据库连接池改成测试库的地址,在自己的开发环境下重 ...

  2. iOS多线程-RunLoop简介

    什么是RunLoop? 从字面上来看是运行循环的意思. 内部就是一个do{}while循环,在这个循环里内部不断的处理各种任务(比如:source/timer/Observer) RunLoop的存在 ...

  3. 【Java之对象清理】finalize()的用途

    Java允许在类中定义一个名为finalize()的方法.它的工作原理是:一旦垃圾回收器准备好释放对象占用的存储空间,将首先调用其finalize()方法.并且在下一次垃圾回收动作发生时,才会真正回收 ...

  4. Qt控件样式 Style Sheet Demo

    迟来的笔记,作为一个程序员每日记事已养成习惯,离开许久,不知不觉已喜欢用文字表达对技术的热爱,学无止境! Qt – 一个跨平台应用程序和UI开发框架:它包括跨平台类库.集成开发工具和跨平台 IDE,使 ...

  5. C++网络套接字编程TCP和UDP实例

    原文地址:C++网络套接字编程TCP和UDP实例作者:xiaojiangjiang 1.       创建一个简单的SOCKET编程流程如下 面向有连接的套接字编程 服务器: 1)  创建套接字(so ...

  6. MVC中渲染页面

    mvc中当返回的字符带有html代码的时候,可以直接使用@Html.Raw(Model.description)这句代码的意思就是返回不是html编码,因此用了这句代码就不需要单独再转换一次

  7. python2.7 学习笔记--列表的使用

    同其它编程语言一样,python也提供了丰富的数据结构,以方便数据的处理.本文介绍两种最基本的数据集合,列表和元组的使用. 一.列表使用介绍 可以理解为一个有序的序列.其使用方式举例如下: list= ...

  8. iOS音频解码表格

  9. jQuery判断及更改checkbox状态

    判断:jquery对象.prop("checked") 选中:jquery对象.prop("checked", true) 取消选中:jquery对象.remo ...

  10. Markdown简单语法

    Content 标题大小 斜体和加粗 分割线 有序列表和无序列表 链接 代码框 标题大小 在字体下方加上-和=分别表示一级标题和二级标题,例如: 一级标题 --- 二级标题 === 或者使用#的个数表 ...