内容源自: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. 使用php后台给自己做一个页面路由,配合ajax实现局部刷新。

    今天就要放假了,把近来囤积的小玩意儿总结整理一下. 在请求一个html页面来嵌入到当前页会有一个问题,就是跟随请求过来的html他的样式表和脚本会失效.是因为文档加载的先后顺序等问题造成的.因此,加载 ...

  2. i redis-desktop-manager 安装

    进入 https://redisdesktop.com/download https://github.com/uglide/RedisDesktopManager/releases 选择ubuntu ...

  3. vue-music 关于Search(搜索页面)-- 搜索历史

    搜索历史展示每一次搜索过,并选中的关键字,保存数据到数组.搜索历史数据是需要在多个组件中共享的,所以保存在vuex 中 searchHistory 数组中,保存触发在搜索列表点击选中之后派发事件到se ...

  4. 简单邮件传输协议SMTP

    1.SMTP是由源地址到目的地址传送邮件的一组规则,用来控制信件的中转方式. 2.SMTP服务器是遵循SMTP协议的发送邮件服务器,用来发送或者中转发出的邮件,客户端通过SMTP命令与SMTP服务器进 ...

  5. 第6天-javascript事件

    什么是事件 事件是用户在访问页面执行时的操作,也就是用户访问页面时的行为.当浏览器探测到一个事件时,比如鼠标点击或者按键.它可以触发与这个事件相关的JavaScript对象(函数),这些对象成为事件处 ...

  6. python 打包详解

    基本步骤: 1. 写setup.py 2. 运行“python setup.py sdist” 3. 在当前目录下会生成文件夹“dist”,打包好的代码就在dist中,以“.tar.gz”的形式被压缩 ...

  7. Linux基础系列-Day2

    基础命令(文件内容管理) 1.cat:在当前终端显示文本文件内容 格式:cat [文件路径] -n 从1开始对所有输出的行数编号 -b 和-n相似,只不过对于空白行不编号:2.head:从文件内容开头 ...

  8. 将Electron桌面应用转化为Win10的UWP应用

    如果有小伙伴不知道如何打包Electron桌面应用的,请戳我. 微软提供了一款快捷工具:electron-windows-store,用于将electron桌面应用打包成Windows10系统上.ap ...

  9. 【BZOJ 3530】【SDOI 2014】数数

    http://www.lydsy.com/JudgeOnline/problem.php?id=3530 上午gty的测试题,爆0了qwq 类似文本生成器那道题,把AC自动机的转移建出来,准确地说建出 ...

  10. HDU 6060 RXD and dividing(LCA)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6060 [题目大意] 给一个n个节点的树,要求将2-n号节点分成k部分, 然后将每一部分加上节点1, ...