博客
关于我
Spring- JdbcTemplate(CRUD)
阅读量:82 次
发布时间:2019-02-25

本文共 3569 字,大约阅读时间需要 11 分钟。

1.1 项目目录

1.2 代码示例

账户实体类

package cn.guardwhy.domain;/** * 账户实体类 */@Data@NoArgsConstructor@AllArgsConstructorpublic class Account {       private Integer id;    private String name;    private Float money;}

测试代码

package cn.guardwhy.jdbc;import cn.guardwhy.domain.Account;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;/** * spring JdbcTemplate实现完整CRUD操作 */public class JdbcTemplateCRUD {       public static void main(String[] args) {           // 1.加载spring配置文件,创建spring IOC容器        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");        // 2.从spring IOC容器中,获取JdbcTemplate        JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");        // 3.查询多行        List
list = jdbcTemplate.query("select id,name,money from account", new RowMapper
() { @Override public Account mapRow(ResultSet rs, int index) throws SQLException { // 创建账户对象 Account account = new Account(); account.setId(rs.getInt("id")); account.setName(rs.getString("name")); account.setMoney(rs.getFloat("money")); return account; } }); // 4.打印结果集 for(Account account:list){ System.out.println(account); } // 5.查询一行一列 System.out.println("查询一行一列-------"); Integer accountNum = jdbcTemplate.queryForObject("select count(*) from account", Integer.class); System.out.println("当前账户数量:" + accountNum); }}

执行结果

1.3 自定义RowMapper

package cn.guardwhy.resources;import cn.guardwhy.domain.Account;import org.springframework.jdbc.core.RowMapper;import java.sql.ResultSet;import java.sql.SQLException;public class AccountRowMapper implements RowMapper
{ /** * 结果集映射的方法: * 结果集中的每一行记录,都会调用一次该方法 */ public Account mapRow(ResultSet rs, int index) throws SQLException{ // 创建账户对象 Account account = new Account(); account.setId(rs.getInt("id")); account.setName(rs.getString("name")); account.setMoney(rs.getFloat("money")); return account; }}

1.4 测试代码

package cn.guardwhy.jdbc;import cn.guardwhy.domain.Account;import cn.guardwhy.resources.AccountRowMapper;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate;import java.util.List;/** * spring JdbcTemplate实现完整CRUD操作 */public class JdbcTemplateCRUD {       public static void main(String[] args) {           // 1.加载spring配置文件,创建spring IOC容器        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");        // 2.从spring IOC容器中,获取JdbcTemplate        JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");        // 3.使用自定义的RowMapper        List
list = jdbcTemplate.query("select * from account", new AccountRowMapper()); // 4.打印结果集 for(Account account:list){ System.out.println(account); } // 5.查询一行一列 System.out.println("查询一行一列-------"); Integer accountNum = jdbcTemplate.queryForObject("select count(*) from account", Integer.class); System.out.println("当前账户数量:" + accountNum); }}

1.5 执行结果

转载地址:http://zse.baihongyu.com/

你可能感兴趣的文章
Mysql5.6主从复制-基于binlog
查看>>
MySQL5.6忘记root密码(win平台)
查看>>
MySQL5.6的Linux安装shell脚本之二进制安装(一)
查看>>
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>