我是靠谱客的博主 爱听歌小蝴蝶,这篇文章主要介绍七十四、Spring与DAO操作 query(),现在分享给大家,希望可以做个参考。

JdbcTemplate类中还提供了大量的query()方法来处理各种对数据库表的查询操作。其中,常用的几个query()方法如下表所示:

方法说明
List query(String sql, RowMapper rowMapper)执行string类型参数提供的sQL语句,并通过RowMapper返回一个List类型的结果。
List query(String sql,PreparedStatementSetter pss RowMapper rowMapper)根据 string 类型参数提供的sQL语句创建PreparedStatement对象,通过RowMapper将结果返回到List中。
List query(String sql, Object[] args ,RowMapper rowMapper)使用Object[]的值来设置SQL语句中的参数值,采用RowMapper回调方法可以直接返回List类型的数据。
queryForObject(String sql,RowMapper rowMapper,Object ...args)将args参数绑定到sQL语句中,并通过RowMapper返回一个Object类型的单行记录。

queryForList( string sql,Object[] args,

class<T> elementType)

该方法可以返回多行数据的结果,但必须是返回列表,elementType参数返回的是List元素类型。

目录

对DB的查询操作

前景连接

案例实施

JDBC 模板对象是多例的


对DB的查询操作

        JDBC 模板的查询结果均是以对象的形式返回。根据返回对象类型的不同,可以将查询分为两类:简单对象查询,自定义对象查询。

  • 简单对象查询:查询结果为 String、Integer 等简单对象类型,或该类型做为元素的集合 类型,如 List等。
  • 自定义对象查询:查询结果为自定义类型,如 User 等,或该类型做为元素的集合类型, 如 List等。

前景连接

Spring与DAO操作 execute()icon-default.png?t=M1L8https://blog.csdn.net/m0_54925305/article/details/123149019?spm=1001.2014.3001.5501

Spring与DAO操作 update()icon-default.png?t=M1L8https://blog.csdn.net/m0_54925305/article/details/123169124?spm=1001.2014.3001.5501

        注:以下案例基于前置案例之上进行拓展query操作

案例实施

        1、数据库插入数据

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
mysql> select * from account; Empty set (0.00 sec) mysql> insert into account(username,balance) value("孙悟空",100); Query OK, 1 row affected (0.04 sec) mysql> insert into account(username,balance) value("唐僧",1000.00); Query OK, 1 row affected (0.01 sec) mysql> insert into account(username,balance) value("猪八戒",2000.00); Query OK, 1 row affected (0.01 sec) mysql> insert into account(username,balance) value("沙僧",5000.00); Query OK, 1 row affected (0.00 sec) mysql> select * from account; +----+-----------+---------+ | id | username | balance | +----+-----------+---------+ | 1 | 孙悟空 | 100 | | 2 | 唐僧 | 1000 | | 3 | 猪八戒 | 2000 | | 4 | 沙僧 | 5000 | +----+-----------+---------+ 4 rows in set (0.00 sec)

        2、AccountDao中添加查询方法

复制代码
1
2
3
4
5
// 通过id查询 public Account findAccountByid(int id); // 查询所有账户 public List<Account> finfAllccount();

        3、AccountDaoImpl中添加查询方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 查询账户信息 @Override public Account findAccountByid(int id) { // 定义SQL语句 String sql = "select * from account where id=?"; // 创建一个新的rowMapper对象 RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class); // 将id 绑定到SQL语句中,通过RowMapper返回一个Object类型的当行记录 return this.jdbcTemplate.queryForObject(sql, rowMapper, id); } // 查询所有账户信息 @Override public List<Account> finfAllccount() { // 定义SQL String sql = "select * from account"; // 创建一个新的rowMapper对象 RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class); // 执行静态的SQL查询,通过RowMapper返回结果 return this.jdbcTemplate.query(sql, rowMapper); }

        4、创建测试类JdbcTemplateTest_delete

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.Example.jdbc; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class JdbcTemplateTest_delete { public static void main(String[] args) { // 加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取AccountDao实例 AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); // 执行deleteAccount()方法,并获取返回结果 int num = accountDao.deleteAccount(1); if (num > 0) { System.out.println("成功删除了" + num + "条数据!"); } else { System.out.println("删除操作执行失败!"); } } @Test public void findAccountByIdTest() { // 加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取AccountDao实例 AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); // 执行findAccountById()方法 Account account = accountDao.findAccountById(1); System.out.println(account); } @Test public void findAllAccountTest() { // 加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取AccountDao实例 AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); // 执行findAllAccount()方法,获取Account对象的集合 List<Account> account = accountDao.findAllAccount(); // 循环输出集合中的对象 for (Account act : account) { System.out.println(act); } } }

        5、查找单个数据,执行findAccountByIdTest方法

        6、查找全部数据,执行findAllAccountTest方法

JDBC 模板对象多例

        JdbcTemplate 对象是多例的,即系统会为每一个使用模板对象的线程(方法)创建一个 JdbcTemplate 实例,并且在该线程(方法)结束时,自动释放 JdbcTemplate 实例。所以在每次使用 JdbcTemplate 对象时,都需要通过 getJdbcTemplate()方法来获取。

最后

以上就是爱听歌小蝴蝶最近收集整理的关于七十四、Spring与DAO操作 query()的全部内容,更多相关七十四、Spring与DAO操作内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部