spring-使用JdbcTemplate完成查詢所有記錄-RowMapper

來源:互聯網
上載者:User

標籤:spring-使用jdbctemplate完成查詢所有記錄-rowmapper

一、建立spring項目
    項目名稱:spring101306
二、在項目上添加jar包
    1.在項目中建立lib目錄
        /lib
    2.在lib目錄下添加spring支援
        commons-logging.jar
        junit-4.10.jar
        log4j.jar
        mysql-connector-java-5.1.18-bin.jar
        spring-beans-3.2.0.RELEASE.jar
        spring-context-3.2.0.RELEASE.jar
        spring-core-3.2.0.RELEASE.jar
        spring-expression-3.2.0.RELEASE.jar
        spring-jdbc-3.2.0.RELEASE.jar
        spring-tx-3.2.0.RELEASE.jar
        com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
三、在項目中添加設定檔與屬性檔案
    1.在項目中建立conf目錄
    2.在conf目錄下添加屬性檔案
        屬性檔案名稱:jdbc.properties
        屬性檔案內容:
        jdbc.url=jdbc:mysql://localhost:3306/spring
        jdbc.driver=com.mysql.jdbc.Driver
        jdbc.username=root
        jdbc.password=root
    2.在conf目錄下添加spring核心設定檔
        設定檔名稱: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"
                       xmlns:context="http://www.springframework.org/schema/context"
                       xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
                
                <!-- 載入屬性檔案 -->
                <context:property-placeholder location="classpath:jdbc.properties"/>
                
                <!-- 1.設定資料庫串連池 -->
                <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                    <property name="jdbcUrl" value="${jdbc.url}"></property>
                    <property name="driverClass" value="${jdbc.driver}"></property>
                    <property name="user" value="${jdbc.username}"></property>
                    <property name="password" value="${jdbc.password}"></property>
                </bean>
        </beans>
四、實現bean設計
    1.在src目錄下建立實體bean的包
        包名:cn.jbit.spring101306.domain
    2.在包下建立實體bean
        public class Temp {
            private Integer tempId;
            private String tempName;
            //省略get and set
        }
五、設計Dao層
    1.在src目錄下建立dao層的包
        包名:cn.jbit.spring101306.dao
    2.在包下建立dao層的介面與實作類別
        1)介面設計
            介面名稱:ITempDao.java
            介面內容:
            public interface ITempDao {
                public List<Temp> findAll2();
            }
        2)介面實作類別設計
            實作類別名稱:TempDaoImpl.java
            實作類別內容:
            public class TempDaoImpl extends JdbcDaoSupport implements ITempDao {
                @Override
                public List<Temp> findAll1() {
                    List<Temp> temps = this.getJdbcTemplate().query("select * from temp", new ResultSetExtractor<List<Temp>>(){
                        @Override
                        public List<Temp> extractData(ResultSet rs) throws SQLException,
                                DataAccessException {
                            List<Temp> temps = new ArrayList<Temp>();
                            while(rs.next()){
                                int tid = rs.getInt("tid");
                                String tname= rs.getString("tname");
                                Temp temp = new Temp();
                                temp.setTempId(tid);
                                temp.setTempName(tname);
                                temps.add(temp);
                            }
                            return temps;
                        }
                        
                    });
                    return temps;
                }
            }
六、在核心設定檔中配置Dao
    <!-- 配置Dao -->
    <bean id="tempdao" class="cn.jbit.spring101306.dao.TempDaoImpl">
        <!-- 為jdbcTemplate進行注入 -->
        <property name="dataSource" ref="dataSource"></property>    
    </bean>
七、測試
    1.在項目上建立test目錄
        /test
    2.在test目錄下建立測試包
        包名:cn.jbit.spring101301.dao
    3.在測試包下建立測試類別
        測試類別名:JdbcTemplateDemo.java
        測試類別的內容:
        public class JdbcTemplateDemo {
            /**
             * 使用spring jdbctemplate查詢所有
             */
            @Test
            public void testJdbcTemplateFindAllByRowMapper(){
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                ITempDao tempDao = (ITempDao) context.getBean("tempdao");
                List<Temp> temps = tempDao.findAll2();
                for (Temp temp : temps) {
                    System.out.println(temp.getTempId()+"------->"+temp.getTempName());
                }
            }
        }

本文出自 “素顏” 部落格,請務必保留此出處http://suyanzhu.blog.51cto.com/8050189/1563392

spring-使用JdbcTemplate完成查詢所有記錄-RowMapper

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.