Spring数据源Spring数据源配置之JDBC

本文详细介绍了如何使用Spring框架中的JdbcTemplate进行数据库操作,包括配置数据源、执行更新、插入、删除、查询等常见操作,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.配置applicationContext.xml文件

<!-- 配置数据库 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/mytest3"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1111"></property>
	</bean>
	
	<bean id="bookDao" class="com.hellojava.dao.BookDao">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

2.业务层里面需要继承jdbcDaoSupport

1.> this.getJdbcTemplate().update(... ...)
2.> this.getJdbcTemplate().execute(... ...)
3.> this.getJdbcTemplate().query(... ...)
执行三者的区别:
update()只能数据表增删改操作
excute()可以执行任何SQL语句,甚至是create table...
query()用来执行查询操作
//比如关于修改采用两种方式
<span style="white-space:pre">	</span>//修改的第一种方法
	//update()只能执行数据表增删改这样的操作
	public int update(Book book){
		String sql="update book set bookName=?,bookAuthor=?,bookPrice=?,bookInfo=? where bookId=?";
		return this.getJdbcTemplate().update(sql, new PreparedStatementSetter(){
			public void setValues(PreparedStatement psment) throws SQLException {
				psment.setString(1, book.getBookName());
				psment.setString(2, book.getBookAuthor());
				psment.setDouble(3, book.getBookPrice());
				psment.setString(4, book.getBookInfo());
				psment.setInt(5, book.getBookId());
			}
		});
	}
	//修改的第二种方法
	//execute()可以执行任何SQL语句,甚至是"create table..."
	public int update1(Book book){
		String sql="update book set bookName=?,bookAuthor=?,bookPrice=?,bookInfo=? where bookId=?";
		return this.getJdbcTemplate().execute(sql, new PreparedStatementCallback<Integer>() {
			public Integer doInPreparedStatement(PreparedStatement psment) throws SQLException, DataAccessException {
				psment.setString(1, book.getBookName());
				psment.setString(2, book.getBookAuthor());
				psment.setDouble(3, book.getBookPrice());
				psment.setString(4, book.getBookInfo());
				psment.setInt(5, book.getBookId());
				return psment.executeUpdate();
			}
		});
	}
	
	public Book loadById(int bookId){
		Book book=new Book();
		String sql="select * from book where bookId=?";
		this.getJdbcTemplate().query(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
				PreparedStatement psment=connection.prepareStatement(sql);
				psment.setInt(1, bookId);
				return psment;
			}
		}, new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				book.setBookId(rs.getInt("bookId"));
				book.setBookAuthor(rs.getString("bookAuthor"));
				book.setBookName(rs.getString("bookName"));
				book.setBookPrice(rs.getDouble("bookPrice"));
				book.setBookInfo(rs.getString("bookInfo"));
			}
		});
		return book;
	}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值