User.java实体类:

package com.liu.jdbc.update;public class User {private Integer id;private String username;private String password;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", password=" + password + "]";}}

UserDao.java接口:

package com.liu.jdbc.update;import java.util.List;public interface UserDao {//根据id查询数据public User selectById(int id);//查询所有数据public List<User> selectAll();}

UserDaoImpl.java实现类:

package com.liu.jdbc.update;import java.util.List;import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;public class UserDaoImpl implements UserDao {//声明JdbcTemplate属性及其setter方法private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}//根据id查询数据@Overridepublic User selectById(int id) {// TODO Auto-generated method stub//定于SQL语句String sql = "select * from user where id = ?";//创建一个新的BeanPropertyRowMapper对象RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);//将id绑定到SQL语句中,通过rowMapper返回一个Object类型的单行记录User user = this.jdbcTemplate.queryForObject(sql, rowMapper, 1);return user;}//查询所有数据@Overridepublic List<User> selectAll() {// TODO Auto-generated method stub//定义SQL语句String sql = "select * from user";//创建一个新的BeanPropertyRowMapper对象RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);//通过rowMapper返回结果List<User> list = this.jdbcTemplate.query(sql, rowMapper);return list;}}

ApplicationContext.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsd"><!-- 配置数据源 --><bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- 数据库驱动 --><property name="driverClassName" value = "com.mysql.jdbc.Driver"></property><!-- 数据库url --><property name="url" value = "jdbc:mysql://localhost:3306/test"></property><!-- 数据库用户名 --><property name="username" value = "root"></property><!-- 数据库密码 --><property name="password" value = "root"></property></bean><!-- 配置jdbc模板类 --><bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate"><!-- 默认必须使用数据源 --><property name="dataSource" ref = "dataSource"></property></bean><!-- 定义ID为userDdao的bean --><bean id = "userDao" class = "com.liu.jdbc.update.UserDaoImpl"><property name="jdbcTemplate" ref ="jdbcTemplate"></property></bean></beans>

Test.java测试类:

package com.liu.jdbc.update;import java.util.List;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String args[]) {Test test = new Test();test.selectById();//test.selectAll();}//通过id查询public void selectById() {String xmlpath = "com/liu/jdbc/update/ApplicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);UserDao userDao = (UserDao) applicationContext.getBean("userDao");User user = userDao.selectById(1);System.out.println(user);}//查询全部数据public void selectAll() {String xmlpath = "com/liu/jdbc/update/ApplicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);UserDao userDao = (UserDao) applicationContext.getBean("userDao");List<User> list = userDao.selectAll();for(User li : list) {System.out.println(li);}}
}

JdbcTemplate中的query方法(代码)相关推荐

  1. python中的object是什么意思_Python object类中的特殊方法代码讲解

    python版本:3.8class object: """ The most base type """ # del obj.xxx或del ...

  2. contentProvider中有关query方法的使用

    query()方法介绍  * 方法的声明如下:  * public final Cursor query(Uri uri, String[] projection, String selection, ...

  3. 【Groovy】闭包 Closure ( 闭包调用 与 call 方法关联 | 接口中定义 call() 方法 | 类中定义 call() 方法 | 代码示例 )

    文章目录 总结 一.接口中定义 call() 方法 二.类中定义 call() 方法 三.完整代码示例 总结 在 实例对象后使用 " () " 括号符号 , 表示调用该实例对象的 ...

  4. JdbcTmplate中的update方法(代码)基础操作

    User.java实体类: package com.liu.jdbc.update;public class User {private Integer id;private String usern ...

  5. php输出楼层号,ZBlog开发中实现评论楼层号正确输出的具体方法代码

    在官方的wiki中,针对评论部分的标签调用是集成了评论楼号的,即标签{$comment.FloorID}.这个标签是不计算子评论的,所以并不是采用key直接计算出的楼号.经过测试,我们会发现,这个楼号 ...

  6. php $db-gt;query 行数,php – 如何在CodeIgniter中组合query()和limit()方法

    是否可以在codeigniter中执行类似的操作: publi function getcat($category_id) { $query = "(SELECT cat.id, cat.t ...

  7. cdate在java中_Java Calendar.add方法代码示例

    本文整理汇总了Java中java.util.Calendar.add方法的典型用法代码示例.如果您正苦于以下问题:Java Calendar.add方法的具体用法?Java Calendar.add怎 ...

  8. python中shelf_Python cmds.shelfLayout方法代码示例

    本文整理汇总了Python中maya.cmds.shelfLayout方法的典型用法代码示例.如果您正苦于以下问题:Python cmds.shelfLayout方法的具体用法?Python cmds ...

  9. python中geometry用法_Python geometry.Point方法代码示例

    本文整理汇总了Python中shapely.geometry.Point方法的典型用法代码示例.如果您正苦于以下问题:Python geometry.Point方法的具体用法?Python geome ...

最新文章

  1. Access restriction: The type Unsafe is not accessible due to restriction on required library
  2. linux cat EOF使用示例
  3. Django redis的使用
  4. JS函数运行在它们被定义的作用域内,而不是它们被执行的作用域内
  5. linux系统如何使用fork函数创建子进程
  6. oracle数据库中函数和存储过程中的区别
  7. Oracle关于时间/日期的操作
  8. 基于HMM的连续小词量语音识别 - 模拟技术 - 电子发烧友网
  9. C# datagridview列绑定类中类的属性
  10. 云计算IaaS核心技术全景指南
  11. 文本聚类分析算法_常用的聚类分析算法
  12. 免费下载Microsoft Visual C++ 2010 Express (VSExpress)安装包地址分享速进
  13. HTML+CSS 仿QQ邮箱登录界面
  14. 10-25 查询选修张老师讲授所有课程的学生
  15. vue将图片保存到相册_Vue保存当前页面为图片
  16. 【JavaScript】用原生js实现幻灯片效果
  17. 更新后的哥德巴赫猜想(位运算)
  18. 在cmd运行java_用cmd运行java时的问题
  19. 自学生物信息学(思维+超全常用网站)
  20. 我的算法基础实验代码-上篇

热门文章

  1. SAP 财务模块 FI-TV 差旅管理
  2. sap 预制凭证与暂存凭证的区别
  3. 物料分类账业务配置及操作手册
  4. SAP_常用业务数据表设计
  5. 疫后“反弹式”增长,AI营销或为广告业打开一扇新窗
  6. 沽空机构两度狙击,波司登2018/19财年业绩显著,未来到底是否值得关注?
  7. vscode怎么弄php,vscode如何设置语言
  8. 线性代数可以速成吗_怎么在一个晚上搞定线性代数?
  9. java数组长度最大值_java 数组排序、最大值、最小值 | 学步园
  10. c语言 字符串 if,C语言用if(strstr(s1,s2))判断字符串是否存在子字符串,无论有没有都会进入到if里是怎么回事?...