JdbcTemplate介绍

为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架,Spring Boot Spring Data-JPA。

作为 SpringJDBC 框架的核心, JDBC 模板的设计目的是为不同类型的JDBC操作提供模板方法. 每个模板方法都能控制整个过程,并允许覆盖过程中的特定任务。

通过这种方式,可以在尽可能保留灵活性的情况下,将数据库存取的工作量降到最低。

JdbcTemplate方法介绍

JdbcTemplate主要提供以下五类方法:

1、execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

Execute、executeQuery、executeUpdate

2、update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句 SQL SERVCER(GO SQL语句 GO) ;

3、query方法及queryForXXX方法:用于执行查询相关语句;

4、call方法:用于执行存储过程、函数相关语句。

JdbcTemplate实现增删改查

JdbcTemplate添加数据

1. 使用配置实现
1.1 创建实体类
public class Account {
    private  Integer accountid;
    private  String accountname;
    private Double balance;

    public Integer getAccountid() {
        return accountid;
    }

    public void setAccountid(Integer accountid) {
        this.accountid = accountid;
    }

    public String getAccountname() {
        return accountname;
    }

    public void setAccountname(String accountname) {
        this.accountname = accountname;
    }

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }
}

实体类

1.2 创建Dao层
//查询所有所有账户
public List<Account> getAllAccounts();

查询方法

.3 创建Dao层实现类并继承JdbcDaoSupport接口

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
    public List<Account> getAllAccounts() {
        JdbcTemplate jdbcTemplate = getJdbcTemplate();
        String sql = "select * from  accounts";

        //RowMapper:接口  封装了记录的行映射关系
        List<Account> lists = jdbcTemplate.query(sql, new RowMapper<Account>() {

            @Override
            public Account mapRow(ResultSet resultSet, int i) throws SQLException {
                //创建Account对象
                Account account = new Account();
                //从ResultSet中解数据保到Accounts对象中
                account.setAccountid(resultSet.getInt("accountid"));
                account.setAccountname(resultSet.getString("accountname"));
                account.setBalance(resultSet.getDouble("balance"));

                return account;
            }
        });

return account;

}

实现查询方法

1.4创建Service层
//查询所有所有账户
public List<Account> getAllAccounts();

查询方法

1.5创建Service层实现类
AccountDao accountDao;
@Override
public List<Account> getAllAccounts() {
    List<Account> allAccounts = accountDao.getAllAccounts();
    return allAccounts;
}

实现查询方法

1.6 编写applicationContext.xml文件
<!--识别到配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<!--spring内置的数据源:提供连接的,不负责管理,使用连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!--构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="accountDao" class="cn.spring.accounttest.dao.impl.AccountDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--Service-->
<bean id="accountService" class="cn.spring.accounttest.service.impl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
</bean>

applicationContext.xml

1.7编写测试类 
@Test
public void getAllAccount(){
    ApplicationContext  context=new ClassPathXmlApplicationContext("applicationContext.xml");
    //从spring容器中获取Service对象
    AccountService accountService = (AccountService)context.getBean("accountService");
    List<Account> allAccounts = accountService.getAllAccounts();
    for (Account account:allAccounts) {
        System.out.println("账户名:"+account.getAccountname()+",余额为:"+account.getBalance());
    }
}

查询测试类

2. 使用注解方式实现
2.1 创建实体类

 

实体类
2.2 创建Dao层
查询方法
2.3 创建Dao层实现类
@Repository
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    Account account = new Account();
    @Override
    public List<Account> getAllAccounts() {

        String sql = "select * from  accounts";

        //自动映射
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class);
        List<Account> query = jdbcTemplate.query(sql, rowMapper);
        for (Account account : query) {
            System.out.println(account);
        }
        return query;
        }
    }

Dao实现类

2.4创建Service层
2.5创建Service层实现类
实现查询方法
2.6编写applicationContext.xml文件
<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"/>

<!--识别到配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<!--spring内置的数据源:提供连接的,不负责管理,使用连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!--构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>

applicationContext.xml

2.7编写测试类
查询测试类

JdbcTemplate实现增删改操作

使用注解方式实现,配置式同添加操作

1.创建Dao层
//删除账户
public int delAccount(int id);

//添加用户
public int addAccount(Account account);

//修改账户
public int updaAccount(Account account);

增删改方法

2.创建Dao曾实现类
@Override
 public int delAccount(int id) {

     String sql="delete from accounts where accountid=2";
     int count = jdbcTemplate.update(sql);
     return count;
 }

@Override
 public int addAccount(Account account) {
     String sql="insert into Accounts(accountname,balance) values(?,?)";
     int count = jdbcTemplate.update(sql,account.getAccountname(),account.getBalance());
     return count;
 }

 @Override
 public int updaAccount(Account account) {
     String sql="update accounts set accountname=?,balance=? where accountid=?";
     int count = jdbcTemplate.update(sql, account.getAccountname(),account.getBalance(),account.getAccountid() );
     return count;
 }

增删改方法实现类

3. 创建Service层
增删改方法
4. 创建Service层实现类
增删改方法实现类
5. 编写applicationContext.xml文件
applicationContext.xml
6. 编写测试类
@Test
public void delAccount(){
    ApplicationContext  context=new ClassPathXmlApplicationContext("applicationContext.xml");
    AccountService accountService =(AccountService) context.getBean("accountServiceImpl");
    int i = accountService.delAccount(2);
    if (i>0){
        System.out.println("删除成功");
    }
}

@Test
public void  addAccount(){
    ApplicationContext  context=new ClassPathXmlApplicationContext("applicationContext.xml");
    AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl");
   Account account=new Account();
   account.setAccountname("刘磊");
   account.setBalance(Double.valueOf(784));
    int count = accountServiceImpl.addAccount(account);
    if (count>0){
        System.out.println("添加成功");
    }
}

@Test
public void updaAcccount(){
    ApplicationContext  context=new ClassPathXmlApplicationContext("applicationContext.xml");
    AccountService accountServiceImpl = (AccountService) context.getBean("accountServiceImpl");
    Account account=new Account();
    account.setAccountid(10);
    account.setAccountname("刘磊");
    account.setBalance(Double.valueOf(784));
    int count = accountServiceImpl.updaAccount(account);
    if (count>0){
        System.out.println("修改成功");
    }
}

增删改测试类

JdbcTemplate实现增删改查操作的更多相关文章

  1. spring学习(四)spring的jdbcTemplate(增删改查封装)

    Spring的jdbcTemplate操作 1.Spring框架一站式框架 (1)针对javaee三层,每一层都有解决技术 (2)到dao 层,使用 jdbcTemplate 2.Spring对不同的 ...

  2. mongoVUE的增删改查操作使用说明

    mongoVUE的增删改查操作使用说明 一. 查询 1. 精确查询 1)右键点击集合名,再左键点击Find 或者直接点击工具栏上的Find 2)查询界面,包括四个区域 {Find}区,查询条件格式{& ...

  3. (转)SQLite数据库增删改查操作

    原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...

  4. 详谈easyui datagrid增删改查操作

    转自:http://blog.csdn.net/abauch_d/article/details/7734395 前几天我把easyui dadtagrid的增删改查的实现代码贴了出来,发现访问量达到 ...

  5. PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码

    PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...

  6. 浅谈dataGridView使用,以及画面布局使用属性,对datagridview进行增删改查操作,以及委托使用技巧

        通过几天的努力后,对datagridview使用作一些简要的介绍,该实例主要运用与通过对datagridview操作.对数据进行增删改查操作时,进行逻辑判断执行相关操作.简单的使用委托功能,实 ...

  7. 05_Elasticsearch 单模式下API的增删改查操作

    05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...

  8. JDBC连接数据库及增删改查操作

    什么是JDBC?Java语言访问数据库的一种规范,是一套APIJDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语言中的接口和类 ...

  9. Elasticsearch 单模式下API的增删改查操作

    <pre name="code" class="html">Elasticsearch 单模式下API的增删改查操作 http://192.168. ...

随机推荐

  1. 2018年秋招总结篇(Java)

    博主开始找工作是10月10号,感觉可以出去找找工作,然后就去了,参加了多场面试.笔试,现在总结一下 1.笔试篇 String StringBuffer StringBuilder的区别? HashMa ...

  2. MySQL命令窗口出现中文乱码的解决方法

    查询表语句的时候,出现了中文乱码,但是用Navicat for MySQL查看的时候却是正常的,字符集都是设置的utf-8,如下图所示:     其实上大学学习java的时候也遇到了中文乱码但是却没有 ...

  3. druid 连接池的配置参数

    介绍 DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生 ...

  4. Flask框架踩坑之ajax跨域请求

    业务场景: 前后端分离需要对接数据接口. 接口测试是在postman做的,今天才开始和前端对接,由于这是我第一次做后端接口开发(第一次嘛,问题比较多)所以在此记录分享我的踩坑之旅,以便能更好的理解,应 ...

  5. linux分析利刃之sar命令详解

    一.sar的概述 在我使用的众多linux分析工具中,sar是一个非常全面的一个分析工具,可以比较瑞士军刀,对文件的读写,系统调用的使用情况,磁盘IO,CPU相关使用情况,内存使用情况,进程活动等都可 ...

  6. CPU的物理数、核心数、线程数

    最近了解下CPU的参数,主要是对常见的CPU参数指标:物理数.核心数以及线程数做了下了解.增长了点自己的见识,方便自己回忆和分享,记录下来.参考了网上的一些说明并加以整理,形成该随笔.主要参考链接如下 ...

  7. Vue躬行记(2)——指令

    Vue不仅内置了各类指令,包括条件渲染.事件处理等,还能注册自定义指令. 一.条件渲染 条件渲染的指令包括v-if.v-else.v-else-if和v-show. 1)v-if 该指令的功能和条件语 ...

  8. JDK-基于Windows环境搭建

    JDK安装: 毋庸置疑你要跑java程序,肯定少不了JDK,如jemter还有还有~ 下载jdk地址1:https://pan.baidu.com/s/1FIvGNvZSy0EpCBxHCz07nA  ...

  9. 常用注解@Controller、@Service、@Autowired

    @Controller.@Service在spring-context-5.1.10.RELEASE.jar包下,所在包如下 @Autowired在spring-beans-5.1.10.RELEAS ...

  10. Java求和的整体思路

    一.    设计思想: 设计这个程序我们需要考虑到参数的输入,并且可以输入多个参数,以及为用户考虑到各种的边界问题.首先第一步我们应该给出输入参数的语句,让用户可以输入.第二步我们应对其进行参数个数的 ...