<?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-2.5.xsd">
<bean id="springDSN"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
</property>
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433;databasename=bbs">
</property>
<property name="username" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default" dependency-check="default">
<property name="dataSource">
<ref bean="springDSN" />
</property>
</bean>
<bean id="bookDao" class="com.yy.struts.dao.BookDao">
<property name="jdbcT">
<ref bean="jdbcTemplate" />
</property>
</bean>
</beans>
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import com.r.vo.Book;
public class BookDao {
private JdbcTemplate jdbcT;
public void setJdbcT(JdbcTemplate jdbcT) {
this.jdbcT = jdbcT;
}
public List findALL() {
String sql = "select * from BookInfo";
return jdbcT.queryForList(sql);
}
public List<Book> findALLBooks() {
List<Book> books = new ArrayList<Book>();;
String sql = "select * from BookInfo";
List list = jdbcT.queryForList(sql);
Iterator iterator = list.iterator();
Book book = null;
while (iterator.hasNext()) {
Map map4book = (Map) iterator.next();
book = new Book();
book.setBid((Integer) map4book.get("bid"));
book.setBookName((String)map4book.get("bookName"));
book.setBookType((String)map4book.get("bookType"));
book.setBookPic(((BigDecimal)map4book.get("bookPic")).doubleValue() );
book.setCount((Integer) map4book.get("count"));
books.add(book);
}
return books;
}
public int delete(int bid){
String sql = "delete from BookInfo where bid =?";
return jdbcT.update(sql, new Object[]{bid});
}
public static void main(String[] args) {
List<Book> books = new BookDao().findALLBooks();;
for(Book book:books){
System.out.println(book.getBid()+","+book.getBookName()+","+book.getBookType());
}
}
}