内容源自: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. iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8001 -j DNAT --to-destination 172.17.0.5:8080 ! -i docker0: iptables: No chain/target/match by that name.

    在docker容器上部署项目后,启动docker容器,出现 iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dpor ...

  2. RecyclerView 和 ListView 使用对比分析

    原文地址:http://blog.coderclock.com/2016/08/08/android/RecyclerView%20%E5%92%8C%20ListView%20%E4%BD%BF%E ...

  3. Web页面中两个listbox的option的转移

    Html: <div><span>所选时间:</span><select id="xuanyongTimelb" style=" ...

  4. c++风格

    http://web.archive.org/web/20160430022340/http://google.github.io/styleguide/cppguide.html 主要注意几点: 函 ...

  5. Socket读取页面

    http://www.knowsky.com/363189.html http://hi.baidu.com/myyers/item/f90fa3f57d89e1d243c36a34 http://h ...

  6. 【置换群】poj3270 Cow Sorting

    并不应该叫置换群……只是用到了置换而已,并没有群. 题解看这个吧,我就不写了:http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669013.ht ...

  7. Java学习笔记(11)

    自定义异常类: 自定义异常类的步骤:自定义一个类继承Exception即可 public class CustomClass { public static void main(String[] ar ...

  8. codevs 1966 乘法游戏

    1966 乘法游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 乘法游戏是在一行牌上进行的.每一张牌包括了一个正整数.在每 ...

  9. centos svnversion安装部署

    第一步: yum install subversion; 第二步: mkdir /data/svn/conf mkdir /data/svn/library 第三步: svnadmin create  ...

  10. WebAPI 操作返回

    定义了一个返回枚举: public enum ResultExceptionEnum { 积分不足 = , 支付失败 = , 用户不存在 = , 验证码发送失败 = , 验证码不正确 = , 账号已存 ...