方法一:继承JdbcTemplate来实现

  1、配置applicationContext  

 <!-- 获取数据源连接   dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/hibernatetest"></property>
<property name="username" value="root"></property>
<property name="password" value="mysql"></property>
</bean> <!-- ********************************** -->
<bean id="personDao" class="cn.test.spring.jdbc.PersonDaoImpl">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>

  2、继承applicationContext

 public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao {

     public void savePerson() {
this.getJdbcTemplate().execute(" INSERT INTO person(pid,Pname,Page) VALUES(2,'李四',50) "); }

  3、测试

@Test
public void savePerson(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/spring/jdbc/applicationContext.xml");
PersonDao personDao= (PersonDao) applicationContext.getBean("personDao");
personDao.savePerson();
}

方法二:jdbcTemplate作为属性带入

  1、配置applicationContext.xml

<!-- 获取数据源连接   dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/hibernatetest"></property>
<property name="username" value="root"></property>
<property name="password" value="mysql"></property>
</bean> <!-- ********************************** --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="personDao2" class="cn.test.spring.jdbc.PersonDaoImpl2">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>

  2、声明字段

 public class PersonDaoImpl2  implements PersonDao {

     private JdbcTemplate jdbcTemplate;

     public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
} public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} public void savePerson() {
this.getJdbcTemplate().execute(" INSERT INTO person(pid,Pname,Page) VALUES(22,'李四2',502) "); }

  3、测试

@Test
public void savePerson2(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/spring/jdbc/applicationContext.xml");
PersonDao personDao= (PersonDao) applicationContext.getBean("personDao2");
personDao.savePerson();
}

方法三:通过构造函数

1、配置applicationContext.xml

<!-- 获取数据源连接   dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/hibernatetest"></property>
<property name="username" value="root"></property>
<property name="password" value="mysql"></property>
</bean> <!-- ********************************** -->
<bean id="personDao3" class="cn.test.spring.jdbc.PersonDaoImpl3">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>

2、构造函数

public class PersonDaoImpl3 extends JdbcTemplate  implements PersonDao {

    public PersonDaoImpl3(DataSource dataSource){
super(dataSource);
} public void savePerson() {
this.execute(" INSERT INTO person(pid,Pname,Page) VALUES(23,'李四3',503) "); }

3、测试

@Test
public void savePerson3(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/spring/jdbc/applicationContext.xml");
PersonDao personDao= (PersonDao) applicationContext.getBean("personDao3");
personDao.savePerson();
}

注:以上方法只能进行增删改,不能进行查找

查找:

目标方法:

public List<Person> getPersons() {
return this.getJdbcTemplate().query("select * from person", new PersonRowMapper());
}

PersonRowMapper.java

public class PersonRowMapper implements RowMapper {

    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Person person=new Person();
person.setPid(rs.getLong("pid"));
person.setPname(rs.getString("Pname"));
person.setPage(rs.getString("Page"));
return person;
}

测试

@Test
public void testQuery(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/spring/jdbc/applicationContext.xml");
PersonDao personDao= (PersonDao) applicationContext.getBean("personDao");
List<Person> personList = personDao.getPersons();
System.out.println(personList.size());
}

Spring与Jdbc Demo的更多相关文章

  1. Spring MVC系列之JDBC Demo(SpringBoot)(七)

    前言 前面我们了解了Spring MVC的基本使用,其实和.NET或.NET Core MVC无异,只是语法不同而已罢了,本节我们将和和数据库打交道,从最基础的JDBC讲解起,文中若有错误之处,还望指 ...

  2. JAVAEE——spring03:spring整合JDBC和aop事务

    一.spring整合JDBC 1.spring提供了很多模板整合Dao技术 2.spring中提供了一个可以操作数据库的对象.对象封装了jdbc技术. JDBCTemplate => JDBC模 ...

  3. Spring整合JDBC以及AOP管理事务

    本节内容: Spring整合JDBC Spring中的AOP管理事务 一.Spring整合JDBC Spring框架永远是一个容器,Spring整合JDBC其实就是Spring提供了一个对象,这个对象 ...

  4. Spring Security OAuth2 Demo

    Spring Security OAuth2 Demo 项目使用的是MySql存储, 需要先创建以下表结构: CREATE SCHEMA IF NOT EXISTS `alan-oauth` DEFA ...

  5. Spring学习笔记(五)—— Spring整合JDBC

    一.Spring对JDBC的支持 Spring提供了很多模板整合Dao技术 与JDBC的整合中,Spring中提供了一个可以操作数据库的对象——JdbcTemplate,该对象封装了JDBC技术,与D ...

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

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

  7. Spring的简单demo

    ---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...

  8. Spring的JDBC框架

    转自: http://www.cnblogs.com/windlaughing/p/3287750.html Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发. Spring主要 ...

  9. [原创]java WEB学习笔记109:Spring学习---spring对JDBC的支持:使用 JdbcTemplate 查询数据库,简化 JDBC 模板查询,在 JDBC 模板中使用具名参数两种实现

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

随机推荐

  1. 接收对 http://192.168.1.18:8001/ObtainData/Service 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。

    [2015/8/5 19:28:49]错误信息:接收对 http://192.168.1.18:8001/ObtainData/Service 的 HTTP 响应时发生错误.这可能是由于服务终结点绑定 ...

  2. 【转】实现展开列ExpandableListView的三种方式之SimpleExpandableListAdapter实例

    原文网址:http://blog.csdn.net/x605940745/article/details/12099709 实现可扩展展开列ExpandableListView的三种方式 欢迎加入QQ ...

  3. 区分execl与system——应用程序中执行命令

    execl:相关函数:fork, execle, execlp, execv, execve, execvp表头文件:#include <unistd.h>函数定义:int execl(c ...

  4. 数据结构(启发式合并):HNOI 2009 梦幻布丁

    Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. Input 第 ...

  5. java Spring使用配置文件读取jdbc.properties

    Spring使用配置文件读取jdbc.properties 在beans.xml中加入两个必须的bean [html]<bean id="propertyConfigurer" ...

  6. Delphi实现WebService带身份认证的数据传输

    WebService使得不同开发工具开发出来的程序可以在网络连通的环境下相互通信,它最大的特点就是标准化(基于XML的一系列标准)带来的跨平台.跨开发工具的通用性,基于HTTP带来的畅通无阻的能力(跨 ...

  7. java基础(五)

    这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...

  8. Java---网络编程(3)-TCP-互传文件和图片

    ☆ TCP 建立连接,形成传输数据的通道. 在连接中进行大数据量传输 通过三次握手完成连接,是可靠协议 必须建立连接,效率会稍低 Socket 和 ServerSocket类 TCP传输 TCP So ...

  9. [转载]JQuery.closest(),parent(),parents()寻找父节点

    1.通过item-1查找 level-3(查找直接上级) $('li.item-1').closest('ul') $('li.item-1').parent() $('li.item-1').par ...

  10. poj 3678 Katu Puzzle(Two Sat)

    题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...