zoukankan      html  css  js  c++  java
  • 使用Spring的jdbcTemplate进一步简化JDBC操作

    先看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-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>
    

    在看SpringUtil类 

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public final class SpringUtil {
        private static ApplicationContext  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        public static Object getBean(String beanName){
             return ctx.getBean(beanName);
        }    
    }
    

    最后看DAO:

    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;
    public class BookDao {
    
        private JdbcTemplate 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());
            }
        }
    }
    
  • 相关阅读:
    allocation size overflow
    数据库隔离级别深入理解(ORACLE)
    查看Orcale数据里的表是否有变化
    意外发现抽象类的构造器
    C语言学习快速笔记
    由javascript的闭包引申到程序语言编译上的自由变量作用域的考量
    easyui的datagrid的列checkbox自定义增加disabled选项
    数据库连接不关闭造成的问题以及RowSet的使用
    Quartz的JobDetail没有触发器指向时会被删除的问题
    发现浏览器开发工具的一个小问题
  • 原文地址:https://www.cnblogs.com/luoruiyuan/p/5498579.html
Copyright © 2011-2022 走看看