Spring JdbcTemplate使用

Spring DAO 对jdbc 改进

1 	简化DAO 实现类编写    对jdbc的api做了封装和简化 

2 	提供了基于AOP 的事务管理 

3 	对JDBC 中的异常做了改进   把原来的检查的异常  封装成了 继承自RuntimeException 的  DataAccessException 

4 	JdbcDaoSupport  这个类   可以提供JdbcTemplate 类型的对象  

5 	JdbcTemplate   是SpringDAO 访问数据的核心类 

6 	自动加载驱动   自动获取sql执行环境   自动获取连接   自动释放资源 

Spring Dao 使用步骤

1 新建需要的表
2 导入ioc aop 数据库驱动 数据库连接池
3 编写 Dao 接口 写明需要的方法
4 配置 jdbcTemplate DataSource 组件
5 实现 Dao接口 在实现类中 引用 jdbcTemplate 来执行一系列的操作

1 创建表

//创建一个Bank的表
create table Bank(
 id int primary key,
 name varchar(20),
 pass varchar(20),
 money varchar(20)
)

2编写实现类

//初始化方法就不放了太长
public class Bank {
	int id;
	String name;
	String password;
	int money;
}

3 编写Dao接口

public interface BankDao {
	//查询数据库中的总数
	int Sum();
	
	//根据id查询对应的姓名
	String findNameByid(int id);

	//根据id查询对用的账户
	Bank findBankById(int id);

	//查询所有的账户
	List<Bank> findAll();
	
	//增加一条数据
	void add(Bank bank);

}

4编写实现类

//持久化层注解 命名当前Bean名称为bank
@Repository("bank")
public class Bankdaoimp implements BankDao {
	//注入JdbcTemplate 模板对象
	@Autowired
	JdbcTemplate jdbc;
	

	@Override
	public int Sum(){
		String sql = "select count(*) from bank";
		return 	jdbc.queryforObject(sql,Integr.class);
	}

	@Override
	public String findNameByid(int id) {
		String sql = "select name from bank where id = ?";
		//中间的参数是返回的类型 
		//如果查询的name是空的会出现DataAccessException异常
		//如果想查询为空的时候返回空可以捕获异常之后返回空
		try{
			String name =  jdbc.queryForObject(sql,String.class,id);
			return name; 
		}cath (DataAccessException e){
			return null;
		}
	}

	@Override
	public Bank findBankById(int id) {
		String sql="select * from bank where id = ?"
		//如果想要查询一个对象或者多个对象时需要实现RowMapp接口来完成数据的转换
		//中间的参数写实现RowMapper接口的实现类
		return jdbc.queryForObject(sql, new BankMapper(),name,pass );
	}

	@Override
	public List<Bank> findAll() {
		//如果想要查询一个对象或者多个对象时需要实现RowMapp接口来完成数据的转换
		//查询一个list时 只需要用query方法
		return jdbc.query("select * from bank", new BankMapper());
	}

	@Override
	public void add(Bank bank){
		String sql = "insert into bank values(?,?,?,?)";
		//当前方法的返回值是 int类型的执行了多少次会返回多少 没有操作数数据库会返回0
		//增删改 方法都是用Update 剩下的就不多操作了
		return jdbc.update(sql,bank.getID(),bank.getName(),bank.getPass(),bank.getMoney());
	}



	
}

//实现RowMapper接口的实现类
//RowMapper<Bank> 其中的泛型类型需要写想要转化的类型
public BankMapper implements RowMapper<Bank> {
	@Override
	public Bank mapRow(ResultSet res, int arg1) throws SQLException {
	//不能使用res.next方法 SpringDao 会自动遍历查询的结果集放入到对象中
		Bank bank = new Bank(res.getInt("id"),res.getString("name"),res.getString("password"),res.getString("money"));
		return bank;
	}
}

5 配置 jdbcTemplate DataSource 组件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	
	//开启组件扫描
	<context:component-scan base-package="com"></context:component-scan>
	//引入db.properties配置数据库连接地址 文件如下
	/*jdbc.driverClassName=com.mysql.jdbc.Driver
	jdbc.url=jdbc:mysql://127.0.0.1/test?characterEncoding=utf8
	jdbc.name=root
	jdbc.pass=root*/
	<context:property-placeholder location="classpath:db.properties"/>
 	
 	//配置DataSource对象给JdbcTemplate模板使用
 	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
 		<property name="url" value="${jdbc.url}"/>
 		<property name="username" value="${jdbc.name}"/>
 		<property name="password" value="${jdbc.pass}"/>
 	</bean>
 	//配置jdbcTemplate模板对象
 	<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
 		<property name="dataSource" ref="dataSource"></property>
 	</bean>
 	
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值