Spring中jdbcTemplate使用样例

使用Spring的jdbcTemplate不需要写一下繁琐的try catch语句,同时Connection也由spring来管理,很方便。
一、创建数据库表

create table bookinfo
(
id varchar(20) primary key,
name varchar(20),
author varchar(20)
)

二、创建数据库访问Dao类

package com.junge.spring.dao;

import java.util.List;

import com.junge.spring.bean.Book;

public interface BookDao
{
List<Book> queryAll();

List<Book> queryByAuthor(String author);

void create(Book book);

void update(Book book);

void delete(String id);
}

package com.junge.spring.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.junge.spring.bean.Book;
import com.junge.spring.dao.BookDao;

public class JdbcTemplateBookDao extends JdbcDaoSupport implements BookDao
{

class BookRowMapper implements RowMapper<Book>
{

@Override
public Book mapRow(ResultSet rs, int rowNum) throws SQLException
{
Book book = new Book();
book.setId(rs.getString("id"));
book.setName(rs.getString("name"));
book.setAuthor("author");
return book;
}

}

@Override
public List<Book> queryAll()
{
return getJdbcTemplate().query("select * from bookinfo",
new BookRowMapper());

}

@Override
public List<Book> queryByAuthor(String author)
{
return getJdbcTemplate().query("select * from bookinfo where author=?",
new Object[] { author }, new BookRowMapper());

}

@Override
public void create(Book book)
{
this.getJdbcTemplate()
.update("insert into bookinfo (id,name,author) values(?,?,?)",
new Object[] { book.getId(), book.getName(),
book.getAuthor() });

}

@Override
public void update(Book book)
{
this.getJdbcTemplate()
.update("update bookinfo set name=?,author=? where id=?",
new Object[] { book.getName(), book.getAuthor(),
book.getId() });

}

@Override
public void delete(String id)
{
this.getJdbcTemplate().update("delete from bookinfo where id=?",
new Object[] { id });

}

}


三、修改配置文件(jdbc_beans.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-3.0.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:3306/bookstore" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>


<bean id="jdbcTemplateBookDao" class="com.junge.spring.dao.impl.JdbcTemplateBookDao">
<property name="dataSource" ref="dataSource" />
</bean>

</beans>



四、测试(JdbcTemplateTest.java)
package com.junge.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.junge.spring.bean.Book;
import com.junge.spring.dao.BookDao;

public class JdbcTemplateTest
{

/**
* @param args
*/
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"jdbc_beans.xml");

BookDao bookDao = (BookDao) context.getBean("jdbcTemplateBookDao");

Book book = new Book();

book.setName("haoshu");
book.setAuthor("zhangsan");
book.setId("6101212");

bookDao.create(book);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值