内容源自:spring中使用jdbc

spring dao层中对jdbc进行了封装,使用模板模式的设计模式,通过ioc被动注入的方式将jdbcTemplate这个模板类注入到数据对象中,进行数据库操作。

我们要在一个类中进行CRUD操作(crud主要被用在描述软件系统中数据库或者持久层的基本操作功能。),首先要将jdbcTemplate这个模板类注入到数据对象类中,然后将DataSource这个类注入到jdbcTemplate,获取数据源。 这样数据对象类就可以通过jdbcTemplate类中的方法进行数据库操作了。

注意:这里需要导如spring jdbc的两个包和数据库驱动包

容器配置如下:

<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-3.2.xsd">

    <!--
        将CityDaoImpl、JdbcTemplate配置成ioc容器中的bean.

     -->
    <bean id="dao" class="com.etoak.dao.CityDaoImpl">
        <property name="jt" ref="jt"></property>
    </bean>
    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds"></property>
    <!--
        提供datasource数据源[接口] ~ bean  

            spring 内置了一个dataSource实现类DriverManageDataSource
     -->
    </bean>
    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/yitu"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

dao:

package com.etoak.dao;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.etoak.bean.City;

/**
 * 使用jdbc方式对表进行CURD操作
 * @author D_xiao
 *
 */
public class CityDaoImpl {
    private JdbcTemplate jt;

    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }
    /**
     *  JdbcTemplate 将连接数据库执行添加操作的流程
     *  封装在update()中
     *  增删改都是使用update方法
     */
    public  boolean addCity(City city){
        String sql =" insert into city values(null,?,?)";
        Object[] args = {city.getPid(),city.getName()};
        int result = jt.update(sql,args);  //result 执行该操作影响的数据量
        return result==1;  //影响一条 则添加成功
    }

    public boolean deleteCity(Integer id){
        String sql = "delete from city where id="+id;
        int result = jt.update(sql,id);
        return result==1;
    }
    public boolean updateCity(City city){
        String sql = "update city set pid=? , name=?  where id = ?";
        Object[] args = {city.getPid(),city.getName(),city.getId()};
        int result = jt.update(sql,args);
        return result==1;
    }

    /**查询单条数据
     * 在使用queryForMap()查询单条数据时,
     * 必须能够保证传入sql可以并且只能查询一条数据,否则会抛异常
     */
    public Map selectCityById(Integer id){
        String sql ="select * from city where id="+id;
        Map map = jt.queryForMap(sql);  //jdbc技术并非orm工具,并不能把直接查出来的关系型数据封装到对象,只能封装到map中
        //key 字段名  value 字段值
        return map;
    }
    /**
     * 查询批量数据
     */
    public List selectAllCitys(){
        String sql = "select * from city";
        List list = jt.queryForList(sql);
        return list;
    }
    /**
     * 查询数据量
     */
    public int selectCityCount(){
        String sql = "select count(*) from city";
        return jt.queryForInt(sql);
    }
    /**
     * 其他查询
     */
    public List selectCityByPage(int start,int end){
        String sql = "select * from city limit ?,?";
        Object[] args = {start,end};
        return jt.queryForList(sql,args);
    }
}

实体:

package com.etoak.bean;

public class City {
    private Integer id;
    private Integer pid;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getPid() {
        return pid;
    }
    public void setPid(Integer pid) {
        this.pid = pid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public City(Integer id, Integer pid, String name) {
        super();
        this.id = id;
        this.pid = pid;
        this.name = name;
    }
    public City() {
        super();
    }

}

Spring框架学习(3)spring中使用jdbc的更多相关文章

  1. Spring框架学习03——Spring Bean 的详解

    1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...

  2. Spring框架学习02——Spring IOC 详解

    1.Spring IOC的基本概念 IOC(Inverse of Control)反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理.当某个Java对象(调用者)需要调用 ...

  3. Spring 框架学习(1)--Spring、Spring MVC扫盲

    纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...

  4. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

  5. Spring框架学习1

    AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理   Spring框架学习(一)   阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...

  6. Spring框架学习总结(上)

    目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...

  7. Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建

    之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...

  8. spring框架学习(三)junit单元测试

    spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...

  9. Spring框架学习之IOC(二)

    Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...

  10. Spring框架学习笔记(5)——Spring Boot创建与使用

    Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...

随机推荐

  1. C#发送Post请求,带参数,不带参数,指定参数

    1.不带参数发送Post请求 /// <summary> /// 指定Post地址使用Get 方式获取全部字符串 /// </summary> /// <param na ...

  2. Eolinker——前置用例的使用

    如下补充均是Eolinker的文档中未说明的部分 1.在Eolinker的API自动化测试中,点击“前置用例”,“添加前置用例” 2.给添加的接口命名完之后,点击名称进入到编辑页面,代码输入框的内容为 ...

  3. 禁止网页右键和复制,ctrl+a都不行。取消页面默认事件【全】。

    document.oncontextmenu=new Function("event.returnValue=false");document.onselectstart=new ...

  4. 从零开始做SSH项目(一)

    1.数据库脚本 用户表 CREATE TABLE `ybl`.`userinfo`( `id` INT NOT NULL AUTO_INCREMENT, `email` ) NOT NULL, `id ...

  5. 【我要学python】爬虫准备之了解基本的html标签

    HTML 标题 <h1>This is a heading</h1> HTML 段落 <p>This is a paragraph.</p> HTML ...

  6. Luogu P2146 软件包管理器(树链剖分+线段树)

    题意 给定\(n\)个软件包,每个软件包都有一个依赖软件包,安装一个软件包必须安装他的依赖软件包,卸载一个软件包必须先卸载所有依赖于它的软件包.给定\(m\)此操作,每次一个操作\(install/u ...

  7. Codeforces Round #260 (Div. 1) Boredom(DP)

    Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  8. 【Tempest】openstack自动化测试组件tempest及自动化测试工具nose

    宝宝心里苦,但是宝宝只能在这穷乡僻壤说,下周又要开组会必须得干点事了.这次是做论文中的实验部分,主要利用到了openstack中的自动化测试组件Tempest,具体原因不细说了. 安装 分两种安装方法 ...

  9. Cobol online program 传指针

  10. 【dfs序+AC自动机+树状数组】BZOJ2434-[Noi2011]阿狸的打字机

    [题目大意] 输入一个字符串,其中:(1)a..z:在字符串末尾添加当前字符(2)P:输出当前字符串(3)B:从当前字符串末尾删去一个字符. 给出m组查询,输出第i个输出的字符串在第j个输出的字符串内 ...