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

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

Spring JdbcTemplate CRUD????

????

?????????????

  • ???????
  • Spring JdbcTemplate CRUD????
  • ???RowMapper??
  • ???????????

?????

package cn.guardwhy.domain;@Data@NoArgsConstructor@AllArgsConstructorpublic class Account {    private Integer id;    private String name;    private Float money;}

Spring JdbcTemplate CRUD????

????

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.util.List;public class JdbcTemplateCRUD {    public static void main(String[] args) {        // ??Spring???????Spring IOC??        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");                // ??JdbcTemplate??        JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");                // ??????        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; } }); // ???? for (Account account : list) { System.out.println(account); } // ?????? System.out.println("??????-------"); Integer accountNum = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM account", Integer.class); System.out.println("??????:" + accountNum); }}

???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
{ @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; }}

????

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;public class JdbcTemplateCRUD {    public static void main(String[] args) {        // ??Spring???????Spring IOC??        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");                // ??JdbcTemplate??        JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");                // ??????RowMapper????        List
list = jdbcTemplate.query("SELECT * FROM account", new AccountRowMapper()); // ???? for (Account account : list) { System.out.println(account); } // ?????? System.out.println("??????-------"); Integer accountNum = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM account", Integer.class); System.out.println("??????:" + accountNum); }}

????

?????????????Spring JdbcTemplate?CRUD??????????????????????????????RowMapper??????????????????????????

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

你可能感兴趣的文章
python pandas中融化的对面
查看>>
python pandas从时间序列中提取唯一日期
查看>>
Python PyQt5 将不再显示此消息复选框添加到 QMessageBox
查看>>
Python PyQt5:如何使用 PyQt5 显示错误消息
查看>>
Python PYSFTP-以字符串/文本形式传递私钥,而不是传递文件路径
查看>>
Python pytest 面试题!
查看>>
Python pytz 时区函数返回一个相差 9 分钟的时区
查看>>
python rabbitmq实现简单/持久/广播/组播/topic/rpc消息异步发送可配置Django
查看>>
Python random和json模块
查看>>
Python random模块seed理解
查看>>
python range()函数
查看>>
Python rdflib可传递查询
查看>>
python redis 集群_python 搭建redis集群
查看>>
python redis连接,在Python中使用Redis连接池的正确方法
查看>>
python regex_Python RegEx
查看>>
python requests post 中文结果请求得到unicode
查看>>
Python Requests接口自动化测试实战
查看>>
Python requests模块
查看>>
python request与grequests该如何选择
查看>>
python request模块
查看>>