Spring用JdbcTemplate类query()方法查询数据库

JdbcTemplate类提供了大量的query()方法来各种对数据库的查询操作,常用的有:

写在前文:

这个表看看了解就好,后面有个印象。本文只举出queryForObject(String sql,RowMapper rowMapper,Object...args)方法和query(String sql,RowMapper rowMapper)方法,即表中第一个和第四个。

目录

准备工作:

 步骤:

1.在包com.itheima.jdbc里创建Account类,声明属性id、username和balance,通过Source创建gettter()  setter()方法、toString()方法。

2、 在包下创建接口AccountDao,声明通过id查找账户的方法和查找所有账户的方法。

3、在包下 创建接口AccountDao的实现类AccountDaoImpl,编写接口中的两个方法

4、在src下创建配置文件applicationContext.xml

5、在包下创建类JdbcTemplateTest

(1)、queryForObject(String sql,RowMapper rowMapper,Object...args)方法的测试

(2)、query(String sql,RowMapper rowMapper)方法的测试


准备工作:

本文事实上是建立在上一篇文章基础上的,https://blog.csdn.net/qq_42023080/article/details/105182890

向建立好的web项目中导入需要的包:Spring架构需要的jar包和第三方依赖包、数据库驱动jar包、jdbc的jar包和数据库事务处理的jar包

在数据库中创建表并插入数据:参考上一篇文章,https://blog.csdn.net/qq_42023080/article/details/105182890

 步骤:

1.在包com.itheima.jdbc里创建Account类,声明属性id、username和balance,通过Source创建gettter()  setter()方法、toString()方法。

package com.itheima.jdbc;

public class Account {
	private Integer id;
	private String username;
	private Integer balance;

	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 Integer getBalance() {
		return balance;
	}

	public void setBalance(Integer balance) {
		this.balance = balance;
	}

	@Override
	public String toString() {
		return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
	}
	
	
}

2、 在包下创建接口AccountDao,声明通过id查找账户的方法和查找所有账户的方法。

package com.itheima.jdbc;

import java.util.List;

public interface AccountDao {

	public Account findAccountById(int id);
	public List<Account> findAllAccount();

}

3、在包下 创建接口AccountDao的实现类AccountDaoImpl,编写接口中的两个方法

package com.itheima.jdbc;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class AccountDaoImpl implements AccountDao{
	
//	声明JdbcTemplate属性及其setter方法
	private JdbcTemplate jdbcTemplate;
	

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	
	@Override
	public Account findAccountById(int id) {
		String sql = "select * from account1 where id=?";
//		创建一个新的BeanPropertyRowMapper对象
		RowMapper<Account> rowMapper = 
				new BeanPropertyRowMapper<Account>(Account.class);
		
		return this.jdbcTemplate.queryForObject(sql,rowMapper,id);
	}

	@Override
	public List<Account> findAllAccount() {
		String sql = "select * from account1 ";
//		创建一个新的BeanPropertyRowMapper对象
		RowMapper<Account> rowMapper = 
				new BeanPropertyRowMapper<Account>(Account.class);
		return this.jdbcTemplate.query(sql, rowMapper);
	}
	
	

}

JdbcTemplate调用queryXXX方法,其中通过BeanPropertyRowMapper实现类将获取的值封装到对象内。而BeanPropertyRowMapper是实现了RowMapper接口。

当pojo对象(简单的java对象)的属性与数据库中对应表的字段名不一致时,则需要自定RowMapper接口实现类,否则对象结果中的值为null或基本类型数据默认值。

4、在src下创建配置文件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/beans
    http://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 name="url" value="jdbc:mysql://localhost/spring?serverTimezone=UTC"/>
    <property name="username" value="root"/>
    <property name="password" value="1111" />
    </bean>
    
    <!-- 配置JdbcTemplate模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
</beans>

5、在包下创建类JdbcTemplateTest

(1)、queryForObject(String sql,RowMapper rowMapper,Object...args)方法的测试

package com.itheima.jdbc;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateTest {

	public static void main(String[] args) {
//                加载配置文件
		ApplicationContext applicationContext = new 
			ClassPathXmlApplicationContext("applicationContext.xml");

		//调用通过id查找账号的测试
		findAccountByIdTest(applicationContext);	

	}
	
	

	public static void findAccountByIdTest(ApplicationContext applicationContext) {
		AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
		
		Account account = accountDao.findAccountById(3);
		if(account!=null) {
			System.out.println(account);
		}else {
			System.out.println("通过id查询用户失败");
		}
	}
	
	
	public static void findAllAccountTest(ApplicationContext applicationContext) {
		AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
		
		List<Account> list = accountDao.findAllAccount();
//               遍历集合list
		for(Account a:list) {
			System.out.println(a);
		}
	}
	
	
}

运行结果;

(2)、query(String sql,RowMapper rowMapper)方法的测试

这里不需要改动太多。在上一个方法的测试中,只需修改mian方法里调用的方法就好

package com.itheima.jdbc;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateTest {

	public static void main(String[] args) {
//                加载配置文件
		ApplicationContext applicationContext = new 
			ClassPathXmlApplicationContext("applicationContext.xml");

		//调用通过id查找账号的测试
		findAllAccountTest(applicationContext);	

	}
	
	

	public static void findAccountByIdTest(ApplicationContext applicationContext) {
		AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
		
		Account account = accountDao.findAccountById(3);
		if(account!=null) {
			System.out.println(account);
		}else {
			System.out.println("通过id查询用户失败");
		}
	}
	
	
	public static void findAllAccountTest(ApplicationContext applicationContext) {
		AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
		
		List<Account> list = accountDao.findAllAccount();
//               遍历集合list
		for(Account a:list) {
			System.out.println(a);
		}
	}
	
	
}

运行效果:

本文完

发布了38 篇原创文章 · 获赞 9 · 访问量 1453

猜你喜欢

转载自blog.csdn.net/qq_42023080/article/details/105239617
今日推荐