博客
关于我
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.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>